@bigmi/core 0.0.1
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.
- package/CHANGELOG.md +11 -0
- package/LICENSE.md +21 -0
- package/README.md +137 -0
- package/dist/cjs/actions/getBalance.d.ts +7 -0
- package/dist/cjs/actions/getBalance.js +11 -0
- package/dist/cjs/actions/getBalance.js.map +1 -0
- package/dist/cjs/actions/getBlock.d.ts +12 -0
- package/dist/cjs/actions/getBlock.js +31 -0
- package/dist/cjs/actions/getBlock.js.map +1 -0
- package/dist/cjs/actions/getBlockCount.d.ts +4 -0
- package/dist/cjs/actions/getBlockCount.js +11 -0
- package/dist/cjs/actions/getBlockCount.js.map +1 -0
- package/dist/cjs/actions/getBlockStats.d.ts +14 -0
- package/dist/cjs/actions/getBlockStats.js +27 -0
- package/dist/cjs/actions/getBlockStats.js.map +1 -0
- package/dist/cjs/actions/getUTXOTransaction.d.ts +9 -0
- package/dist/cjs/actions/getUTXOTransaction.js +24 -0
- package/dist/cjs/actions/getUTXOTransaction.js.map +1 -0
- package/dist/cjs/actions/sendUTXOTransaction.d.ts +8 -0
- package/dist/cjs/actions/sendUTXOTransaction.js +15 -0
- package/dist/cjs/actions/sendUTXOTransaction.js.map +1 -0
- package/dist/cjs/actions/signPsbt.d.ts +3 -0
- package/dist/cjs/actions/signPsbt.js +11 -0
- package/dist/cjs/actions/signPsbt.js.map +1 -0
- package/dist/cjs/actions/waitForTransaction.d.ts +32 -0
- package/dist/cjs/actions/waitForTransaction.js +185 -0
- package/dist/cjs/actions/waitForTransaction.js.map +1 -0
- package/dist/cjs/actions/watchBlockNumber.d.ts +14 -0
- package/dist/cjs/actions/watchBlockNumber.js +45 -0
- package/dist/cjs/actions/watchBlockNumber.js.map +1 -0
- package/dist/cjs/chains/bitcoin.d.ts +36 -0
- package/dist/cjs/chains/bitcoin.js +21 -0
- package/dist/cjs/chains/bitcoin.js.map +1 -0
- package/dist/cjs/clients/types.d.ts +27 -0
- package/dist/cjs/clients/types.js +3 -0
- package/dist/cjs/clients/types.js.map +1 -0
- package/dist/cjs/decorators/UTXOAPIActions.d.ts +6 -0
- package/dist/cjs/decorators/UTXOAPIActions.js +10 -0
- package/dist/cjs/decorators/UTXOAPIActions.js.map +1 -0
- package/dist/cjs/decorators/UTXOActions.d.ts +10 -0
- package/dist/cjs/decorators/UTXOActions.js +14 -0
- package/dist/cjs/decorators/UTXOActions.js.map +1 -0
- package/dist/cjs/index.d.ts +38 -0
- package/dist/cjs/index.js +58 -0
- package/dist/cjs/index.js.map +1 -0
- package/dist/cjs/package.json +1 -0
- package/dist/cjs/transports/ankr.d.ts +2 -0
- package/dist/cjs/transports/ankr.js +21 -0
- package/dist/cjs/transports/ankr.js.map +1 -0
- package/dist/cjs/transports/blockchair.d.ts +2 -0
- package/dist/cjs/transports/blockchair.js +24 -0
- package/dist/cjs/transports/blockchair.js.map +1 -0
- package/dist/cjs/transports/blockcypher.d.ts +2 -0
- package/dist/cjs/transports/blockcypher.js +21 -0
- package/dist/cjs/transports/blockcypher.js.map +1 -0
- package/dist/cjs/transports/getHttpRpcClient.d.ts +19 -0
- package/dist/cjs/transports/getHttpRpcClient.js +77 -0
- package/dist/cjs/transports/getHttpRpcClient.js.map +1 -0
- package/dist/cjs/transports/getRpcProviderMethods.d.ts +2 -0
- package/dist/cjs/transports/getRpcProviderMethods.js +22 -0
- package/dist/cjs/transports/getRpcProviderMethods.js.map +1 -0
- package/dist/cjs/transports/mempool.d.ts +2 -0
- package/dist/cjs/transports/mempool.js +17 -0
- package/dist/cjs/transports/mempool.js.map +1 -0
- package/dist/cjs/transports/types.d.ts +60 -0
- package/dist/cjs/transports/types.js +3 -0
- package/dist/cjs/transports/types.js.map +1 -0
- package/dist/cjs/transports/utxo.d.ts +6 -0
- package/dist/cjs/transports/utxo.js +56 -0
- package/dist/cjs/transports/utxo.js.map +1 -0
- package/dist/cjs/types/blockStats.d.ts +34 -0
- package/dist/cjs/types/blockStats.js +3 -0
- package/dist/cjs/types/blockStats.js.map +1 -0
- package/dist/cjs/types/transaction.d.ts +41 -0
- package/dist/cjs/types/transaction.js +3 -0
- package/dist/cjs/types/transaction.js.map +1 -0
- package/dist/cjs/utils/cancelTransaction.d.ts +2 -0
- package/dist/cjs/utils/cancelTransaction.js +51 -0
- package/dist/cjs/utils/cancelTransaction.js.map +1 -0
- package/dist/cjs/utils/getUTXOAddress.d.ts +19 -0
- package/dist/cjs/utils/getUTXOAddress.js +120 -0
- package/dist/cjs/utils/getUTXOAddress.js.map +1 -0
- package/dist/cjs/utils/isUTXOAddress.d.ts +2 -0
- package/dist/cjs/utils/isUTXOAddress.js +18 -0
- package/dist/cjs/utils/isUTXOAddress.js.map +1 -0
- package/dist/cjs/utils/modifyFee.d.ts +2 -0
- package/dist/cjs/utils/modifyFee.js +50 -0
- package/dist/cjs/utils/modifyFee.js.map +1 -0
- package/dist/cjs/utils/observe.d.ts +11 -0
- package/dist/cjs/utils/observe.js +48 -0
- package/dist/cjs/utils/observe.js.map +1 -0
- package/dist/cjs/utils/poll.d.ts +9 -0
- package/dist/cjs/utils/poll.js +30 -0
- package/dist/cjs/utils/poll.js.map +1 -0
- package/dist/cjs/utils/sleep.d.ts +1 -0
- package/dist/cjs/utils/sleep.js +9 -0
- package/dist/cjs/utils/sleep.js.map +1 -0
- package/dist/esm/actions/getBalance.d.ts +8 -0
- package/dist/esm/actions/getBalance.js +8 -0
- package/dist/esm/actions/getBalance.js.map +1 -0
- package/dist/esm/actions/getBlock.d.ts +12 -0
- package/dist/esm/actions/getBlock.js +28 -0
- package/dist/esm/actions/getBlock.js.map +1 -0
- package/dist/esm/actions/getBlockCount.d.ts +4 -0
- package/dist/esm/actions/getBlockCount.js +8 -0
- package/dist/esm/actions/getBlockCount.js.map +1 -0
- package/dist/esm/actions/getBlockStats.d.ts +14 -0
- package/dist/esm/actions/getBlockStats.js +24 -0
- package/dist/esm/actions/getBlockStats.js.map +1 -0
- package/dist/esm/actions/getUTXOTransaction.d.ts +11 -0
- package/dist/esm/actions/getUTXOTransaction.js +21 -0
- package/dist/esm/actions/getUTXOTransaction.js.map +1 -0
- package/dist/esm/actions/sendUTXOTransaction.d.ts +10 -0
- package/dist/esm/actions/sendUTXOTransaction.js +12 -0
- package/dist/esm/actions/sendUTXOTransaction.js.map +1 -0
- package/dist/esm/actions/signPsbt.d.ts +3 -0
- package/dist/esm/actions/signPsbt.js +8 -0
- package/dist/esm/actions/signPsbt.js.map +1 -0
- package/dist/esm/actions/waitForTransaction.d.ts +75 -0
- package/dist/esm/actions/waitForTransaction.js +230 -0
- package/dist/esm/actions/waitForTransaction.js.map +1 -0
- package/dist/esm/actions/watchBlockNumber.d.ts +25 -0
- package/dist/esm/actions/watchBlockNumber.js +58 -0
- package/dist/esm/actions/watchBlockNumber.js.map +1 -0
- package/dist/esm/chains/bitcoin.d.ts +36 -0
- package/dist/esm/chains/bitcoin.js +18 -0
- package/dist/esm/chains/bitcoin.js.map +1 -0
- package/dist/esm/clients/types.d.ts +43 -0
- package/dist/esm/clients/types.js +2 -0
- package/dist/esm/clients/types.js.map +1 -0
- package/dist/esm/decorators/UTXOAPIActions.d.ts +6 -0
- package/dist/esm/decorators/UTXOAPIActions.js +7 -0
- package/dist/esm/decorators/UTXOAPIActions.js.map +1 -0
- package/dist/esm/decorators/UTXOActions.d.ts +10 -0
- package/dist/esm/decorators/UTXOActions.js +11 -0
- package/dist/esm/decorators/UTXOActions.js.map +1 -0
- package/dist/esm/index.d.ts +38 -0
- package/dist/esm/index.js +31 -0
- package/dist/esm/index.js.map +1 -0
- package/dist/esm/package.json +1 -0
- package/dist/esm/transports/ankr.d.ts +2 -0
- package/dist/esm/transports/ankr.js +18 -0
- package/dist/esm/transports/ankr.js.map +1 -0
- package/dist/esm/transports/blockchair.d.ts +2 -0
- package/dist/esm/transports/blockchair.js +21 -0
- package/dist/esm/transports/blockchair.js.map +1 -0
- package/dist/esm/transports/blockcypher.d.ts +2 -0
- package/dist/esm/transports/blockcypher.js +18 -0
- package/dist/esm/transports/blockcypher.js.map +1 -0
- package/dist/esm/transports/getHttpRpcClient.d.ts +24 -0
- package/dist/esm/transports/getHttpRpcClient.js +74 -0
- package/dist/esm/transports/getHttpRpcClient.js.map +1 -0
- package/dist/esm/transports/getRpcProviderMethods.d.ts +2 -0
- package/dist/esm/transports/getRpcProviderMethods.js +19 -0
- package/dist/esm/transports/getRpcProviderMethods.js.map +1 -0
- package/dist/esm/transports/mempool.d.ts +2 -0
- package/dist/esm/transports/mempool.js +14 -0
- package/dist/esm/transports/mempool.js.map +1 -0
- package/dist/esm/transports/types.d.ts +60 -0
- package/dist/esm/transports/types.js +2 -0
- package/dist/esm/transports/types.js.map +1 -0
- package/dist/esm/transports/utxo.d.ts +6 -0
- package/dist/esm/transports/utxo.js +53 -0
- package/dist/esm/transports/utxo.js.map +1 -0
- package/dist/esm/types/blockStats.d.ts +34 -0
- package/dist/esm/types/blockStats.js +2 -0
- package/dist/esm/types/blockStats.js.map +1 -0
- package/dist/esm/types/transaction.d.ts +41 -0
- package/dist/esm/types/transaction.js +2 -0
- package/dist/esm/types/transaction.js.map +1 -0
- package/dist/esm/utils/cancelTransaction.d.ts +2 -0
- package/dist/esm/utils/cancelTransaction.js +55 -0
- package/dist/esm/utils/cancelTransaction.js.map +1 -0
- package/dist/esm/utils/getUTXOAddress.d.ts +19 -0
- package/dist/esm/utils/getUTXOAddress.js +116 -0
- package/dist/esm/utils/getUTXOAddress.js.map +1 -0
- package/dist/esm/utils/isUTXOAddress.d.ts +2 -0
- package/dist/esm/utils/isUTXOAddress.js +14 -0
- package/dist/esm/utils/isUTXOAddress.js.map +1 -0
- package/dist/esm/utils/modifyFee.d.ts +2 -0
- package/dist/esm/utils/modifyFee.js +53 -0
- package/dist/esm/utils/modifyFee.js.map +1 -0
- package/dist/esm/utils/observe.d.ts +18 -0
- package/dist/esm/utils/observe.js +51 -0
- package/dist/esm/utils/observe.js.map +1 -0
- package/dist/esm/utils/poll.d.ts +12 -0
- package/dist/esm/utils/poll.js +30 -0
- package/dist/esm/utils/poll.js.map +1 -0
- package/dist/esm/utils/sleep.d.ts +1 -0
- package/dist/esm/utils/sleep.js +6 -0
- package/dist/esm/utils/sleep.js.map +1 -0
- package/dist/types/actions/getBalance.d.ts +9 -0
- package/dist/types/actions/getBalance.d.ts.map +1 -0
- package/dist/types/actions/getBlock.d.ts +13 -0
- package/dist/types/actions/getBlock.d.ts.map +1 -0
- package/dist/types/actions/getBlockCount.d.ts +5 -0
- package/dist/types/actions/getBlockCount.d.ts.map +1 -0
- package/dist/types/actions/getBlockStats.d.ts +15 -0
- package/dist/types/actions/getBlockStats.d.ts.map +1 -0
- package/dist/types/actions/getUTXOTransaction.d.ts +12 -0
- package/dist/types/actions/getUTXOTransaction.d.ts.map +1 -0
- package/dist/types/actions/sendUTXOTransaction.d.ts +11 -0
- package/dist/types/actions/sendUTXOTransaction.d.ts.map +1 -0
- package/dist/types/actions/signPsbt.d.ts +4 -0
- package/dist/types/actions/signPsbt.d.ts.map +1 -0
- package/dist/types/actions/waitForTransaction.d.ts +76 -0
- package/dist/types/actions/waitForTransaction.d.ts.map +1 -0
- package/dist/types/actions/watchBlockNumber.d.ts +26 -0
- package/dist/types/actions/watchBlockNumber.d.ts.map +1 -0
- package/dist/types/chains/bitcoin.d.ts +37 -0
- package/dist/types/chains/bitcoin.d.ts.map +1 -0
- package/dist/types/clients/types.d.ts +44 -0
- package/dist/types/clients/types.d.ts.map +1 -0
- package/dist/types/decorators/UTXOAPIActions.d.ts +7 -0
- package/dist/types/decorators/UTXOAPIActions.d.ts.map +1 -0
- package/dist/types/decorators/UTXOActions.d.ts +11 -0
- package/dist/types/decorators/UTXOActions.d.ts.map +1 -0
- package/dist/types/index.d.ts +39 -0
- package/dist/types/index.d.ts.map +1 -0
- package/dist/types/transports/ankr.d.ts +3 -0
- package/dist/types/transports/ankr.d.ts.map +1 -0
- package/dist/types/transports/blockchair.d.ts +3 -0
- package/dist/types/transports/blockchair.d.ts.map +1 -0
- package/dist/types/transports/blockcypher.d.ts +3 -0
- package/dist/types/transports/blockcypher.d.ts.map +1 -0
- package/dist/types/transports/getHttpRpcClient.d.ts +25 -0
- package/dist/types/transports/getHttpRpcClient.d.ts.map +1 -0
- package/dist/types/transports/getRpcProviderMethods.d.ts +3 -0
- package/dist/types/transports/getRpcProviderMethods.d.ts.map +1 -0
- package/dist/types/transports/mempool.d.ts +3 -0
- package/dist/types/transports/mempool.d.ts.map +1 -0
- package/dist/types/transports/types.d.ts +61 -0
- package/dist/types/transports/types.d.ts.map +1 -0
- package/dist/types/transports/utxo.d.ts +7 -0
- package/dist/types/transports/utxo.d.ts.map +1 -0
- package/dist/types/types/blockStats.d.ts +35 -0
- package/dist/types/types/blockStats.d.ts.map +1 -0
- package/dist/types/types/transaction.d.ts +42 -0
- package/dist/types/types/transaction.d.ts.map +1 -0
- package/dist/types/utils/cancelTransaction.d.ts +3 -0
- package/dist/types/utils/cancelTransaction.d.ts.map +1 -0
- package/dist/types/utils/getUTXOAddress.d.ts +20 -0
- package/dist/types/utils/getUTXOAddress.d.ts.map +1 -0
- package/dist/types/utils/isUTXOAddress.d.ts +3 -0
- package/dist/types/utils/isUTXOAddress.d.ts.map +1 -0
- package/dist/types/utils/modifyFee.d.ts +3 -0
- package/dist/types/utils/modifyFee.d.ts.map +1 -0
- package/dist/types/utils/observe.d.ts +19 -0
- package/dist/types/utils/observe.d.ts.map +1 -0
- package/dist/types/utils/poll.d.ts +13 -0
- package/dist/types/utils/poll.d.ts.map +1 -0
- package/dist/types/utils/sleep.d.ts +2 -0
- package/dist/types/utils/sleep.d.ts.map +1 -0
- package/package.json +50 -0
- package/src/actions/getBalance.ts +26 -0
- package/src/actions/getBlock.ts +58 -0
- package/src/actions/getBlockCount.ts +20 -0
- package/src/actions/getBlockStats.ts +55 -0
- package/src/actions/getUTXOTransaction.ts +43 -0
- package/src/actions/sendUTXOTransaction.ts +29 -0
- package/src/actions/signPsbt.ts +23 -0
- package/src/actions/waitForTransaction.ts +387 -0
- package/src/actions/watchBlockNumber.ts +105 -0
- package/src/chains/bitcoin.ts +18 -0
- package/src/clients/types.ts +48 -0
- package/src/decorators/UTXOAPIActions.ts +20 -0
- package/src/decorators/UTXOActions.ts +37 -0
- package/src/index.ts +97 -0
- package/src/transports/ankr.ts +30 -0
- package/src/transports/blockchair.ts +27 -0
- package/src/transports/blockcypher.ts +32 -0
- package/src/transports/getHttpRpcClient.ts +122 -0
- package/src/transports/getRpcProviderMethods.ts +22 -0
- package/src/transports/mempool.ts +34 -0
- package/src/transports/types.ts +71 -0
- package/src/transports/utxo.ts +84 -0
- package/src/types/blockStats.ts +35 -0
- package/src/types/transaction.ts +43 -0
- package/src/utils/cancelTransaction.ts +75 -0
- package/src/utils/getUTXOAddress.ts +148 -0
- package/src/utils/isUTXOAddress.ts +18 -0
- package/src/utils/modifyFee.ts +78 -0
- package/src/utils/observe.ts +81 -0
- package/src/utils/poll.ts +48 -0
- package/src/utils/sleep.ts +5 -0
- package/tsconfig.json +10 -0
|
@@ -0,0 +1,148 @@
|
|
|
1
|
+
import { sha256 } from '@noble/hashes/sha256'
|
|
2
|
+
import { type Decoded, bech32, bech32m } from 'bech32'
|
|
3
|
+
import bs58 from 'bs58'
|
|
4
|
+
|
|
5
|
+
export enum UTXONetwork {
|
|
6
|
+
Mainnet = 'mainnet',
|
|
7
|
+
Testnet = 'testnet',
|
|
8
|
+
Regtest = 'regtest',
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export enum UTXOAddressType {
|
|
12
|
+
p2pkh = 'p2pkh',
|
|
13
|
+
p2sh = 'p2sh',
|
|
14
|
+
p2wpkh = 'p2wpkh',
|
|
15
|
+
p2wsh = 'p2wsh',
|
|
16
|
+
p2tr = 'p2tr',
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export type UTXOAddress = {
|
|
20
|
+
bech32: boolean
|
|
21
|
+
network: UTXONetwork
|
|
22
|
+
address: string
|
|
23
|
+
type: UTXOAddressType
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
const addressTypes: {
|
|
27
|
+
[key: number]: { type: UTXOAddressType; network: UTXONetwork }
|
|
28
|
+
} = {
|
|
29
|
+
0: {
|
|
30
|
+
type: UTXOAddressType.p2pkh,
|
|
31
|
+
network: UTXONetwork.Mainnet,
|
|
32
|
+
},
|
|
33
|
+
111: {
|
|
34
|
+
type: UTXOAddressType.p2pkh,
|
|
35
|
+
network: UTXONetwork.Testnet,
|
|
36
|
+
},
|
|
37
|
+
5: {
|
|
38
|
+
type: UTXOAddressType.p2sh,
|
|
39
|
+
network: UTXONetwork.Mainnet,
|
|
40
|
+
},
|
|
41
|
+
196: {
|
|
42
|
+
type: UTXOAddressType.p2sh,
|
|
43
|
+
network: UTXONetwork.Testnet,
|
|
44
|
+
},
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
const parseBech32 = (address: string): UTXOAddress => {
|
|
48
|
+
let decoded: Decoded
|
|
49
|
+
|
|
50
|
+
try {
|
|
51
|
+
if (
|
|
52
|
+
address.startsWith('bc1p') ||
|
|
53
|
+
address.startsWith('tb1p') ||
|
|
54
|
+
address.startsWith('bcrt1p')
|
|
55
|
+
) {
|
|
56
|
+
decoded = bech32m.decode(address)
|
|
57
|
+
} else {
|
|
58
|
+
decoded = bech32.decode(address)
|
|
59
|
+
}
|
|
60
|
+
} catch (_error) {
|
|
61
|
+
throw new Error('Invalid address')
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
const mapPrefixToNetwork: { [key: string]: UTXONetwork } = {
|
|
65
|
+
bc: UTXONetwork.Mainnet,
|
|
66
|
+
tb: UTXONetwork.Testnet,
|
|
67
|
+
bcrt: UTXONetwork.Regtest,
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
const network: UTXONetwork = mapPrefixToNetwork[decoded.prefix]
|
|
71
|
+
|
|
72
|
+
if (network === undefined) {
|
|
73
|
+
throw new Error('Invalid address')
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
const witnessVersion = decoded.words[0]
|
|
77
|
+
|
|
78
|
+
if (witnessVersion < 0 || witnessVersion > 16) {
|
|
79
|
+
throw new Error('Invalid address')
|
|
80
|
+
}
|
|
81
|
+
const data = bech32.fromWords(decoded.words.slice(1))
|
|
82
|
+
|
|
83
|
+
let type: UTXOAddressType
|
|
84
|
+
if (data.length === 20) {
|
|
85
|
+
type = UTXOAddressType.p2wpkh
|
|
86
|
+
} else if (witnessVersion === 1) {
|
|
87
|
+
type = UTXOAddressType.p2tr
|
|
88
|
+
} else {
|
|
89
|
+
type = UTXOAddressType.p2wsh
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
return {
|
|
93
|
+
bech32: true,
|
|
94
|
+
network,
|
|
95
|
+
address,
|
|
96
|
+
type,
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
export const getUTXOAddress = (address: string): UTXOAddress => {
|
|
101
|
+
let decoded: Uint8Array
|
|
102
|
+
const prefix = address.substring(0, 2).toLowerCase()
|
|
103
|
+
|
|
104
|
+
if (prefix === 'bc' || prefix === 'tb') {
|
|
105
|
+
return parseBech32(address)
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
try {
|
|
109
|
+
decoded = bs58.decode(address)
|
|
110
|
+
} catch (_error) {
|
|
111
|
+
throw new Error('Invalid address')
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
const { length } = decoded
|
|
115
|
+
|
|
116
|
+
if (length !== 25) {
|
|
117
|
+
throw new Error('Invalid address')
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
const version = decoded[0]
|
|
121
|
+
|
|
122
|
+
const checksum = decoded.slice(length - 4, length)
|
|
123
|
+
const body = decoded.slice(0, length - 4)
|
|
124
|
+
|
|
125
|
+
const expectedChecksum = sha256(sha256(body)).slice(0, 4)
|
|
126
|
+
|
|
127
|
+
if (
|
|
128
|
+
checksum.some(
|
|
129
|
+
(value: number, index: number) => value !== expectedChecksum[index]
|
|
130
|
+
)
|
|
131
|
+
) {
|
|
132
|
+
throw new Error('Invalid address')
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
const validVersions = Object.keys(addressTypes).map(Number)
|
|
136
|
+
|
|
137
|
+
if (!validVersions.includes(version)) {
|
|
138
|
+
throw new Error('Invalid address')
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
const addressType = addressTypes[version]
|
|
142
|
+
|
|
143
|
+
return {
|
|
144
|
+
...addressType,
|
|
145
|
+
address,
|
|
146
|
+
bech32: false,
|
|
147
|
+
}
|
|
148
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { type UTXONetwork, getUTXOAddress } from './getUTXOAddress.js'
|
|
2
|
+
|
|
3
|
+
export const isUTXOAddress = (
|
|
4
|
+
address: string,
|
|
5
|
+
network?: UTXONetwork
|
|
6
|
+
): boolean => {
|
|
7
|
+
try {
|
|
8
|
+
const utxoAddress = getUTXOAddress(address)
|
|
9
|
+
|
|
10
|
+
if (network) {
|
|
11
|
+
return network === utxoAddress.network
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
return true
|
|
15
|
+
} catch (_error) {
|
|
16
|
+
return false
|
|
17
|
+
}
|
|
18
|
+
}
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
import { Psbt, address } from 'bitcoinjs-lib'
|
|
2
|
+
|
|
3
|
+
interface InputData {
|
|
4
|
+
hash: Uint8Array
|
|
5
|
+
index: number
|
|
6
|
+
nonWitnessUtxo?: Uint8Array
|
|
7
|
+
witnessUtxo?: {
|
|
8
|
+
script: Uint8Array
|
|
9
|
+
value: bigint
|
|
10
|
+
}
|
|
11
|
+
redeemScript?: Uint8Array
|
|
12
|
+
witnessScript?: Uint8Array
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export function modifyFee(psbt: Psbt, newFee: bigint, accountAddress: string) {
|
|
16
|
+
const newPsbt = new Psbt()
|
|
17
|
+
const inputs = psbt.data.inputs
|
|
18
|
+
const outputs = psbt.txOutputs
|
|
19
|
+
|
|
20
|
+
// Add inputs to the new PSBT
|
|
21
|
+
for (let i = 0; i < inputs.length; i++) {
|
|
22
|
+
const input = inputs[i]
|
|
23
|
+
const inputData: InputData = {
|
|
24
|
+
hash: psbt.txInputs[i].hash,
|
|
25
|
+
index: psbt.txInputs[i].index,
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
// Include UTXO information
|
|
29
|
+
if (input.nonWitnessUtxo) {
|
|
30
|
+
inputData.nonWitnessUtxo = input.nonWitnessUtxo
|
|
31
|
+
} else if (input.witnessUtxo) {
|
|
32
|
+
inputData.witnessUtxo = input.witnessUtxo
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
// Include scripts if necessary
|
|
36
|
+
if (input.redeemScript) {
|
|
37
|
+
inputData.redeemScript = input.redeemScript
|
|
38
|
+
}
|
|
39
|
+
if (input.witnessScript) {
|
|
40
|
+
inputData.witnessScript = input.witnessScript
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
newPsbt.addInput(inputData)
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
const changeOutputScript = address.toOutputScript(accountAddress)
|
|
47
|
+
|
|
48
|
+
// Add outputs to the new PSBT
|
|
49
|
+
for (const output of outputs) {
|
|
50
|
+
const outputData = {
|
|
51
|
+
script: output.script,
|
|
52
|
+
value: output.value,
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
const scriptsAreEqual =
|
|
56
|
+
output.script.length === changeOutputScript.length &&
|
|
57
|
+
output.script.every((value, index) => value === changeOutputScript[index])
|
|
58
|
+
|
|
59
|
+
if (scriptsAreEqual) {
|
|
60
|
+
outputData.value = output.value - newFee
|
|
61
|
+
if (outputData.value < 0) {
|
|
62
|
+
throw new Error(
|
|
63
|
+
'Insufficient funds to adjust the fee by the specified amount.'
|
|
64
|
+
)
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
newPsbt.addOutput(outputData)
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
// Modify the input sequence number to enable RBF
|
|
72
|
+
newPsbt.txInputs.forEach((_, index) => {
|
|
73
|
+
// Set sequence number to less than 0xfffffffe, e.g., 0xfffffffd
|
|
74
|
+
newPsbt.setInputSequence(index, 0xfffffffd)
|
|
75
|
+
})
|
|
76
|
+
|
|
77
|
+
return newPsbt
|
|
78
|
+
}
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
import type { MaybePromise } from 'viem'
|
|
2
|
+
|
|
3
|
+
type Callback = ((...args: any[]) => any) | undefined
|
|
4
|
+
type Callbacks = Record<string, Callback>
|
|
5
|
+
|
|
6
|
+
/** @internal */
|
|
7
|
+
export const listenersCache = /*#__PURE__*/ new Map<
|
|
8
|
+
string,
|
|
9
|
+
{ id: number; fns: Callbacks }[]
|
|
10
|
+
>()
|
|
11
|
+
/** @internal */
|
|
12
|
+
export const cleanupCache = /*#__PURE__*/ new Map<string, () => void>()
|
|
13
|
+
|
|
14
|
+
type EmitFunction<callbacks extends Callbacks> = (
|
|
15
|
+
emit: callbacks
|
|
16
|
+
) => MaybePromise<void | (() => void)>
|
|
17
|
+
|
|
18
|
+
let callbackCount = 0
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* @description Sets up an observer for a given function. If another function
|
|
22
|
+
* is set up under the same observer id, the function will only be called once
|
|
23
|
+
* for both instances of the observer.
|
|
24
|
+
*/
|
|
25
|
+
export function observe<callbacks extends Callbacks>(
|
|
26
|
+
observerId: string,
|
|
27
|
+
callbacks: callbacks,
|
|
28
|
+
fn: EmitFunction<callbacks>
|
|
29
|
+
) {
|
|
30
|
+
const callbackId = ++callbackCount
|
|
31
|
+
|
|
32
|
+
const getListeners = () => listenersCache.get(observerId) || []
|
|
33
|
+
|
|
34
|
+
const unsubscribe = () => {
|
|
35
|
+
const listeners = getListeners()
|
|
36
|
+
listenersCache.set(
|
|
37
|
+
observerId,
|
|
38
|
+
listeners.filter((cb: any) => cb.id !== callbackId)
|
|
39
|
+
)
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
const unwatch = () => {
|
|
43
|
+
const cleanup = cleanupCache.get(observerId)
|
|
44
|
+
if (getListeners().length === 1 && cleanup) {
|
|
45
|
+
cleanup()
|
|
46
|
+
}
|
|
47
|
+
unsubscribe()
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
const listeners = getListeners()
|
|
51
|
+
listenersCache.set(observerId, [
|
|
52
|
+
...listeners,
|
|
53
|
+
{ id: callbackId, fns: callbacks },
|
|
54
|
+
])
|
|
55
|
+
|
|
56
|
+
if (listeners && listeners.length > 0) {
|
|
57
|
+
return unwatch
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
const emit: callbacks = {} as callbacks
|
|
61
|
+
for (const key in callbacks) {
|
|
62
|
+
emit[key] = ((
|
|
63
|
+
...args: Parameters<NonNullable<callbacks[keyof callbacks]>>
|
|
64
|
+
) => {
|
|
65
|
+
const listeners = getListeners()
|
|
66
|
+
if (listeners.length === 0) {
|
|
67
|
+
return
|
|
68
|
+
}
|
|
69
|
+
for (const listener of listeners) {
|
|
70
|
+
listener.fns[key]?.(...args)
|
|
71
|
+
}
|
|
72
|
+
}) as callbacks[Extract<keyof callbacks, string>]
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
const cleanup = fn(emit)
|
|
76
|
+
if (typeof cleanup === 'function') {
|
|
77
|
+
cleanupCache.set(observerId, cleanup)
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
return unwatch
|
|
81
|
+
}
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { sleep } from './sleep.js'
|
|
2
|
+
|
|
3
|
+
type PollOptions<T> = {
|
|
4
|
+
// Whether or not to emit when the polling starts.
|
|
5
|
+
emitOnBegin?: boolean | undefined
|
|
6
|
+
// The initial wait time (in ms) before polling.
|
|
7
|
+
initialWaitTime?: ((data: T | undefined) => Promise<number>) | undefined
|
|
8
|
+
// The interval (in ms).
|
|
9
|
+
interval: number
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* @description Polls a function at a specified interval.
|
|
14
|
+
*/
|
|
15
|
+
export function poll<T>(
|
|
16
|
+
fn: ({ unpoll }: { unpoll: () => void }) => Promise<T | undefined>,
|
|
17
|
+
{ emitOnBegin, initialWaitTime, interval }: PollOptions<T>
|
|
18
|
+
) {
|
|
19
|
+
let active = true
|
|
20
|
+
|
|
21
|
+
const unwatch = () => {
|
|
22
|
+
active = false
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
const watch = async () => {
|
|
26
|
+
let data: T | undefined = undefined
|
|
27
|
+
if (emitOnBegin) {
|
|
28
|
+
data = await fn({ unpoll: unwatch })
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
const initialWait = (await initialWaitTime?.(data)) ?? interval
|
|
32
|
+
await sleep(initialWait)
|
|
33
|
+
|
|
34
|
+
const poll = async () => {
|
|
35
|
+
if (!active) {
|
|
36
|
+
return
|
|
37
|
+
}
|
|
38
|
+
await fn({ unpoll: unwatch })
|
|
39
|
+
await sleep(interval)
|
|
40
|
+
poll()
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
poll()
|
|
44
|
+
}
|
|
45
|
+
watch()
|
|
46
|
+
|
|
47
|
+
return unwatch
|
|
48
|
+
}
|