Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
it('can export a BIP32 xpriv, then import it', () => {
const mnemonic =
'praise you muffin lion enable neck grocery crumble super myself license ghost';
const seed = bip39.mnemonicToSeedSync(mnemonic);
const node = bip32.fromSeed(seed);
const strng = node.toBase58();
const restored = bip32.fromBase58(strng);
assert.strictEqual(getAddress(node), getAddress(restored)); // same public key
assert.strictEqual(node.toWIF(), restored.toWIF()); // same private key
});
function toPkey (mnemonic, type) {
console.log(mnemonic, type)
if (!validateMnemonic(mnemonic)) {
throw new Error('Wrong seed format. Seed must be 12 words.')
}
const seed = mnemonicToSeedSync(mnemonic)
const hdkey = fromMasterSeed(seed).derive('m’/44’/349’/0’/0')
let pkey, found
for (let i = 0; !found; i++) {
if (i > 100) {
// there must be something wrong, because the ratio of regular account is 50%
throw new Error('Too many tries deriving regular account from seed.')
}
pkey = hdkey.deriveChild(i).privateKey
const { address } = ecc.toPubKeyAndAddress(pkey)
found = codec.isAddressType(address, type)
}
return pkey
}
generateKeyPair = ({ mnemonic }: { mnemonic: string }) => {
// eslint-disable-next-line no-undef
if (!__generateKeyPair) {
sleep();
}
// Generate 64 seed bytes (512 bits) from phrase - this is a wallet's master seed
const seed = bip39.mnemonicToSeedSync(mnemonic);
let publicKey = new Uint8Array(32);
let secretKey = new Uint8Array(64);
const saveKeys = (pk, sk) => {
if (pk === null || sk === null) {
this.stopAndCleanUp();
throw new Error('key generation failed');
}
publicKey = pk;
secretKey = sk;
};
__generateKeyPair(seed, saveKeys); // eslint-disable-line no-undef
return { publicKey: toHexString(publicKey), secretKey: toHexString(secretKey) };
};
module.exports = ({index, network}) => {
if (!validateMnemonic(SSS_CLAIM_BIP39_SEED)) {
console.log([500, 'ExpectedValidMnemonic', generateMnemonic()]);
process.exit();
}
if (index === undefined || index < minIndex || index > maxIndex) {
throw new Error('ExpectedValidIndex');
}
if (!network || !networks[network]) {
throw new Error('ExpectedValidNetwork');
}
const net = networks[network];
const seed = mnemonicToSeedSync(SSS_CLAIM_BIP39_SEED);
const root = fromSeed(seed, networks[network]);
const keyPair = root.derivePath(`m/0'/0/${index}`);
return {
p2pkh_address: p2pkh({network: net, pubkey: keyPair.publicKey}).address,
p2wpkh_address: p2wpkh({network: net, pubkey: keyPair.publicKey}).address,
pk_hash: hash160(keyPair.publicKey).toString('hex'),
private_key: keyPair.toWIF(),
public_key: keyPair.publicKey.toString('hex'),
};
};
async init(basePath) {
this.basePath = basePath ? basePath : this.supportedPaths[0].path;
this.hdKey = HDKey.fromMasterSeed(
bip39.mnemonicToSeedSync(this.mnemonic, this.password)
);
}
getAccount(idx) {
private generateAccountFromKeys() {
const pvKey = PrivateKey.fromSeed(this.keys.key, this.currentNetwork);
const pbKey = pvKey.getPublicKey();
pbKey.setNetwork(this.currentNetwork);
const wallet = this.userDataProvider.getWalletByAddress(pbKey.getAddress());
const account: AccountBackup = {};
account.address = wallet.address;
account.mnemonic = this.keys.key;
account.publicKey = pbKey.toHex();
account.seed = bip39.mnemonicToSeedSync(account.mnemonic).toString('hex');
if (pbKey.network.wif) {
account.wif = pvKey.toWIF();
}
if (this.keys.secondKey) {
account.secondMnemonic = this.keys.secondKey;
}
this.account = account;
}
export function getSeedFromMnemonics(mnemonic: string, passphrase: string = '', wordlist: Array = bip39.wordlists.EN): {
seed: Buffer;
seedHex: Hex;
} {
const err = checkParams({ mnemonic, wordlist }, [ 'mnemonic', 'wordlist' ], [{
name: 'mnemonic',
func: _m => validateMnemonics(_m, wordlist)
}]);
if (err) {
throw new Error(err.message);
}
const seed: Buffer = bip39.mnemonicToSeedSync(mnemonic, passphrase);
const seedHex: Hex = seed.toString('hex');
return { seed, seedHex };
}
export function createMnemonic(bits: number = 256, wordlist: Array, pwd: string = '') {
const err = checkParams({ bits }, ['bits']);
if (err) {
throw new Error(err.message);
}
wordlist = wordlist || bip39.wordlists.EN;
const mnemonic = bip39.generateMnemonic(bits, null, wordlist);
const entropy = bip39.mnemonicToEntropy(mnemonic, wordlist);
const seed = bip39.mnemonicToSeedSync(mnemonic, pwd).toString('hex');
return { mnemonic, entropy, seed };
}
private generateAccountFromEntropy() {
const account: AccountBackup = {};
const wordlist = bip39.wordlists[this.wordlistLanguage || 'english'];
account.entropy = this.entropy;
account.mnemonic = bip39.entropyToMnemonic(account.entropy, wordlist);
const pvKey = PrivateKey.fromSeed(account.mnemonic, this.currentNetwork);
const pbKey = pvKey.getPublicKey();
account.address = pbKey.getAddress();
account.publicKey = pbKey.toHex();
if (pbKey.network.wif) {
account.wif = pvKey.toWIF();
}
account.seed = bip39.mnemonicToSeedSync(account.mnemonic).toString('hex');
this.account = account;
}