|
| 1 | +import { |
| 2 | + WrapPsbt as WasmPsbt, |
| 3 | + type WasmBIP32, |
| 4 | + type WasmECPair, |
| 5 | + type WrapDescriptor, |
| 6 | + type PsbtInputData, |
| 7 | + type PsbtOutputData, |
| 8 | + type PsbtOutputDataWithAddress, |
| 9 | +} from "../wasm/wasm_utxo.js"; |
| 10 | +import type { IPsbt } from "../psbt.js"; |
| 11 | +import type { CoinName } from "../coinName.js"; |
| 12 | +import type { BIP32 } from "../bip32.js"; |
| 13 | +import { Transaction } from "../transaction.js"; |
| 14 | + |
| 15 | +export type SignPsbtResult = { |
| 16 | + [inputIndex: number]: [pubkey: string][]; |
| 17 | +}; |
| 18 | + |
| 19 | +export class Psbt implements IPsbt { |
| 20 | + private _wasm: WasmPsbt; |
| 21 | + |
| 22 | + constructor(versionOrWasm?: number | WasmPsbt, lockTime?: number) { |
| 23 | + if (versionOrWasm instanceof WasmPsbt) { |
| 24 | + this._wasm = versionOrWasm; |
| 25 | + } else { |
| 26 | + this._wasm = new WasmPsbt(versionOrWasm, lockTime); |
| 27 | + } |
| 28 | + } |
| 29 | + |
| 30 | + /** @internal Access the underlying WASM instance */ |
| 31 | + get wasm(): WasmPsbt { |
| 32 | + return this._wasm; |
| 33 | + } |
| 34 | + |
| 35 | + // -- Static / Factory -- |
| 36 | + |
| 37 | + static create(version?: number, lockTime?: number): Psbt { |
| 38 | + return new Psbt(new WasmPsbt(version, lockTime)); |
| 39 | + } |
| 40 | + |
| 41 | + static deserialize(bytes: Uint8Array): Psbt { |
| 42 | + return new Psbt(WasmPsbt.deserialize(bytes)); |
| 43 | + } |
| 44 | + |
| 45 | + // -- Serialization -- |
| 46 | + |
| 47 | + serialize(): Uint8Array { |
| 48 | + return this._wasm.serialize(); |
| 49 | + } |
| 50 | + |
| 51 | + clone(): Psbt { |
| 52 | + return new Psbt(this._wasm.clone()); |
| 53 | + } |
| 54 | + |
| 55 | + // -- IPsbt: introspection -- |
| 56 | + |
| 57 | + inputCount(): number { |
| 58 | + return this._wasm.input_count(); |
| 59 | + } |
| 60 | + |
| 61 | + outputCount(): number { |
| 62 | + return this._wasm.output_count(); |
| 63 | + } |
| 64 | + |
| 65 | + version(): number { |
| 66 | + return this._wasm.version(); |
| 67 | + } |
| 68 | + |
| 69 | + lockTime(): number { |
| 70 | + return this._wasm.lock_time(); |
| 71 | + } |
| 72 | + |
| 73 | + unsignedTxId(): string { |
| 74 | + return this._wasm.unsigned_tx_id(); |
| 75 | + } |
| 76 | + |
| 77 | + getInputs(): PsbtInputData[] { |
| 78 | + return this._wasm.get_inputs() as PsbtInputData[]; |
| 79 | + } |
| 80 | + |
| 81 | + getOutputs(): PsbtOutputData[] { |
| 82 | + return this._wasm.get_outputs() as PsbtOutputData[]; |
| 83 | + } |
| 84 | + |
| 85 | + getGlobalXpubs(): BIP32[] { |
| 86 | + return this._wasm.get_global_xpubs() as BIP32[]; |
| 87 | + } |
| 88 | + |
| 89 | + getOutputsWithAddress(coin: CoinName): PsbtOutputDataWithAddress[] { |
| 90 | + return this._wasm.get_outputs_with_address(coin) as PsbtOutputDataWithAddress[]; |
| 91 | + } |
| 92 | + |
| 93 | + // -- IPsbt: mutation -- |
| 94 | + |
| 95 | + addInputAtIndex( |
| 96 | + index: number, |
| 97 | + txid: string, |
| 98 | + vout: number, |
| 99 | + value: bigint, |
| 100 | + script: Uint8Array, |
| 101 | + sequence?: number, |
| 102 | + ): number { |
| 103 | + return this._wasm.add_input_at_index(index, txid, vout, value, script, sequence); |
| 104 | + } |
| 105 | + |
| 106 | + addInput( |
| 107 | + txid: string, |
| 108 | + vout: number, |
| 109 | + value: bigint, |
| 110 | + script: Uint8Array, |
| 111 | + sequence?: number, |
| 112 | + ): number { |
| 113 | + return this._wasm.add_input(txid, vout, value, script, sequence); |
| 114 | + } |
| 115 | + |
| 116 | + addOutputAtIndex(index: number, script: Uint8Array, value: bigint): number { |
| 117 | + return this._wasm.add_output_at_index(index, script, value); |
| 118 | + } |
| 119 | + |
| 120 | + addOutput(script: Uint8Array, value: bigint): number { |
| 121 | + return this._wasm.add_output(script, value); |
| 122 | + } |
| 123 | + |
| 124 | + removeInput(index: number): void { |
| 125 | + this._wasm.remove_input(index); |
| 126 | + } |
| 127 | + |
| 128 | + removeOutput(index: number): void { |
| 129 | + this._wasm.remove_output(index); |
| 130 | + } |
| 131 | + |
| 132 | + // -- Descriptor updates -- |
| 133 | + |
| 134 | + updateInputWithDescriptor(inputIndex: number, descriptor: WrapDescriptor): void { |
| 135 | + this._wasm.update_input_with_descriptor(inputIndex, descriptor); |
| 136 | + } |
| 137 | + |
| 138 | + updateOutputWithDescriptor(outputIndex: number, descriptor: WrapDescriptor): void { |
| 139 | + this._wasm.update_output_with_descriptor(outputIndex, descriptor); |
| 140 | + } |
| 141 | + |
| 142 | + // -- Signing -- |
| 143 | + |
| 144 | + signWithXprv(xprv: string): SignPsbtResult { |
| 145 | + return this._wasm.sign_with_xprv(xprv) as unknown as SignPsbtResult; |
| 146 | + } |
| 147 | + |
| 148 | + signWithPrv(prv: Uint8Array): SignPsbtResult { |
| 149 | + return this._wasm.sign_with_prv(prv) as unknown as SignPsbtResult; |
| 150 | + } |
| 151 | + |
| 152 | + signAll(key: WasmBIP32): SignPsbtResult { |
| 153 | + return this._wasm.sign_all(key) as unknown as SignPsbtResult; |
| 154 | + } |
| 155 | + |
| 156 | + signAllWithEcpair(key: WasmECPair): SignPsbtResult { |
| 157 | + return this._wasm.sign_all_with_ecpair(key) as unknown as SignPsbtResult; |
| 158 | + } |
| 159 | + |
| 160 | + // -- Signature introspection -- |
| 161 | + |
| 162 | + getPartialSignatures(inputIndex: number): Array<{ pubkey: Uint8Array; signature: Uint8Array }> { |
| 163 | + return this._wasm.get_partial_signatures(inputIndex) as Array<{ |
| 164 | + pubkey: Uint8Array; |
| 165 | + signature: Uint8Array; |
| 166 | + }>; |
| 167 | + } |
| 168 | + |
| 169 | + hasPartialSignatures(inputIndex: number): boolean { |
| 170 | + return this._wasm.has_partial_signatures(inputIndex); |
| 171 | + } |
| 172 | + |
| 173 | + // -- Validation -- |
| 174 | + |
| 175 | + validateSignatureAtInput(inputIndex: number, pubkey: Uint8Array): boolean { |
| 176 | + return this._wasm.validate_signature_at_input(inputIndex, pubkey); |
| 177 | + } |
| 178 | + |
| 179 | + verifySignatureWithKey(inputIndex: number, key: WasmBIP32): boolean { |
| 180 | + return this._wasm.verify_signature_with_key(inputIndex, key); |
| 181 | + } |
| 182 | + |
| 183 | + // -- Transaction extraction -- |
| 184 | + |
| 185 | + getUnsignedTx(): Uint8Array { |
| 186 | + return this._wasm.get_unsigned_tx(); |
| 187 | + } |
| 188 | + |
| 189 | + finalize(): void { |
| 190 | + this._wasm.finalize_mut(); |
| 191 | + } |
| 192 | + |
| 193 | + extractTransaction(): Transaction { |
| 194 | + return Transaction.fromWasm(this._wasm.extract_transaction()); |
| 195 | + } |
| 196 | +} |
0 commit comments