@arklabs/wallet-sdk
Version:
Bitcoin wallet SDK with Taproot and Ark integration
41 lines (40 loc) • 1.34 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.InMemoryKey = void 0;
const utils_1 = require("@scure/btc-signer/utils");
const base_1 = require("@scure/base");
const signingSession_1 = require("../tree/signingSession");
const ZERO_32 = new Uint8Array(32).fill(0);
class InMemoryKey {
constructor(key) {
this.key = key || (0, utils_1.randomPrivateKeyBytes)();
}
static fromPrivateKey(privateKey) {
return new InMemoryKey(privateKey);
}
static fromHex(privateKeyHex) {
return new InMemoryKey(base_1.hex.decode(privateKeyHex));
}
async sign(tx, inputIndexes) {
const txCpy = tx.clone();
if (!inputIndexes) {
if (!txCpy.sign(this.key, undefined, ZERO_32)) {
throw new Error("Failed to sign transaction");
}
return txCpy;
}
for (const inputIndex of inputIndexes) {
if (!txCpy.signIdx(this.key, inputIndex, undefined, ZERO_32)) {
throw new Error(`Failed to sign input #${inputIndex}`);
}
}
return txCpy;
}
xOnlyPublicKey() {
return (0, utils_1.pubSchnorr)(this.key);
}
signerSession() {
return signingSession_1.TreeSignerSession.random();
}
}
exports.InMemoryKey = InMemoryKey;