Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import axios from 'axios
- import { generateMnemonic, validateMnemonic, mnemonicToEntropy } from 'bip39-light'
- const apiClient = axios.create({
- baseURL: 'https://graphql.rraayy.com'
- })
- export async function GetAdressesUTXO(addresses) {
- return apiClient
- .post('/', {
- query: `
- query utxoSetForAddress($addresses: [String]) {
- utxos(order_by: { value: desc }, where: { address: { _in: $addresses } }) {
- address
- value
- tokens {
- assetId
- assetName
- policyId
- quantity
- }
- }
- }
- `,
- variables: {
- addresses,
- },
- })
- .then((response) => {
- if (response) {
- return response.data
- }
- return false
- })
- .catch((err) => console.log(err))
- }
- class CardanoWasmModule {
- async load() {
- this.wasm = await import('@emurgo/cardano-serialization-lib-browser/cardano_serialization_lib')
- }
- get API() {
- return this.wasm
- }
- }
- /**
- * Mnemonic generation
- * @return {string}
- */
- export const CardanoGenerateMnemonic = (length = 24) => {
- return generateMnemonic((32 * length) / 3)
- }
- /**
- * Get private key, public key, reward address
- * @return {object}
- */
- export const CardanoGetAccountInfo = async (network, mnemonic) => {
- await CardanoWasm.load()
- const harden = (num) => {
- return 0x80000000 + num
- }
- const entropy = mnemonicToEntropy(mnemonic)
- const rootKey = CardanoWasm.API.Bip32PrivateKey.from_bip39_entropy(
- Buffer.from(entropy, 'hex'),
- Buffer.from(''),
- )
- const privateKey = rootKey.derive(harden(1852)).derive(harden(1815)).derive(harden(0))
- const stakeKey = privateKey.derive(2).derive(0).to_public()
- const currentNetwork = network === 'testnet'
- ? CardanoWasm.API.NetworkInfo.testnet().network_id()
- : CardanoWasm.API.NetworkInfo.mainnet().network_id()
- const rewardAddress = CardanoWasm.API.RewardAddress.new(
- currentNetwork,
- CardanoWasm.API.StakeCredential.from_keyhash(stakeKey.to_raw_key().hash()),
- )
- const rewardAddressBech32 = rewardAddress.to_address().to_bech32()
- const privateKeyBech32 = privateKey.to_bech32()
- const publicKeyBech32 = privateKey.to_public().to_bech32()
- return { rewardAddressBech32, privateKeyBech32, publicKeyBech32 }
- }
- /**
- * Generate address pack related to Private Key
- * @param {string} publicKeyBech32 private key
- * @param {string} type external internal / all derives
- * @param {number} shift shifting addresses by page size
- * @param {number} page page size
- * @return {array} adresses array
- */
- export const CardanoGetAccountAdresses = async (
- network,
- publicKeyBech32,
- type = 'external',
- shift = 0,
- page = 20,
- ) => {
- await CardanoWasm.load()
- const publicKey = CardanoWasm.API.Bip32PublicKey.from_bech32(publicKeyBech32)
- const accountAdresses = []
- const currentNetwork = network === 'testnet'
- ? CardanoWasm.API.NetworkInfo.testnet().network_id()
- : CardanoWasm.API.NetworkInfo.mainnet().network_id()
- const generateAddresses = (addressType) => {
- const tmpAddresses = []
- for (let i = 0 + page * shift; i < page + page * shift; i += 1) {
- const utxoPubKey = publicKey
- .derive(addressType) // 0 external / 1 internal
- .derive(i)
- const stakeKey = publicKey
- .derive(2) // chimeric
- .derive(0)
- const baseAddr = CardanoWasm.API.BaseAddress.new(
- currentNetwork,
- CardanoWasm.API.StakeCredential.from_keyhash(utxoPubKey.to_raw_key().hash()),
- CardanoWasm.API.StakeCredential.from_keyhash(stakeKey.to_raw_key().hash()),
- )
- const baseAddrBech32 = baseAddr.to_address().to_bech32()
- tmpAddresses.push(baseAddrBech32)
- }
- return tmpAddresses
- }
- switch (type) {
- case 'external':
- accountAdresses.push(...generateAddresses(0))
- break
- case 'internal':
- accountAdresses.push(...generateAddresses(1))
- break
- case 'all':
- accountAdresses.push(...generateAddresses(0))
- accountAdresses.push(...generateAddresses(1))
- break
- default:
- break
- }
- return accountAdresses
- }
- function* checkAddresses(type, shift, pageSize) {
- const tmpAddresses = yield call(
- Cardano.CardanoGetAccountAdresses,
- CARDANO_NETWORK,
- publicKey,
- type,
- shift,
- pageSize,
- )
- const tmpAddresssesUTXO = yield call(Explorer.GetAdressesUTXO, tmpAddresses)
- return [tmpAddresssesUTXO, tmpAddresses]
- }
- const UTXOArray = []
- const adressesArray = []
- const pageSize = 20
- const type = 'all'
- const maxShiftIndex = 10
- let shiftIndex = 0
- function* getAddressesWithShift(shift) {
- const [adresssesWithUTXOs, checkedAdresses] = yield call(checkAddresses, type, shift, pageSize)
- adressesArray.push(...checkedAdresses)
- if (adresssesWithUTXOs.data && shiftIndex < maxShiftIndex) {
- if (adresssesWithUTXOs.data.utxos.length) {
- shiftIndex += 1
- UTXOArray.push(...adresssesWithUTXOs.data.utxos)
- yield call(getAddressesWithShift, shiftIndex)
- }
- }
- }
- yield call(getAddressesWithShift, shiftIndex)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement