Created
May 29, 2021 21:23
-
-
Save cgcardona/57b1131899965499fbde794b8363d224 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { | |
Buffer, | |
BinTools | |
} from "../../src" | |
import { | |
KeyChain, | |
KeyPair, | |
} from "../../src/apis/avm" | |
import * as bip39 from "bip39" | |
import * as HDKey from "hdkey" | |
import bintools from "../../src/utils/bintools" | |
import { HDNode } from "ethers/lib/utils" | |
const main = async (): Promise<any> => { | |
const bintools: BinTools = BinTools.getInstance() | |
const numberOfAddresses: number = 5 | |
const purpose: number = 44 | |
const coin_type: number = 9000 | |
const account: number = 0 | |
const change: number = 0 | |
const derivationPath: string = `m/${purpose}'/${coin_type}'/${account}'/${change}` | |
const mnemonic: string = bip39.generateMnemonic(256) | |
console.log(`Mnemonic: ${mnemonic}`) | |
const seed: any = bip39.mnemonicToSeedSync(mnemonic) | |
const hdkey: HDKey = HDKey.fromMasterSeed(seed) | |
const prefixes: string[] = ["X", "P", "C"] | |
prefixes.forEach((prefix: string): void => { | |
console.log(`${prefix} Addresses`) | |
for (let i: number = 0; i < numberOfAddresses; i++) { | |
const hdPath: string = `${derivationPath}/${i}` | |
// derive key pairs from a seed/bip39 for deposit addresses | |
const hdKey: HDKey = hdkey.derive(hdPath) as HDKey | |
const keyChain: KeyChain = new KeyChain("avax", prefix) | |
const pkHex: string = hdKey.privateKey.toString("hex") | |
const pkBuf: Buffer = new Buffer(pkHex, "hex") | |
const keyPair: KeyPair = keyChain.importKey(pkBuf) | |
let address: string = keyPair.getAddressString() | |
// validate bech32 address | |
bintools.stringToAddress(address) | |
console.log(`HDPath: ${hdPath}. Bech32 address: ${address}`) | |
if (prefix === "C") { | |
const hdNode = HDNode.fromSeed(pkBuf) | |
address = hdNode.address | |
// validate eth-style address | |
bintools.stringToAddress(address) | |
console.log(`ETH-style hex address: ${address}`) | |
} | |
} | |
console.log("++++++++++++++++++++++++++++++++++++") | |
console.log("\n") | |
}) | |
} | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment