|
1 | 1 | import { FixedScriptWalletNamespace } from "./wasm/wasm_utxo"; |
2 | | -import type { UtxolibNetwork, UtxolibRootWalletKeys } from "./utxolibCompat"; |
| 2 | +import type { UtxolibName, UtxolibNetwork, UtxolibRootWalletKeys } from "./utxolibCompat"; |
| 3 | +import type { CoinName } from "./coinName"; |
3 | 4 | import { Triple } from "./triple"; |
4 | 5 | import { AddressFormat } from "./address"; |
5 | 6 |
|
| 7 | +export type NetworkName = UtxolibName | CoinName; |
| 8 | + |
6 | 9 | export type WalletKeys = |
7 | 10 | /** Just an xpub triple, will assume default derivation prefixes */ |
8 | 11 | | Triple<string> |
@@ -42,3 +45,65 @@ export function address( |
42 | 45 | ): string { |
43 | 46 | return FixedScriptWalletNamespace.address(keys, chain, index, network, addressFormat); |
44 | 47 | } |
| 48 | + |
| 49 | +type ReplayProtection = |
| 50 | + | { |
| 51 | + outputScripts: Uint8Array[]; |
| 52 | + } |
| 53 | + | { |
| 54 | + addresses: string[]; |
| 55 | + }; |
| 56 | + |
| 57 | +export type ScriptId = { chain: number; index: number }; |
| 58 | + |
| 59 | +export type ParsedInput = { |
| 60 | + address?: string; |
| 61 | + script: Uint8Array; |
| 62 | + value: bigint; |
| 63 | + scriptId: ScriptId | undefined; |
| 64 | +}; |
| 65 | + |
| 66 | +export type ParsedOutput = { |
| 67 | + address?: string; |
| 68 | + script: Uint8Array; |
| 69 | + value: bigint; |
| 70 | + scriptId?: ScriptId; |
| 71 | +}; |
| 72 | + |
| 73 | +export type ParsedTransaction = { |
| 74 | + inputs: ParsedInput[]; |
| 75 | + outputs: ParsedOutput[]; |
| 76 | + spendAmount: bigint; |
| 77 | + minerFee: bigint; |
| 78 | + virtualSize: number; |
| 79 | +}; |
| 80 | + |
| 81 | +import { BitGoPsbt as WasmBitGoPsbt } from "./wasm/wasm_utxo"; |
| 82 | + |
| 83 | +export class BitGoPsbt { |
| 84 | + private constructor(private wasm: WasmBitGoPsbt) {} |
| 85 | + |
| 86 | + /** |
| 87 | + * Deserialize a PSBT from bytes |
| 88 | + * @param bytes - The PSBT bytes |
| 89 | + * @param network - The network to use for deserialization (either utxolib name like "bitcoin" or coin name like "btc") |
| 90 | + * @returns A BitGoPsbt instance |
| 91 | + */ |
| 92 | + static fromBytes(bytes: Uint8Array, network: NetworkName): BitGoPsbt { |
| 93 | + const wasm = WasmBitGoPsbt.fromBytes(bytes, network); |
| 94 | + return new BitGoPsbt(wasm); |
| 95 | + } |
| 96 | + |
| 97 | + /** |
| 98 | + * Parse transaction with wallet keys to identify wallet inputs/outputs |
| 99 | + * @param walletKeys - The wallet keys to use for identification |
| 100 | + * @param replayProtection - Scripts that are allowed as inputs without wallet validation |
| 101 | + * @returns Parsed transaction information |
| 102 | + */ |
| 103 | + parseTransactionWithWalletKeys( |
| 104 | + walletKeys: WalletKeys, |
| 105 | + replayProtection: ReplayProtection, |
| 106 | + ): ParsedTransaction { |
| 107 | + return this.wasm.parseTransactionWithWalletKeys(walletKeys, replayProtection); |
| 108 | + } |
| 109 | +} |
0 commit comments