UNPKG

@terra-money/core

Version:

This package provides Terra Blockchain client side APIs to support building transaction, singing, or querying chain data.

80 lines 3.02 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const HEX = require("crypto-js/enc-hex"); const RIPEMD160 = require("crypto-js/ripemd160"); const SHA256 = require("crypto-js/sha256"); const bip32 = require("bip32"); const bip39 = require("bip39"); const bech32 = require("bech32"); const secp256k1 = require("secp256k1"); const accPrefix = 'terra'; const valPrefix = 'terravaloper'; async function deriveMasterKey(mnemonic) { // throws if mnemonic is invalid if (!bip39.validateMnemonic(mnemonic)) { throw new Error('invalid mnemonic'); } const seed = await bip39.mnemonicToSeed(mnemonic); return bip32.fromSeed(seed); } exports.deriveMasterKey = deriveMasterKey; function deriveMasterKeySync(mnemonic) { // throws if mnemonic is invalid if (!bip39.validateMnemonic(mnemonic)) { throw new Error('invalid mnemonic'); } const seed = bip39.mnemonicToSeedSync(mnemonic); return bip32.fromSeed(seed); } exports.deriveMasterKeySync = deriveMasterKeySync; function deriveKeypair(masterKey, account = 0, index = 0) { const hdPathLuna = `m/44'/330'/${account}'/0/${index}`; const terraHD = masterKey.derivePath(hdPathLuna); const privateKey = terraHD.privateKey; if (!privateKey) { throw new Error('Failed to derive key pair'); } const publicKey = Buffer.from(secp256k1.publicKeyCreate(privateKey, true)); return { privateKey, publicKey }; } exports.deriveKeypair = deriveKeypair; // NOTE: this only works with a compressed public key (33 bytes) function getAddress(publicKey) { if (typeof publicKey !== 'object' || publicKey.constructor !== Buffer) { throw TypeError('parameter must be Buffer that contains public key'); } const message = HEX.parse(publicKey.toString('hex')); const hash = RIPEMD160(SHA256(message)).toString(); const address = Buffer.from(hash, 'hex'); return Buffer.from(bech32.toWords(address)); } // NOTE: this only works with a compressed public key (33 bytes) function getAccAddress(publicKey) { const words = getAddress(publicKey); return bech32.encode(accPrefix, words); } exports.getAccAddress = getAccAddress; // NOTE: this only works with a compressed public key (33 bytes) function getValAddress(publicKey) { const words = getAddress(publicKey); return bech32.encode(valPrefix, words); } exports.getValAddress = getValAddress; function convertValAddressToAccAddress(address) { const { words } = bech32.decode(address); return bech32.encode(accPrefix, words); } exports.convertValAddressToAccAddress = convertValAddressToAccAddress; function convertAccAddressToValAddress(address) { const { words } = bech32.decode(address); return bech32.encode(valPrefix, words); } exports.convertAccAddressToValAddress = convertAccAddressToValAddress; function generateMnemonic() { return bip39.generateMnemonic(256); } exports.generateMnemonic = generateMnemonic; //# sourceMappingURL=keyUtils.js.map