@meshconnect/uwc-bitcoin 0.2.4-snapshot.ec6b95b → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,126 +0,0 @@
1
- import type {
2
- BitcoinAccount,
3
- BitcoinPsbtSignRequest,
4
- BitcoinSendTransferRequest,
5
- BitcoinSigHash,
6
- BitcoinSignMessageParams,
7
- SignatureType,
8
- TransactionResult
9
- } from '@meshconnect/uwc-types'
10
- import { WalletConnectorError } from '@meshconnect/uwc-types'
11
- import type { BitcoinTransportAdapter } from '../adapter'
12
-
13
- /**
14
- * Issues a WalletConnect JSON-RPC request for a bip122 method. The WalletConnect
15
- * transport binds this to its live `SignClient` + session topic + chainId, e.g.
16
- * `(method, params) => signClient.request({ topic, chainId, request: { method, params } })`.
17
- * Kept as a plain function so `uwc-bitcoin` needs no `@walletconnect/*` dependency.
18
- */
19
- export type BitcoinWalletConnectRequest = (
20
- method: string,
21
- params: Record<string, unknown>
22
- ) => Promise<unknown>
23
-
24
- /** Maps a SIGHASH flag to its numeric value (Bitcoin Core encoding). */
25
- const SIGHASH_VALUES: Record<BitcoinSigHash, number> = {
26
- ALL: 0x01,
27
- NONE: 0x02,
28
- SINGLE: 0x03,
29
- 'ALL|ANYONECANPAY': 0x81,
30
- 'NONE|ANYONECANPAY': 0x82,
31
- 'SINGLE|ANYONECANPAY': 0x83
32
- }
33
-
34
- interface WcAccountAddress {
35
- address: string
36
- publicKey?: string
37
- path?: string
38
- intention?: string
39
- }
40
-
41
- /**
42
- * WalletConnect (bip122) transport adapter — the Path A / mobile path (Trust
43
- * Wallet, …). The WalletConnect transport pairs the session and constructs this
44
- * adapter bound to the connected account's first external address plus a
45
- * `request` function; this adapter maps our domain shapes onto the Reown/CAIP-2
46
- * bip122 JSON-RPC methods (`getAccountAddresses`, `signMessage`, `signPsbt`,
47
- * `sendTransfer`).
48
- *
49
- * Wire format follows the Reown Bitcoin RPC reference (what wallets implement
50
- * over WalletConnect) — all-JSON strings, `account` on every request. NOTE:
51
- * untested end-to-end; grounded on the spec, not a live wallet.
52
- */
53
- export class WalletConnectBitcoinAdapter implements BitcoinTransportAdapter {
54
- /**
55
- * @param account The connected account's first external address — the
56
- * `account` parameter every bip122 method requires.
57
- * @param request Issues the bound WalletConnect JSON-RPC request.
58
- */
59
- constructor(
60
- private readonly account: string,
61
- private readonly request: BitcoinWalletConnectRequest
62
- ) {}
63
-
64
- async getAccounts(): Promise<BitcoinAccount[]> {
65
- const result = (await this.request('getAccountAddresses', {
66
- account: this.account
67
- })) as WcAccountAddress[]
68
- return result.map(entry => ({
69
- address: entry.address,
70
- // WalletConnect wallets MAY omit the public key; empty when absent.
71
- publicKey: entry.publicKey ?? '',
72
- ...(entry.intention === 'ordinal'
73
- ? { purpose: 'ordinals' as const }
74
- : entry.intention === 'payment'
75
- ? { purpose: 'payment' as const }
76
- : {})
77
- }))
78
- }
79
-
80
- async signMessage(params: BitcoinSignMessageParams): Promise<SignatureType> {
81
- const result = (await this.request('signMessage', {
82
- account: this.account,
83
- message: params.message,
84
- address: params.account.address,
85
- ...(params.protocol ? { protocol: params.protocol } : {})
86
- })) as { signature: string }
87
- return { type: 'standard', signature: result.signature }
88
- }
89
-
90
- async signPsbt(request: BitcoinPsbtSignRequest): Promise<string> {
91
- if (!request.inputsToSign?.length) {
92
- throw new WalletConnectorError({
93
- type: 'unknown',
94
- message:
95
- 'walletconnect bitcoin: signPsbt requires inputsToSign (the WC signPsbt method has no "sign all" mode)'
96
- })
97
- }
98
- const signInputs = request.inputsToSign.flatMap(input =>
99
- input.signingIndexes.map(index => ({
100
- address: input.address,
101
- index,
102
- ...(input.sigHash
103
- ? { sighashTypes: [SIGHASH_VALUES[input.sigHash]] }
104
- : {})
105
- }))
106
- )
107
- const result = (await this.request('signPsbt', {
108
- account: this.account,
109
- psbt: request.psbt,
110
- signInputs,
111
- broadcast: false
112
- })) as { psbt: string }
113
- return result.psbt
114
- }
115
-
116
- async sendTransfer(
117
- request: BitcoinSendTransferRequest
118
- ): Promise<TransactionResult> {
119
- const result = (await this.request('sendTransfer', {
120
- account: this.account,
121
- recipientAddress: request.to,
122
- amount: request.amount.toString()
123
- })) as { txid: string }
124
- return result.txid
125
- }
126
- }