@arkade-os/sdk 0.4.15 → 0.4.16
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/README.md +102 -96
- package/dist/cjs/arkfee/estimator.js +1 -1
- package/dist/cjs/arkfee/types.js +2 -1
- package/dist/cjs/arknote/index.js +43 -4
- package/dist/cjs/bip322/index.js +1 -1
- package/dist/cjs/contracts/arkcontract.js +1 -1
- package/dist/cjs/contracts/contractManager.js +40 -24
- package/dist/cjs/contracts/contractWatcher.js +29 -22
- package/dist/cjs/contracts/handlers/default.js +1 -1
- package/dist/cjs/contracts/handlers/delegate.js +1 -1
- package/dist/cjs/contracts/handlers/helpers.js +1 -1
- package/dist/cjs/extension/asset/assetGroup.js +92 -5
- package/dist/cjs/extension/asset/assetId.js +67 -3
- package/dist/cjs/extension/asset/assetInput.js +18 -0
- package/dist/cjs/extension/asset/assetOutput.js +15 -0
- package/dist/cjs/extension/asset/assetRef.js +66 -0
- package/dist/cjs/extension/asset/metadata.js +15 -0
- package/dist/cjs/extension/asset/packet.js +4 -1
- package/dist/cjs/extension/index.js +1 -1
- package/dist/cjs/forfeit.js +14 -0
- package/dist/cjs/identity/seedIdentity.js +2 -2
- package/dist/cjs/identity/singleKey.js +4 -0
- package/dist/cjs/intent/index.js +28 -12
- package/dist/cjs/providers/ark.js +3 -2
- package/dist/cjs/providers/delegator.js +20 -1
- package/dist/cjs/providers/expoArk.js +2 -2
- package/dist/cjs/providers/indexer.js +2 -2
- package/dist/cjs/providers/onchain.js +2 -1
- package/dist/cjs/repositories/realm/schemas.js +2 -2
- package/dist/cjs/repositories/realm/types.js +1 -1
- package/dist/cjs/script/address.js +37 -6
- package/dist/cjs/script/base.js +70 -1
- package/dist/cjs/script/default.js +3 -0
- package/dist/cjs/script/delegate.js +4 -0
- package/dist/cjs/script/tapscript.js +17 -2
- package/dist/cjs/script/vhtlc.js +35 -27
- package/dist/cjs/storage/fileSystem.js +1 -1
- package/dist/cjs/storage/inMemory.js +1 -1
- package/dist/cjs/storage/indexedDB.js +1 -1
- package/dist/cjs/storage/localStorage.js +1 -1
- package/dist/cjs/tree/validation.js +1 -1
- package/dist/cjs/utils/arkTransaction.js +5 -5
- package/dist/cjs/utils/bip21.js +16 -3
- package/dist/cjs/utils/syncCursors.js +4 -4
- package/dist/cjs/utils/transaction.js +1 -1
- package/dist/cjs/utils/transactionHistory.js +11 -11
- package/dist/cjs/utils/unknownFields.js +3 -3
- package/dist/cjs/wallet/asset-manager.js +4 -4
- package/dist/cjs/wallet/batch.js +5 -5
- package/dist/cjs/wallet/delegator.js +9 -8
- package/dist/cjs/wallet/expo/background.js +3 -3
- package/dist/cjs/wallet/expo/wallet.js +7 -7
- package/dist/cjs/wallet/index.js +43 -0
- package/dist/cjs/wallet/onchain.js +43 -5
- package/dist/cjs/wallet/ramps.js +44 -14
- package/dist/cjs/wallet/serviceWorker/wallet-message-handler.js +22 -22
- package/dist/cjs/wallet/serviceWorker/wallet.js +28 -24
- package/dist/cjs/wallet/unroll.js +12 -8
- package/dist/cjs/wallet/utils.js +1 -1
- package/dist/cjs/wallet/vtxo-manager.js +122 -82
- package/dist/cjs/wallet/wallet.js +125 -67
- package/dist/cjs/worker/expo/asyncStorageTaskQueue.js +1 -1
- package/dist/cjs/worker/expo/processors/contractPollProcessor.js +2 -2
- package/dist/cjs/worker/expo/taskRunner.js +3 -3
- package/dist/cjs/worker/messageBus.js +3 -0
- package/dist/esm/arkfee/estimator.js +1 -1
- package/dist/esm/arkfee/types.js +2 -1
- package/dist/esm/arknote/index.js +43 -4
- package/dist/esm/bip322/index.js +1 -1
- package/dist/esm/contracts/arkcontract.js +1 -1
- package/dist/esm/contracts/contractManager.js +40 -24
- package/dist/esm/contracts/contractWatcher.js +29 -22
- package/dist/esm/contracts/handlers/default.js +1 -1
- package/dist/esm/contracts/handlers/delegate.js +1 -1
- package/dist/esm/contracts/handlers/helpers.js +1 -1
- package/dist/esm/extension/asset/assetGroup.js +92 -5
- package/dist/esm/extension/asset/assetId.js +67 -3
- package/dist/esm/extension/asset/assetInput.js +18 -0
- package/dist/esm/extension/asset/assetOutput.js +15 -0
- package/dist/esm/extension/asset/assetRef.js +66 -0
- package/dist/esm/extension/asset/metadata.js +15 -0
- package/dist/esm/extension/asset/packet.js +4 -1
- package/dist/esm/extension/index.js +1 -1
- package/dist/esm/forfeit.js +14 -0
- package/dist/esm/identity/seedIdentity.js +2 -2
- package/dist/esm/identity/singleKey.js +4 -0
- package/dist/esm/index.js +1 -1
- package/dist/esm/intent/index.js +28 -12
- package/dist/esm/providers/ark.js +3 -2
- package/dist/esm/providers/delegator.js +20 -1
- package/dist/esm/providers/expoArk.js +2 -2
- package/dist/esm/providers/indexer.js +2 -2
- package/dist/esm/providers/onchain.js +2 -1
- package/dist/esm/repositories/realm/schemas.js +2 -2
- package/dist/esm/repositories/realm/types.js +1 -1
- package/dist/esm/script/address.js +37 -6
- package/dist/esm/script/base.js +70 -1
- package/dist/esm/script/default.js +3 -0
- package/dist/esm/script/delegate.js +4 -0
- package/dist/esm/script/tapscript.js +17 -2
- package/dist/esm/script/vhtlc.js +35 -27
- package/dist/esm/storage/fileSystem.js +1 -1
- package/dist/esm/storage/inMemory.js +1 -1
- package/dist/esm/storage/indexedDB.js +1 -1
- package/dist/esm/storage/localStorage.js +1 -1
- package/dist/esm/tree/validation.js +1 -1
- package/dist/esm/utils/arkTransaction.js +5 -5
- package/dist/esm/utils/bip21.js +16 -3
- package/dist/esm/utils/syncCursors.js +4 -4
- package/dist/esm/utils/transaction.js +1 -1
- package/dist/esm/utils/transactionHistory.js +11 -11
- package/dist/esm/utils/unknownFields.js +3 -3
- package/dist/esm/wallet/asset-manager.js +4 -4
- package/dist/esm/wallet/batch.js +5 -5
- package/dist/esm/wallet/delegator.js +9 -8
- package/dist/esm/wallet/expo/background.js +3 -3
- package/dist/esm/wallet/expo/wallet.js +7 -7
- package/dist/esm/wallet/index.js +43 -0
- package/dist/esm/wallet/onchain.js +43 -5
- package/dist/esm/wallet/ramps.js +44 -14
- package/dist/esm/wallet/serviceWorker/wallet-message-handler.js +22 -22
- package/dist/esm/wallet/serviceWorker/wallet.js +28 -24
- package/dist/esm/wallet/unroll.js +12 -8
- package/dist/esm/wallet/utils.js +1 -1
- package/dist/esm/wallet/vtxo-manager.js +121 -81
- package/dist/esm/wallet/wallet.js +125 -67
- package/dist/esm/worker/expo/asyncStorageTaskQueue.js +1 -1
- package/dist/esm/worker/expo/processors/contractPollProcessor.js +2 -2
- package/dist/esm/worker/expo/taskRunner.js +3 -3
- package/dist/esm/worker/messageBus.js +3 -0
- package/dist/types/arkfee/estimator.d.ts +1 -1
- package/dist/types/arkfee/types.d.ts +2 -1
- package/dist/types/arknote/index.d.ts +44 -4
- package/dist/types/bip322/index.d.ts +1 -1
- package/dist/types/contracts/arkcontract.d.ts +1 -1
- package/dist/types/contracts/contractManager.d.ts +40 -63
- package/dist/types/contracts/contractWatcher.d.ts +39 -18
- package/dist/types/contracts/handlers/default.d.ts +1 -1
- package/dist/types/contracts/handlers/delegate.d.ts +1 -1
- package/dist/types/contracts/handlers/helpers.d.ts +1 -1
- package/dist/types/contracts/types.d.ts +36 -26
- package/dist/types/extension/asset/assetGroup.d.ts +92 -1
- package/dist/types/extension/asset/assetId.d.ts +67 -3
- package/dist/types/extension/asset/assetInput.d.ts +18 -0
- package/dist/types/extension/asset/assetOutput.d.ts +15 -0
- package/dist/types/extension/asset/assetRef.d.ts +66 -0
- package/dist/types/extension/asset/metadata.d.ts +15 -0
- package/dist/types/extension/asset/packet.d.ts +4 -1
- package/dist/types/extension/index.d.ts +1 -1
- package/dist/types/forfeit.d.ts +14 -0
- package/dist/types/identity/index.d.ts +16 -0
- package/dist/types/identity/seedIdentity.d.ts +8 -6
- package/dist/types/identity/singleKey.d.ts +4 -0
- package/dist/types/intent/index.d.ts +19 -6
- package/dist/types/providers/ark.d.ts +40 -2
- package/dist/types/providers/delegator.d.ts +54 -1
- package/dist/types/providers/expoArk.d.ts +2 -2
- package/dist/types/providers/indexer.d.ts +105 -2
- package/dist/types/providers/onchain.d.ts +62 -1
- package/dist/types/repositories/realm/schemas.d.ts +2 -2
- package/dist/types/repositories/realm/types.d.ts +2 -2
- package/dist/types/repositories/walletRepository.d.ts +16 -0
- package/dist/types/script/address.d.ts +35 -2
- package/dist/types/script/base.d.ts +66 -1
- package/dist/types/script/default.d.ts +3 -0
- package/dist/types/script/delegate.d.ts +4 -0
- package/dist/types/script/tapscript.d.ts +17 -2
- package/dist/types/script/vhtlc.d.ts +35 -27
- package/dist/types/storage/fileSystem.d.ts +1 -1
- package/dist/types/storage/inMemory.d.ts +1 -1
- package/dist/types/storage/index.d.ts +1 -1
- package/dist/types/storage/indexedDB.d.ts +1 -1
- package/dist/types/storage/localStorage.d.ts +1 -1
- package/dist/types/utils/arkTransaction.d.ts +3 -3
- package/dist/types/utils/bip21.d.ts +17 -0
- package/dist/types/utils/syncCursors.d.ts +4 -4
- package/dist/types/utils/transaction.d.ts +1 -1
- package/dist/types/utils/transactionHistory.d.ts +3 -3
- package/dist/types/utils/unknownFields.d.ts +5 -5
- package/dist/types/wallet/asset-manager.d.ts +3 -3
- package/dist/types/wallet/batch.d.ts +27 -7
- package/dist/types/wallet/delegator.d.ts +10 -0
- package/dist/types/wallet/expo/background.d.ts +4 -4
- package/dist/types/wallet/expo/wallet.d.ts +10 -10
- package/dist/types/wallet/index.d.ts +457 -25
- package/dist/types/wallet/onchain.d.ts +42 -4
- package/dist/types/wallet/ramps.d.ts +40 -10
- package/dist/types/wallet/serviceWorker/wallet-message-handler.d.ts +4 -4
- package/dist/types/wallet/serviceWorker/wallet.d.ts +71 -33
- package/dist/types/wallet/unroll.d.ts +8 -6
- package/dist/types/wallet/vtxo-manager.d.ts +146 -93
- package/dist/types/wallet/wallet.d.ts +91 -33
- package/dist/types/worker/expo/asyncStorageTaskQueue.d.ts +1 -1
- package/dist/types/worker/expo/processors/contractPollProcessor.d.ts +1 -1
- package/dist/types/worker/expo/taskRunner.d.ts +6 -6
- package/dist/types/worker/messageBus.d.ts +5 -3
- package/package.json +1 -1
|
@@ -9,7 +9,7 @@ import { DUST_AMOUNT } from './utils.js';
|
|
|
9
9
|
* Onchain Bitcoin wallet implementation for traditional Bitcoin transactions.
|
|
10
10
|
*
|
|
11
11
|
* This wallet handles regular Bitcoin transactions on the blockchain without
|
|
12
|
-
* using the
|
|
12
|
+
* using the Arkade protocol. It supports P2TR (Pay-to-Taproot) addresses and
|
|
13
13
|
* provides basic Bitcoin wallet functionality.
|
|
14
14
|
*
|
|
15
15
|
* @example
|
|
@@ -29,6 +29,16 @@ export class OnchainWallet {
|
|
|
29
29
|
this.onchainP2TR = onchainP2TR;
|
|
30
30
|
this.provider = provider;
|
|
31
31
|
}
|
|
32
|
+
/**
|
|
33
|
+
* Create an onchain wallet for the given identity and Bitcoin network.
|
|
34
|
+
*
|
|
35
|
+
* @param identity - Identity used to derive the Taproot key and sign transactions
|
|
36
|
+
* @param networkName - Bitcoin network name, @see NetworkName
|
|
37
|
+
* @param provider - Optional onchain provider override, @see OnchainProvider
|
|
38
|
+
* @returns Configured onchain wallet
|
|
39
|
+
* @defaultValue `provider = new EsploraProvider('https://mempool.space/api')`
|
|
40
|
+
* @throws Error if the configured identity cannot produce a valid x-only public key
|
|
41
|
+
*/
|
|
32
42
|
static async create(identity, networkName, provider) {
|
|
33
43
|
const pubkey = await identity.xOnlyPublicKey();
|
|
34
44
|
if (!pubkey) {
|
|
@@ -42,9 +52,21 @@ export class OnchainWallet {
|
|
|
42
52
|
get address() {
|
|
43
53
|
return this.onchainP2TR.address || "";
|
|
44
54
|
}
|
|
55
|
+
/**
|
|
56
|
+
* Fetch spendable onchain outputs for the wallet address.
|
|
57
|
+
*
|
|
58
|
+
* @returns Spendable onchain outputs for the wallet address
|
|
59
|
+
* @see getBalance
|
|
60
|
+
*/
|
|
45
61
|
async getCoins() {
|
|
46
62
|
return this.provider.getCoins(this.address);
|
|
47
63
|
}
|
|
64
|
+
/**
|
|
65
|
+
* Return the wallet's total onchain balance in satoshis.
|
|
66
|
+
*
|
|
67
|
+
* @returns Confirmed plus unconfirmed onchain balance
|
|
68
|
+
* @see getCoins
|
|
69
|
+
*/
|
|
48
70
|
async getBalance() {
|
|
49
71
|
const coins = await this.getCoins();
|
|
50
72
|
const onchainConfirmed = coins
|
|
@@ -59,9 +81,9 @@ export class OnchainWallet {
|
|
|
59
81
|
/**
|
|
60
82
|
* Iteratively selects coins and estimates transaction fees until convergence.
|
|
61
83
|
*
|
|
62
|
-
* This method handles the circular dependency between
|
|
84
|
+
* This method handles the circular dependency between output selection and fee
|
|
63
85
|
* estimation: the fee depends on transaction size, which depends on the number
|
|
64
|
-
* of inputs (selected
|
|
86
|
+
* of inputs (selected outputs) and whether a change output is needed.
|
|
65
87
|
*
|
|
66
88
|
* The algorithm iterates up to 10 times, refining the fee estimate based on
|
|
67
89
|
* the actual transaction structure. It resolves dust oscillation loops that
|
|
@@ -70,7 +92,7 @@ export class OnchainWallet {
|
|
|
70
92
|
* When a lower fee is computed (indicating the change output was dropped),
|
|
71
93
|
* the function accepts this state to guarantee termination.
|
|
72
94
|
*
|
|
73
|
-
* @param coins - Available
|
|
95
|
+
* @param coins - Available onchain outputs to select from
|
|
74
96
|
* @param amount - Target send amount in satoshis
|
|
75
97
|
* @param feeRate - Fee rate in sat/vbyte
|
|
76
98
|
* @param recipientAddress - Destination address for size estimation
|
|
@@ -103,6 +125,14 @@ export class OnchainWallet {
|
|
|
103
125
|
}
|
|
104
126
|
throw new Error("Fee estimation failed: could not converge");
|
|
105
127
|
}
|
|
128
|
+
/**
|
|
129
|
+
* Send bitcoin to a single onchain address.
|
|
130
|
+
*
|
|
131
|
+
* @param params - destination `address`, `amount` (in satoshis), and optional `feeRate` override (other fields ignored)
|
|
132
|
+
* @returns Broadcast transaction id
|
|
133
|
+
* @throws Error if the amount is non-positive, below dust, or cannot be funded
|
|
134
|
+
* @see SendBitcoinParams
|
|
135
|
+
*/
|
|
106
136
|
async send(params) {
|
|
107
137
|
if (params.amount <= 0) {
|
|
108
138
|
throw new Error("Amount must be positive");
|
|
@@ -148,6 +178,14 @@ export class OnchainWallet {
|
|
|
148
178
|
const txid = await this.provider.broadcastTransaction(tx.hex);
|
|
149
179
|
return txid;
|
|
150
180
|
}
|
|
181
|
+
/**
|
|
182
|
+
* CPFP-bump a parent transaction that contains a pay-to-anchor output.
|
|
183
|
+
*
|
|
184
|
+
* @param parent - Parent transaction containing a pay-to-anchor output
|
|
185
|
+
* @returns Tuple of parent transaction id and child transaction id
|
|
186
|
+
* @throws Error if the parent transaction has no pay-to-anchor output or bumping cannot be funded
|
|
187
|
+
* @see send
|
|
188
|
+
*/
|
|
151
189
|
async bumpP2A(parent) {
|
|
152
190
|
const parentVsize = parent.vsize;
|
|
153
191
|
let child = new Transaction({
|
|
@@ -169,7 +207,7 @@ export class OnchainWallet {
|
|
|
169
207
|
if (!fee) {
|
|
170
208
|
throw new Error(`invalid fee, got ${fee} with vsize ${packageVSize}, feeRate ${feeRate}`);
|
|
171
209
|
}
|
|
172
|
-
// Select
|
|
210
|
+
// Select onchain outputs
|
|
173
211
|
const coins = await this.getCoins();
|
|
174
212
|
const selected = selectCoins(coins, fee, true);
|
|
175
213
|
for (const input of selected.inputs) {
|
package/dist/esm/wallet/ramps.js
CHANGED
|
@@ -4,30 +4,50 @@ import { hex } from "@scure/base";
|
|
|
4
4
|
import { networks } from '../networks.js';
|
|
5
5
|
import { ArkAddress } from '../script/address.js';
|
|
6
6
|
/**
|
|
7
|
-
* Ramps is a class wrapping
|
|
7
|
+
* Ramps is a class wrapping `settle` method to provide a more convenient interface for onboarding and offboarding operations.
|
|
8
|
+
*
|
|
9
|
+
* @see IWallet.settle
|
|
10
|
+
* @see onboard
|
|
11
|
+
* @see offboard
|
|
8
12
|
*
|
|
9
13
|
* @example
|
|
10
14
|
* ```typescript
|
|
11
15
|
* const ramps = new Ramps(wallet);
|
|
12
|
-
*
|
|
13
|
-
* await ramps.
|
|
16
|
+
* const feeInfo = { intentFee: {}, txFeeRate: '1' };
|
|
17
|
+
* await ramps.onboard(feeInfo); // onboard all boarding inputs
|
|
18
|
+
* await ramps.offboard('bc1q...', feeInfo); // collaboratively exit all virtual outputs to an onchain address
|
|
14
19
|
* ```
|
|
15
20
|
*/
|
|
16
21
|
export class Ramps {
|
|
22
|
+
/**
|
|
23
|
+
* Create convenience wrappers for onboarding and offboarding flows.
|
|
24
|
+
*
|
|
25
|
+
* @param wallet - Wallet used to query funds and execute settlement transactions
|
|
26
|
+
*/
|
|
17
27
|
constructor(wallet) {
|
|
18
28
|
this.wallet = wallet;
|
|
19
29
|
}
|
|
20
30
|
/**
|
|
21
|
-
* Onboard boarding
|
|
31
|
+
* Onboard boarding inputs.
|
|
22
32
|
*
|
|
23
33
|
* @param feeInfo - The fee info to deduct from the onboard amount.
|
|
24
|
-
* @param boardingUtxos -
|
|
25
|
-
* @param amount -
|
|
26
|
-
* @param eventCallback -
|
|
34
|
+
* @param boardingUtxos - Specific boarding inputs to onboard. If not provided, all boarding inputs will be used.
|
|
35
|
+
* @param amount - Amount to onboard. If not provided, the total amount of boarding inputs will be onboarded.
|
|
36
|
+
* @param eventCallback - Optional callback that receives settlement events
|
|
37
|
+
* @returns The Arkade transaction id created by settlement
|
|
38
|
+
* @throws Error if no boarding inputs remain after fee deduction or if `amount` exceeds available value
|
|
39
|
+
* @see IWallet.getBoardingUtxos
|
|
40
|
+
* @see IWallet.settle
|
|
41
|
+
* @example
|
|
42
|
+
* ```typescript
|
|
43
|
+
* const feeInfo = { intentFee: {}, txFeeRate: '1' };
|
|
44
|
+
* const ramps = new Ramps(wallet);
|
|
45
|
+
* await ramps.onboard(feeInfo);
|
|
46
|
+
* ```
|
|
27
47
|
*/
|
|
28
48
|
async onboard(feeInfo, boardingUtxos, amount, eventCallback) {
|
|
29
49
|
boardingUtxos = boardingUtxos ?? (await this.wallet.getBoardingUtxos());
|
|
30
|
-
// Calculate input fees and filter out
|
|
50
|
+
// Calculate input fees and filter out boarding inputs where fee >= value.
|
|
31
51
|
const estimator = new Estimator(feeInfo?.intentFee ?? {});
|
|
32
52
|
const filteredBoardingUtxos = [];
|
|
33
53
|
let totalAmount = 0n;
|
|
@@ -36,7 +56,7 @@ export class Ramps {
|
|
|
36
56
|
amount: BigInt(utxo.value),
|
|
37
57
|
});
|
|
38
58
|
if (inputFee.satoshis >= utxo.value) {
|
|
39
|
-
//
|
|
59
|
+
// Skip boarding inputs where spending fees are greater than or equal to the input value.
|
|
40
60
|
continue;
|
|
41
61
|
}
|
|
42
62
|
filteredBoardingUtxos.push(utxo);
|
|
@@ -84,19 +104,29 @@ export class Ramps {
|
|
|
84
104
|
}, eventCallback);
|
|
85
105
|
}
|
|
86
106
|
/**
|
|
87
|
-
* Offboard
|
|
107
|
+
* Offboard virtual outputs, or collaboratively exit them to an onchain address.
|
|
88
108
|
*
|
|
89
109
|
* @param destinationAddress - The destination address to offboard to.
|
|
90
110
|
* @param feeInfo - The fee info to deduct from the offboard amount.
|
|
91
|
-
* @param amount - The amount to offboard. If not provided, the total amount of
|
|
92
|
-
* @param eventCallback -
|
|
111
|
+
* @param amount - The amount to offboard. If not provided, the total amount of virtual outputs will be offboarded.
|
|
112
|
+
* @param eventCallback - Optional callback that receives settlement events
|
|
113
|
+
* @returns The Arkade transaction id created by settlement
|
|
114
|
+
* @throws Error if no virtual outputs remain after fee deduction or the destination address cannot be decoded
|
|
115
|
+
* @see IWallet.getVtxos
|
|
116
|
+
* @see IWallet.settle
|
|
117
|
+
* @example
|
|
118
|
+
* ```typescript
|
|
119
|
+
* const feeInfo = { intentFee: {}, txFeeRate: '1' };
|
|
120
|
+
* const ramps = new Ramps(wallet);
|
|
121
|
+
* await ramps.offboard('bc1q...', feeInfo);
|
|
122
|
+
* ```
|
|
93
123
|
*/
|
|
94
124
|
async offboard(destinationAddress, feeInfo, amount, eventCallback) {
|
|
95
125
|
const vtxos = await this.wallet.getVtxos({
|
|
96
126
|
withRecoverable: true,
|
|
97
127
|
withUnrolled: false,
|
|
98
128
|
});
|
|
99
|
-
// Calculate input fees and filter out
|
|
129
|
+
// Calculate input fees and filter out virtual outputs where fee >= value.
|
|
100
130
|
const estimator = new Estimator(feeInfo?.intentFee ?? {});
|
|
101
131
|
const filteredVtxos = [];
|
|
102
132
|
let totalAmount = 0n;
|
|
@@ -113,7 +143,7 @@ export class Ramps {
|
|
|
113
143
|
: undefined,
|
|
114
144
|
});
|
|
115
145
|
if (inputFee.satoshis >= vtxo.value) {
|
|
116
|
-
//
|
|
146
|
+
// Skip virtual outputs where spending fees are greater than or equal to the output value.
|
|
117
147
|
continue;
|
|
118
148
|
}
|
|
119
149
|
filteredVtxos.push(vtxo);
|
|
@@ -49,7 +49,7 @@ export class WalletMessageHandler {
|
|
|
49
49
|
this.contractEventsSubscription = undefined;
|
|
50
50
|
}
|
|
51
51
|
// Dispose the wallet to stop VtxoManager background tasks
|
|
52
|
-
// (auto-renewal, boarding
|
|
52
|
+
// (auto-renewal, boarding input polling) and ContractWatcher.
|
|
53
53
|
try {
|
|
54
54
|
if (this.wallet) {
|
|
55
55
|
await this.wallet.dispose();
|
|
@@ -484,7 +484,7 @@ export class WalletMessageHandler {
|
|
|
484
484
|
}
|
|
485
485
|
const totalBoarding = confirmed + unconfirmed;
|
|
486
486
|
const totalOffchain = settled + preconfirmed + recoverable;
|
|
487
|
-
// aggregate asset balances from spendable
|
|
487
|
+
// aggregate asset balances from spendable virtual outputs
|
|
488
488
|
const assetBalances = new Map();
|
|
489
489
|
for (const vtxo of spendableVtxos) {
|
|
490
490
|
if (vtxo.assets) {
|
|
@@ -529,9 +529,9 @@ export class WalletMessageHandler {
|
|
|
529
529
|
return;
|
|
530
530
|
}
|
|
531
531
|
// Initialize contract manager FIRST — this populates the repository
|
|
532
|
-
// with full
|
|
532
|
+
// with full virtual output history for all contracts (one indexer call per contract)
|
|
533
533
|
await this.ensureContractEventBroadcasting();
|
|
534
|
-
// Refresh cached data (
|
|
534
|
+
// Refresh cached data (virtual outputs, boarding inputs, tx history)
|
|
535
535
|
await this.refreshCachedData();
|
|
536
536
|
// Recover pending transactions (init-only, not on reload).
|
|
537
537
|
// Pending txs only exist if a send was interrupted mid-finalization.
|
|
@@ -562,12 +562,12 @@ export class WalletMessageHandler {
|
|
|
562
562
|
: [];
|
|
563
563
|
if ([...newVtxos, ...spentVtxos].length === 0)
|
|
564
564
|
return;
|
|
565
|
-
// save
|
|
565
|
+
// save virtual outputs using unified repository
|
|
566
566
|
await this.walletRepository?.saveVtxos(address, [
|
|
567
567
|
...newVtxos,
|
|
568
568
|
...spentVtxos,
|
|
569
569
|
]);
|
|
570
|
-
// notify all clients about the
|
|
570
|
+
// notify all clients about the virtual output state update
|
|
571
571
|
this.scheduleForNextTick(() => this.tagged({
|
|
572
572
|
type: "VTXO_UPDATE",
|
|
573
573
|
broadcast: true,
|
|
@@ -577,11 +577,11 @@ export class WalletMessageHandler {
|
|
|
577
577
|
if (funds.type === "utxo") {
|
|
578
578
|
const utxos = funds.coins.map((utxo) => extendCoin(this.readonlyWallet, utxo));
|
|
579
579
|
const boardingAddress = await this.readonlyWallet.getBoardingAddress();
|
|
580
|
-
// save
|
|
581
|
-
// TODO: remove
|
|
580
|
+
// save boarding inputs using unified repository
|
|
581
|
+
// TODO: remove UTXOs by address
|
|
582
582
|
// await this.walletRepository.clearUtxos(boardingAddress);
|
|
583
583
|
await this.walletRepository?.saveUtxos(boardingAddress, utxos);
|
|
584
|
-
// notify all clients about the
|
|
584
|
+
// notify all clients about the boarding input state update
|
|
585
585
|
this.scheduleForNextTick(() => this.tagged({
|
|
586
586
|
type: "UTXO_UPDATE",
|
|
587
587
|
broadcast: true,
|
|
@@ -590,8 +590,8 @@ export class WalletMessageHandler {
|
|
|
590
590
|
}
|
|
591
591
|
});
|
|
592
592
|
// Eagerly start the VtxoManager so its background tasks (auto-renewal,
|
|
593
|
-
// boarding
|
|
594
|
-
// waiting for a client to send a
|
|
593
|
+
// boarding input polling/sweep) run inside the service worker without
|
|
594
|
+
// waiting for a client to send a VtxoManager message first.
|
|
595
595
|
if (this.wallet) {
|
|
596
596
|
try {
|
|
597
597
|
await this.wallet.getVtxoManager();
|
|
@@ -602,7 +602,7 @@ export class WalletMessageHandler {
|
|
|
602
602
|
}
|
|
603
603
|
}
|
|
604
604
|
/**
|
|
605
|
-
* Refresh
|
|
605
|
+
* Refresh virtual outputs, boarding inputs, and transaction history from cache.
|
|
606
606
|
* Shared by onWalletInitialized (full bootstrap) and reloadWallet
|
|
607
607
|
* (post-refresh), avoiding duplicate subscriptions and VtxoManager restarts.
|
|
608
608
|
*/
|
|
@@ -610,14 +610,14 @@ export class WalletMessageHandler {
|
|
|
610
610
|
if (!this.readonlyWallet || !this.walletRepository) {
|
|
611
611
|
return;
|
|
612
612
|
}
|
|
613
|
-
// Read
|
|
613
|
+
// Read virtual outputs from repository (now populated by contract manager)
|
|
614
614
|
const vtxos = await this.getVtxosFromRepo();
|
|
615
|
-
// Fetch boarding
|
|
615
|
+
// Fetch boarding inputs and save using unified repository
|
|
616
616
|
const boardingAddress = await this.readonlyWallet.getBoardingAddress();
|
|
617
617
|
const coins = await this.readonlyWallet.onchainProvider.getCoins(boardingAddress);
|
|
618
618
|
await this.walletRepository.deleteUtxos(boardingAddress);
|
|
619
619
|
await this.walletRepository.saveUtxos(boardingAddress, coins.map((utxo) => extendCoin(this.readonlyWallet, utxo)));
|
|
620
|
-
// Build transaction history from cached
|
|
620
|
+
// Build transaction history from cached virtual outputs (no indexer call)
|
|
621
621
|
const address = await this.readonlyWallet.getAddress();
|
|
622
622
|
const txs = await this.buildTransactionHistoryFromCache(vtxos);
|
|
623
623
|
if (txs)
|
|
@@ -759,7 +759,7 @@ export class WalletMessageHandler {
|
|
|
759
759
|
this.indexerProvider = undefined;
|
|
760
760
|
}
|
|
761
761
|
/**
|
|
762
|
-
* Read all
|
|
762
|
+
* Read all virtual outputs from the repository, aggregated across all contract
|
|
763
763
|
* addresses and the wallet's primary address, with deduplication.
|
|
764
764
|
*/
|
|
765
765
|
async getVtxosFromRepo() {
|
|
@@ -776,7 +776,7 @@ export class WalletMessageHandler {
|
|
|
776
776
|
}
|
|
777
777
|
}
|
|
778
778
|
};
|
|
779
|
-
// Aggregate
|
|
779
|
+
// Aggregate virtual outputs from all contract addresses
|
|
780
780
|
const manager = await this.readonlyWallet.getContractManager();
|
|
781
781
|
const contracts = await manager.getContracts();
|
|
782
782
|
for (const contract of contracts) {
|
|
@@ -790,15 +790,15 @@ export class WalletMessageHandler {
|
|
|
790
790
|
return allVtxos;
|
|
791
791
|
}
|
|
792
792
|
/**
|
|
793
|
-
* Build transaction history from cached
|
|
793
|
+
* Build transaction history from cached virtual outputs without hitting the indexer.
|
|
794
794
|
* Falls back to indexer only for uncached transaction timestamps.
|
|
795
795
|
*/
|
|
796
796
|
async buildTransactionHistoryFromCache(vtxos) {
|
|
797
797
|
if (!this.readonlyWallet)
|
|
798
798
|
return null;
|
|
799
799
|
const { boardingTxs, commitmentsToIgnore } = await this.readonlyWallet.getBoardingTxs();
|
|
800
|
-
// Build a lookup for cached
|
|
801
|
-
// Multiple
|
|
800
|
+
// Build a lookup for cached virtual output timestamps, keyed by txid.
|
|
801
|
+
// Multiple virtual outputs can share a txid (different vouts) — we keep the
|
|
802
802
|
// earliest createdAt so the history ordering is stable.
|
|
803
803
|
const vtxoCreatedAt = new Map();
|
|
804
804
|
for (const vtxo of vtxos) {
|
|
@@ -809,8 +809,8 @@ export class WalletMessageHandler {
|
|
|
809
809
|
}
|
|
810
810
|
}
|
|
811
811
|
// Pre-fetch uncached timestamps in a single batched indexer call.
|
|
812
|
-
// buildTransactionHistory needs these for spent-offchain
|
|
813
|
-
// no change outputs (i.e. arkTxId is set but no
|
|
812
|
+
// buildTransactionHistory needs these for spent-offchain virtual outputs with
|
|
813
|
+
// no change outputs (i.e. arkTxId is set but no virtual output has txid === arkTxId).
|
|
814
814
|
if (this.indexerProvider) {
|
|
815
815
|
const uncachedTxids = new Set();
|
|
816
816
|
for (const vtxo of vtxos) {
|
|
@@ -179,6 +179,13 @@ export class ServiceWorkerReadonlyWallet {
|
|
|
179
179
|
getTimeoutForRequest(request) {
|
|
180
180
|
return this.messageTimeouts[request.type] ?? 30000;
|
|
181
181
|
}
|
|
182
|
+
/**
|
|
183
|
+
* Create a readonly service-worker wallet bound to an already-registered worker.
|
|
184
|
+
*
|
|
185
|
+
* @param options - Service worker, identity, and backend configuration
|
|
186
|
+
* @returns Initialized readonly service-worker wallet
|
|
187
|
+
* @throws Error if service-worker initialization fails
|
|
188
|
+
*/
|
|
182
189
|
static async create(options) {
|
|
183
190
|
const walletRepository = options.storage?.walletRepository ??
|
|
184
191
|
new IndexedDBWalletRepository();
|
|
@@ -239,23 +246,17 @@ export class ServiceWorkerReadonlyWallet {
|
|
|
239
246
|
return wallet;
|
|
240
247
|
}
|
|
241
248
|
/**
|
|
242
|
-
* Simplified setup method that handles service worker registration
|
|
243
|
-
*
|
|
249
|
+
* Simplified setup method that handles service worker registration
|
|
250
|
+
* and wallet initialization automatically.
|
|
251
|
+
*
|
|
252
|
+
* @see ServiceWorkerReadonlyWallet.create
|
|
244
253
|
*
|
|
245
254
|
* @example
|
|
246
255
|
* ```typescript
|
|
247
|
-
* // One-liner setup - handles everything automatically!
|
|
248
256
|
* const wallet = await ServiceWorkerReadonlyWallet.setup({
|
|
249
257
|
* serviceWorkerPath: '/service-worker.js',
|
|
250
|
-
* arkServerUrl: 'https://
|
|
251
|
-
*
|
|
252
|
-
*
|
|
253
|
-
* // With custom readonly identity
|
|
254
|
-
* const identity = ReadonlySingleKey.fromPublicKey('your_public_key_hex');
|
|
255
|
-
* const wallet = await ServiceWorkerReadonlyWallet.setup({
|
|
256
|
-
* serviceWorkerPath: '/service-worker.js',
|
|
257
|
-
* arkServerUrl: 'https://mutinynet.arkade.sh',
|
|
258
|
-
* identity
|
|
258
|
+
* arkServerUrl: 'https://arkade.computer',
|
|
259
|
+
* identity: ReadonlySingleKey.fromPublicKey('your_public_key_hex')
|
|
259
260
|
* });
|
|
260
261
|
* ```
|
|
261
262
|
*/
|
|
@@ -457,6 +458,7 @@ export class ServiceWorkerReadonlyWallet {
|
|
|
457
458
|
});
|
|
458
459
|
return this.reinitPromise;
|
|
459
460
|
}
|
|
461
|
+
/** Clear cached wallet state from both the page and service worker storage. */
|
|
460
462
|
async clear() {
|
|
461
463
|
const message = {
|
|
462
464
|
id: getRandomId(),
|
|
@@ -529,6 +531,11 @@ export class ServiceWorkerReadonlyWallet {
|
|
|
529
531
|
throw new Error(`Failed to get boarding UTXOs: ${error}`);
|
|
530
532
|
}
|
|
531
533
|
}
|
|
534
|
+
/**
|
|
535
|
+
* Return service-worker wallet status, including connectivity and sync state.
|
|
536
|
+
*
|
|
537
|
+
* @returns Current service-worker wallet status payload including `walletInitalized` and `xOnlyPublicKey`
|
|
538
|
+
*/
|
|
532
539
|
async getStatus() {
|
|
533
540
|
const message = {
|
|
534
541
|
id: getRandomId(),
|
|
@@ -573,6 +580,11 @@ export class ServiceWorkerReadonlyWallet {
|
|
|
573
580
|
throw new Error(`Failed to get vtxos: ${error}`);
|
|
574
581
|
}
|
|
575
582
|
}
|
|
583
|
+
/**
|
|
584
|
+
* Trigger a wallet reload inside the service worker.
|
|
585
|
+
*
|
|
586
|
+
* @returns `true` when the wallet was reloaded
|
|
587
|
+
*/
|
|
576
588
|
async reload() {
|
|
577
589
|
const message = {
|
|
578
590
|
id: getRandomId(),
|
|
@@ -851,23 +863,15 @@ export class ServiceWorkerWallet extends ServiceWorkerReadonlyWallet {
|
|
|
851
863
|
return wallet;
|
|
852
864
|
}
|
|
853
865
|
/**
|
|
854
|
-
* Simplified setup method that handles service worker registration
|
|
855
|
-
*
|
|
866
|
+
* Simplified setup method that handles service worker registration
|
|
867
|
+
* and wallet initialization automatically.
|
|
856
868
|
*
|
|
857
869
|
* @example
|
|
858
870
|
* ```typescript
|
|
859
|
-
* // One-liner setup - handles everything automatically!
|
|
860
|
-
* const wallet = await ServiceWorkerWallet.setup({
|
|
861
|
-
* serviceWorkerPath: '/service-worker.js',
|
|
862
|
-
* arkServerUrl: 'https://mutinynet.arkade.sh'
|
|
863
|
-
* });
|
|
864
|
-
*
|
|
865
|
-
* // With custom identity
|
|
866
|
-
* const identity = SingleKey.fromHex('your_private_key_hex');
|
|
867
871
|
* const wallet = await ServiceWorkerWallet.setup({
|
|
868
872
|
* serviceWorkerPath: '/service-worker.js',
|
|
869
|
-
* arkServerUrl: 'https://
|
|
870
|
-
* identity
|
|
873
|
+
* arkServerUrl: 'https://arkade.computer',
|
|
874
|
+
* identity: MnemonicIdentity.fromMnemonic('abandon abandon...')
|
|
871
875
|
* });
|
|
872
876
|
* ```
|
|
873
877
|
*/
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { base64, hex } from "@scure/base";
|
|
2
2
|
import { SigHash, TaprootControlBlock } from "@scure/btc-signer";
|
|
3
|
+
import { timelockToSequence } from '../contracts/handlers/helpers.js';
|
|
3
4
|
import { ChainTxType } from '../providers/indexer.js';
|
|
4
5
|
import { VtxoScript } from '../script/base.js';
|
|
5
6
|
import { TxWeightEstimator } from '../utils/txSizeEstimator.js';
|
|
@@ -15,7 +16,7 @@ export var Unroll;
|
|
|
15
16
|
StepType[StepType["DONE"] = 2] = "DONE";
|
|
16
17
|
})(StepType = Unroll.StepType || (Unroll.StepType = {}));
|
|
17
18
|
/**
|
|
18
|
-
* Manages the unrolling process of a
|
|
19
|
+
* Manages the unrolling process of a virtual output back to the Bitcoin blockchain.
|
|
19
20
|
*
|
|
20
21
|
* The Session class implements an async iterator that processes the unrolling steps:
|
|
21
22
|
* 1. **WAIT**: Waits for a transaction to be confirmed onchain (if it's in mempool)
|
|
@@ -39,19 +40,21 @@ export var Unroll;
|
|
|
39
40
|
* console.log(`Broadcasting transaction ${doneStep.tx.id}`);
|
|
40
41
|
* break;
|
|
41
42
|
* case Unroll.StepType.DONE:
|
|
42
|
-
* console.log(`Unrolling complete for
|
|
43
|
+
* console.log(`Unrolling complete for virtual output ${doneStep.vtxoTxid}`);
|
|
43
44
|
* break;
|
|
44
45
|
* }
|
|
45
46
|
* }
|
|
46
47
|
* ```
|
|
47
48
|
**/
|
|
48
49
|
class Session {
|
|
50
|
+
/** Create an unroll session from a virtual output outpoint and its dependency chain. */
|
|
49
51
|
constructor(toUnroll, bumper, explorer, indexer) {
|
|
50
52
|
this.toUnroll = toUnroll;
|
|
51
53
|
this.bumper = bumper;
|
|
52
54
|
this.explorer = explorer;
|
|
53
55
|
this.indexer = indexer;
|
|
54
56
|
}
|
|
57
|
+
/** Create an unroll session by loading the virtual output chain from the indexer. */
|
|
55
58
|
static async create(toUnroll, bumper, explorer, indexer) {
|
|
56
59
|
const { chain } = await indexer.getVtxoChain(toUnroll);
|
|
57
60
|
return new Session({ ...toUnroll, chain }, bumper, explorer, indexer);
|
|
@@ -120,7 +123,7 @@ export var Unroll;
|
|
|
120
123
|
});
|
|
121
124
|
}
|
|
122
125
|
else {
|
|
123
|
-
// finalize
|
|
126
|
+
// finalize Arkade transaction
|
|
124
127
|
tx.finalize();
|
|
125
128
|
}
|
|
126
129
|
return {
|
|
@@ -150,11 +153,11 @@ export var Unroll;
|
|
|
150
153
|
}
|
|
151
154
|
Unroll.Session = Session;
|
|
152
155
|
/**
|
|
153
|
-
* Complete the unroll of a
|
|
154
|
-
* @param wallet the wallet owning the
|
|
155
|
-
* @param vtxoTxids the txids of the
|
|
156
|
+
* Complete the unroll of a virtual output by broadcasting the transaction that spends the CSV path.
|
|
157
|
+
* @param wallet the wallet owning the virtual output(s)
|
|
158
|
+
* @param vtxoTxids the txids of the virtual output(s) to complete unroll
|
|
156
159
|
* @param outputAddress the address to send the unrolled funds to
|
|
157
|
-
* @throws if the
|
|
160
|
+
* @throws if the virtual output(s) are not fully unrolled, if the txids are not found, if the tx is not confirmed, if no exit path is found or not available
|
|
158
161
|
* @returns the txid of the transaction spending the unrolled funds
|
|
159
162
|
*/
|
|
160
163
|
async function completeUnroll(wallet, vtxoTxids, outputAddress) {
|
|
@@ -184,11 +187,12 @@ export var Unroll;
|
|
|
184
187
|
throw new Error(`spending leaf not found for vtxo ${vtxo.txid}:${vtxo.vout}`);
|
|
185
188
|
}
|
|
186
189
|
totalAmount += BigInt(vtxo.value);
|
|
190
|
+
const sequence = timelockToSequence(exit.params.timelock);
|
|
187
191
|
inputs.push({
|
|
188
192
|
txid: vtxo.txid,
|
|
189
193
|
index: vtxo.vout,
|
|
190
194
|
tapLeafScript: [spendingLeaf],
|
|
191
|
-
sequence
|
|
195
|
+
sequence,
|
|
192
196
|
witnessUtxo: {
|
|
193
197
|
amount: BigInt(vtxo.value),
|
|
194
198
|
script: VtxoScript.decode(vtxo.tapTree).pkScript,
|
package/dist/esm/wallet/utils.js
CHANGED
|
@@ -52,7 +52,7 @@ export function validateRecipients(recipients, dustAmount) {
|
|
|
52
52
|
address = ArkAddress.decode(recipient.address);
|
|
53
53
|
}
|
|
54
54
|
catch (e) {
|
|
55
|
-
throw new Error(`Invalid
|
|
55
|
+
throw new Error(`Invalid Arkade address: ${recipient.address}`);
|
|
56
56
|
}
|
|
57
57
|
const amount = recipient.amount || dustAmount;
|
|
58
58
|
if (amount <= 0) {
|