@arklabs/wallet-sdk
Version:
Bitcoin wallet SDK with Taproot and Ark integration
52 lines (51 loc) • 1.98 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.VtxoScript = void 0;
exports.scriptFromTapLeafScript = scriptFromTapLeafScript;
const payment_1 = require("@scure/btc-signer/payment");
const utils_1 = require("@scure/btc-signer/utils");
const address_1 = require("./address");
const btc_signer_1 = require("@scure/btc-signer");
const base_1 = require("@scure/base");
function scriptFromTapLeafScript(leaf) {
return leaf[1].subarray(0, leaf[1].length - 1); // remove the version byte
}
class VtxoScript {
static decode(scripts) {
return new VtxoScript(scripts.map(base_1.hex.decode));
}
constructor(scripts) {
this.scripts = scripts;
const tapTree = (0, payment_1.taprootListToTree)(scripts.map((script) => ({ script, leafVersion: payment_1.TAP_LEAF_VERSION })));
const payment = (0, payment_1.p2tr)(utils_1.TAPROOT_UNSPENDABLE_KEY, tapTree, undefined, true);
if (!payment.tapLeafScript ||
payment.tapLeafScript.length !== scripts.length) {
throw new Error("invalid scripts");
}
this.leaves = payment.tapLeafScript;
this.tweakedPublicKey = payment.tweakedPubkey;
}
encode() {
return this.scripts.map(base_1.hex.encode);
}
address(prefix, serverPubKey) {
return new address_1.ArkAddress(serverPubKey, this.tweakedPublicKey, prefix);
}
get pkScript() {
return btc_signer_1.Script.encode(["OP_1", this.tweakedPublicKey]);
}
onchainAddress(network) {
return (0, payment_1.Address)(network).encode({
type: "tr",
pubkey: this.tweakedPublicKey,
});
}
findLeaf(scriptHex) {
const leaf = this.leaves.find((leaf) => base_1.hex.encode(scriptFromTapLeafScript(leaf)) === scriptHex);
if (!leaf) {
throw new Error(`leaf '${scriptHex}' not found`);
}
return leaf;
}
}
exports.VtxoScript = VtxoScript;