UNPKG

@arklabs/wallet-sdk

Version:

Bitcoin wallet SDK with Taproot and Ark integration

37 lines (36 loc) 1.18 kB
import { pubSchnorr, randomPrivateKeyBytes } from "@scure/btc-signer/utils"; import { hex } from "@scure/base"; import { TreeSignerSession } from '../tree/signingSession.js'; const ZERO_32 = new Uint8Array(32).fill(0); export class InMemoryKey { constructor(key) { this.key = key || randomPrivateKeyBytes(); } static fromPrivateKey(privateKey) { return new InMemoryKey(privateKey); } static fromHex(privateKeyHex) { return new InMemoryKey(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 pubSchnorr(this.key); } signerSession() { return TreeSignerSession.random(); } }