-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathPsbt.ts
More file actions
146 lines (113 loc) · 3.82 KB
/
Psbt.ts
File metadata and controls
146 lines (113 loc) · 3.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
import {
WrapPsbt as WasmPsbt,
type WasmBIP32,
type WasmECPair,
type WrapDescriptor,
type PsbtOutputDataWithAddress,
} from "../wasm/wasm_utxo.js";
import type { IPsbt } from "../psbt.js";
import type { CoinName } from "../coinName.js";
import { Transaction } from "../transaction.js";
import { PsbtBase } from "../psbtBase.js";
export type SignPsbtResult = {
[inputIndex: number]: [pubkey: string][];
};
export class Psbt extends PsbtBase<WasmPsbt> implements IPsbt {
constructor(versionOrWasm?: number | WasmPsbt, lockTime?: number) {
super(
versionOrWasm instanceof WasmPsbt ? versionOrWasm : new WasmPsbt(versionOrWasm, lockTime),
);
}
/** @internal Access the underlying WASM instance */
get wasm(): WasmPsbt {
return this._wasm;
}
// -- Static / Factory --
static create(version?: number, lockTime?: number): Psbt {
return new Psbt(new WasmPsbt(version, lockTime));
}
static deserialize(bytes: Uint8Array): Psbt {
return new Psbt(WasmPsbt.deserialize(bytes));
}
// -- Serialization --
clone(): Psbt {
return new Psbt(this._wasm.clone());
}
// -- IPsbt: introspection --
getOutputsWithAddress(coin: CoinName): PsbtOutputDataWithAddress[] {
return this._wasm.get_outputs_with_address(coin) as PsbtOutputDataWithAddress[];
}
// -- IPsbt: mutation --
addInputAtIndex(
index: number,
txid: string,
vout: number,
value: bigint,
script: Uint8Array,
sequence?: number,
): number {
return this._wasm.add_input_at_index(index, txid, vout, value, script, sequence);
}
addInput(
txid: string,
vout: number,
value: bigint,
script: Uint8Array,
sequence?: number,
): number {
return this._wasm.add_input(txid, vout, value, script, sequence);
}
addOutputAtIndex(index: number, script: Uint8Array, value: bigint): number {
return this._wasm.add_output_at_index(index, script, value);
}
addOutput(script: Uint8Array, value: bigint): number {
return this._wasm.add_output(script, value);
}
// -- Descriptor updates --
updateInputWithDescriptor(inputIndex: number, descriptor: WrapDescriptor): void {
this._wasm.update_input_with_descriptor(inputIndex, descriptor);
}
updateOutputWithDescriptor(outputIndex: number, descriptor: WrapDescriptor): void {
this._wasm.update_output_with_descriptor(outputIndex, descriptor);
}
// -- Signing --
signWithXprv(xprv: string): SignPsbtResult {
return this._wasm.sign_with_xprv(xprv) as unknown as SignPsbtResult;
}
signWithPrv(prv: Uint8Array): SignPsbtResult {
return this._wasm.sign_with_prv(prv) as unknown as SignPsbtResult;
}
signAll(key: WasmBIP32): SignPsbtResult {
return this._wasm.sign_all(key) as unknown as SignPsbtResult;
}
signAllWithEcpair(key: WasmECPair): SignPsbtResult {
return this._wasm.sign_all_with_ecpair(key) as unknown as SignPsbtResult;
}
// -- Signature introspection --
getPartialSignatures(inputIndex: number): Array<{ pubkey: Uint8Array; signature: Uint8Array }> {
return this._wasm.get_partial_signatures(inputIndex) as Array<{
pubkey: Uint8Array;
signature: Uint8Array;
}>;
}
hasPartialSignatures(inputIndex: number): boolean {
return this._wasm.has_partial_signatures(inputIndex);
}
// -- Validation --
validateSignatureAtInput(inputIndex: number, pubkey: Uint8Array): boolean {
return this._wasm.validate_signature_at_input(inputIndex, pubkey);
}
verifySignatureWithKey(inputIndex: number, key: WasmBIP32): boolean {
return this._wasm.verify_signature_with_key(inputIndex, key);
}
// -- Transaction extraction --
getUnsignedTx(): Uint8Array {
return this._wasm.get_unsigned_tx();
}
finalize(): void {
this._wasm.finalize_mut();
}
extractTransaction(): Transaction {
return Transaction.fromWasm(this._wasm.extract_transaction());
}
}