atomicals-js
Version:
Atomicals JavaScript SDK and CLI
133 lines (132 loc) • 6.17 kB
JavaScript
;
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.createNKeyPairs = exports.createPrimaryAndFundingImportedKeyPairs = exports.createKeyPair = exports.toXOnly = void 0;
const bitcoin = require('bitcoinjs-lib');
const ecc = __importStar(require("tiny-secp256k1"));
bitcoin.initEccLib(ecc);
const ecpair_1 = __importDefault(require("ecpair"));
const address_helpers_1 = require("./address-helpers");
const create_mnemonic_phrase_1 = require("./create-mnemonic-phrase");
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);
};
exports.toXOnly = toXOnly;
const bip39 = require('bip39');
const createKeyPair = (phrase = '', path = address_helpers_1.defaultDerivedPath, passphrase = '') => __awaiter(void 0, void 0, void 0, function* () {
var _a;
if (!phrase || phrase === '') {
const phraseResult = (0, create_mnemonic_phrase_1.createMnemonicPhrase)();
phrase = phraseResult.phrase;
}
const seed = yield bip39.mnemonicToSeed(phrase, passphrase);
const rootKey = bip32.fromSeed(seed);
const childNodePrimary = rootKey.derivePath(path);
// const p2pkh = bitcoin.payments.p2pkh({ pubkey: childNodePrimary.publicKey });
const childNodeXOnlyPubkeyPrimary = (0, exports.toXOnly)(childNodePrimary.publicKey);
const p2trPrimary = bitcoin.payments.p2tr({
internalPubkey: childNodeXOnlyPubkeyPrimary,
network: command_helpers_1.NETWORK
});
if (!p2trPrimary.address || !p2trPrimary.output) {
throw "error creating p2tr";
}
/* const p2pkhPrimary = bitcoin.payments.p2pkh({
pubkey: childNodePrimary.publicKey,
network: NETWORK
});
// console.log('p2pkhPrimary', p2pkhPrimary, p2pkhPrimary.address.toString())
*/
// Used for signing, since the output and address are using a tweaked key
// We must tweak the signer in the same way.
const tweakedChildNodePrimary = childNodePrimary.tweak(bitcoin.crypto.taggedHash('TapTweak', childNodeXOnlyPubkeyPrimary));
// Do a sanity check with the WIF serialized and then verify childNodePrimary is the same
const wif = childNodePrimary.toWIF();
const keypair = ECPair.fromWIF(wif);
if (childNodePrimary.publicKey.toString('hex') !== keypair.publicKey.toString('hex')) {
throw 'createKeyPair error child node not match sanity check';
}
return {
address: p2trPrimary.address,
publicKey: childNodePrimary.publicKey.toString('hex'),
publicKeyXOnly: childNodeXOnlyPubkeyPrimary.toString('hex'),
path,
WIF: childNodePrimary.toWIF(),
privateKey: (_a = childNodePrimary.privateKey) === null || _a === void 0 ? void 0 : _a.toString('hex'),
};
});
exports.createKeyPair = createKeyPair;
const createPrimaryAndFundingImportedKeyPairs = (phrase, path, passphrase, n) => __awaiter(void 0, void 0, void 0, function* () {
if (!phrase) {
phrase = (0, create_mnemonic_phrase_1.createMnemonicPhrase)().phrase;
}
let pathUsed = address_helpers_1.defaultDerivedPath.substring(0, 11);
if (path) {
pathUsed = path;
}
const imported = {};
if (n) {
for (let i = 2; i < n + 2; i++) {
imported[i + ''] = yield (0, exports.createKeyPair)(phrase, `${pathUsed}/0/` + i, passphrase);
}
}
return {
wallet: {
phrase,
passphrase,
primary: yield (0, exports.createKeyPair)(phrase, `${pathUsed}/0/0`, passphrase),
funding: yield (0, exports.createKeyPair)(phrase, `${pathUsed}/1/0`, passphrase)
},
imported
};
});
exports.createPrimaryAndFundingImportedKeyPairs = createPrimaryAndFundingImportedKeyPairs;
const createNKeyPairs = (phrase, passphrase, n = 1) => __awaiter(void 0, void 0, void 0, function* () {
const keypairs = [];
for (let i = 0; i < n; i++) {
keypairs.push(yield (0, exports.createKeyPair)(phrase, `${address_helpers_1.defaultDerivedPath.substring(0, 13)}/${i}`, passphrase));
}
return {
phrase,
keypairs,
};
});
exports.createNKeyPairs = createNKeyPairs;