UNPKG

atomicals-js

Version:
82 lines (81 loc) 3.82 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.decodeMnemonicPhrase = void 0; const bitcoin = require('bitcoinjs-lib'); const ecpair_1 = __importDefault(require("ecpair")); const ecc = __importStar(require("tiny-secp256k1")); bitcoin.initEccLib(ecc); const ECPair = (0, ecpair_1.default)(ecc); const bip32_1 = __importDefault(require("bip32")); const command_helpers_1 = require("../commands/command-helpers"); const bip32 = (0, bip32_1.default)(ecc); const toXOnly = (publicKey) => { return publicKey.slice(1, 33); }; const bip39 = require('bip39'); const decodeMnemonicPhrase = (phrase, path, passphrase) => __awaiter(void 0, void 0, void 0, function* () { var _a; if (!bip39.validateMnemonic(phrase)) { throw new Error("Invalid mnemonic phrase provided!"); } const seed = yield bip39.mnemonicToSeed(phrase, passphrase); const rootKey = bip32.fromSeed(seed); const childNode = rootKey.derivePath(path); // const { address } = bitcoin.payments.p2pkh({ pubkey: childNode.publicKey }); const childNodeXOnlyPubkey = toXOnly(childNode.publicKey); const p2tr = bitcoin.payments.p2tr({ internalPubkey: childNodeXOnlyPubkey, network: command_helpers_1.NETWORK }); if (!p2tr.address || !p2tr.output) { throw "error creating p2tr"; } // Used for signing, since the output and address are using a tweaked key // We must tweak the signer in the same way. const tweakedChildNode = childNode.tweak(bitcoin.crypto.taggedHash('TapTweak', childNodeXOnlyPubkey)); return { phrase, address: p2tr.address, publicKey: childNode.publicKey.toString('hex'), publicKeyXOnly: childNodeXOnlyPubkey.toString('hex'), path, WIF: childNode.toWIF(), privateKey: (_a = childNode.privateKey) === null || _a === void 0 ? void 0 : _a.toString('hex'), }; }); exports.decodeMnemonicPhrase = decodeMnemonicPhrase;