Skip to content

Instantly share code, notes, and snippets.

@cgcardona
Created May 29, 2021 21:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cgcardona/57b1131899965499fbde794b8363d224 to your computer and use it in GitHub Desktop.
Save cgcardona/57b1131899965499fbde794b8363d224 to your computer and use it in GitHub Desktop.
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