@arkade-os/sdk 0.1.3 → 0.2.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.
- package/README.md +156 -174
- package/dist/cjs/arknote/index.js +61 -58
- package/dist/cjs/bip322/errors.js +13 -0
- package/dist/cjs/bip322/index.js +178 -0
- package/dist/cjs/forfeit.js +14 -25
- package/dist/cjs/identity/singleKey.js +68 -0
- package/dist/cjs/index.js +41 -17
- package/dist/cjs/providers/ark.js +253 -317
- package/dist/cjs/providers/indexer.js +525 -0
- package/dist/cjs/providers/onchain.js +193 -15
- package/dist/cjs/script/address.js +48 -17
- package/dist/cjs/script/base.js +120 -3
- package/dist/cjs/script/default.js +18 -4
- package/dist/cjs/script/tapscript.js +46 -14
- package/dist/cjs/script/vhtlc.js +27 -7
- package/dist/cjs/tree/signingSession.js +63 -106
- package/dist/cjs/tree/txTree.js +193 -0
- package/dist/cjs/tree/validation.js +79 -155
- package/dist/cjs/utils/anchor.js +35 -0
- package/dist/cjs/utils/arkTransaction.js +108 -0
- package/dist/cjs/utils/transactionHistory.js +84 -72
- package/dist/cjs/utils/txSizeEstimator.js +12 -0
- package/dist/cjs/utils/unknownFields.js +211 -0
- package/dist/cjs/wallet/index.js +12 -0
- package/dist/cjs/wallet/onchain.js +201 -0
- package/dist/cjs/wallet/ramps.js +95 -0
- package/dist/cjs/wallet/serviceWorker/db/vtxo/idb.js +32 -0
- package/dist/cjs/wallet/serviceWorker/request.js +15 -12
- package/dist/cjs/wallet/serviceWorker/response.js +22 -27
- package/dist/cjs/wallet/serviceWorker/utils.js +8 -0
- package/dist/cjs/wallet/serviceWorker/wallet.js +58 -34
- package/dist/cjs/wallet/serviceWorker/worker.js +117 -108
- package/dist/cjs/wallet/unroll.js +270 -0
- package/dist/cjs/wallet/wallet.js +701 -459
- package/dist/esm/arknote/index.js +61 -57
- package/dist/esm/bip322/errors.js +9 -0
- package/dist/esm/bip322/index.js +174 -0
- package/dist/esm/forfeit.js +15 -26
- package/dist/esm/identity/singleKey.js +64 -0
- package/dist/esm/index.js +30 -12
- package/dist/esm/providers/ark.js +252 -317
- package/dist/esm/providers/indexer.js +521 -0
- package/dist/esm/providers/onchain.js +193 -15
- package/dist/esm/script/address.js +48 -17
- package/dist/esm/script/base.js +120 -3
- package/dist/esm/script/default.js +18 -4
- package/dist/esm/script/tapscript.js +46 -14
- package/dist/esm/script/vhtlc.js +27 -7
- package/dist/esm/tree/signingSession.js +65 -108
- package/dist/esm/tree/txTree.js +189 -0
- package/dist/esm/tree/validation.js +75 -152
- package/dist/esm/utils/anchor.js +31 -0
- package/dist/esm/utils/arkTransaction.js +105 -0
- package/dist/esm/utils/transactionHistory.js +84 -72
- package/dist/esm/utils/txSizeEstimator.js +12 -0
- package/dist/esm/utils/unknownFields.js +173 -0
- package/dist/esm/wallet/index.js +9 -0
- package/dist/esm/wallet/onchain.js +196 -0
- package/dist/esm/wallet/ramps.js +91 -0
- package/dist/esm/wallet/serviceWorker/db/vtxo/idb.js +32 -0
- package/dist/esm/wallet/serviceWorker/request.js +15 -12
- package/dist/esm/wallet/serviceWorker/response.js +22 -27
- package/dist/esm/wallet/serviceWorker/utils.js +8 -0
- package/dist/esm/wallet/serviceWorker/wallet.js +59 -35
- package/dist/esm/wallet/serviceWorker/worker.js +117 -108
- package/dist/esm/wallet/unroll.js +267 -0
- package/dist/esm/wallet/wallet.js +674 -466
- package/dist/types/arknote/index.d.ts +40 -13
- package/dist/types/bip322/errors.d.ts +6 -0
- package/dist/types/bip322/index.d.ts +57 -0
- package/dist/types/forfeit.d.ts +2 -14
- package/dist/types/identity/singleKey.d.ts +27 -0
- package/dist/types/index.d.ts +23 -12
- package/dist/types/providers/ark.d.ts +114 -95
- package/dist/types/providers/indexer.d.ts +186 -0
- package/dist/types/providers/onchain.d.ts +41 -11
- package/dist/types/script/address.d.ts +26 -2
- package/dist/types/script/base.d.ts +13 -3
- package/dist/types/script/default.d.ts +22 -0
- package/dist/types/script/tapscript.d.ts +61 -5
- package/dist/types/script/vhtlc.d.ts +27 -0
- package/dist/types/tree/signingSession.d.ts +5 -5
- package/dist/types/tree/txTree.d.ts +28 -0
- package/dist/types/tree/validation.d.ts +15 -22
- package/dist/types/utils/anchor.d.ts +19 -0
- package/dist/types/utils/arkTransaction.d.ts +27 -0
- package/dist/types/utils/transactionHistory.d.ts +7 -1
- package/dist/types/utils/txSizeEstimator.d.ts +3 -0
- package/dist/types/utils/unknownFields.d.ts +83 -0
- package/dist/types/wallet/index.d.ts +51 -50
- package/dist/types/wallet/onchain.d.ts +49 -0
- package/dist/types/wallet/ramps.d.ts +32 -0
- package/dist/types/wallet/serviceWorker/db/vtxo/idb.d.ts +2 -0
- package/dist/types/wallet/serviceWorker/db/vtxo/index.d.ts +2 -0
- package/dist/types/wallet/serviceWorker/request.d.ts +14 -16
- package/dist/types/wallet/serviceWorker/response.d.ts +17 -19
- package/dist/types/wallet/serviceWorker/utils.d.ts +8 -0
- package/dist/types/wallet/serviceWorker/wallet.d.ts +36 -8
- package/dist/types/wallet/serviceWorker/worker.d.ts +7 -3
- package/dist/types/wallet/unroll.d.ts +102 -0
- package/dist/types/wallet/wallet.d.ts +71 -26
- package/package.json +14 -15
- package/dist/cjs/identity/inMemoryKey.js +0 -40
- package/dist/cjs/tree/vtxoTree.js +0 -231
- package/dist/cjs/utils/coinselect.js +0 -73
- package/dist/cjs/utils/psbt.js +0 -137
- package/dist/esm/identity/inMemoryKey.js +0 -36
- package/dist/esm/tree/vtxoTree.js +0 -191
- package/dist/esm/utils/coinselect.js +0 -69
- package/dist/esm/utils/psbt.js +0 -131
- package/dist/types/identity/inMemoryKey.d.ts +0 -12
- package/dist/types/tree/vtxoTree.d.ts +0 -33
- package/dist/types/utils/coinselect.d.ts +0 -21
- package/dist/types/utils/psbt.d.ts +0 -11
|
@@ -1,29 +1,34 @@
|
|
|
1
1
|
import { Output, SettlementEvent } from "../providers/ark";
|
|
2
2
|
import { Identity } from "../identity";
|
|
3
|
-
import { NetworkName } from "../networks";
|
|
4
3
|
import { RelativeTimelock } from "../script/tapscript";
|
|
5
4
|
import { EncodedVtxoScript, TapLeafScript } from "../script/base";
|
|
5
|
+
import { Bytes } from "@scure/btc-signer/utils";
|
|
6
|
+
/**
|
|
7
|
+
* Configuration options for wallet initialization.
|
|
8
|
+
*
|
|
9
|
+
* Defines the parameters required to create and configure a wallet instance,
|
|
10
|
+
* including identity, server URLs, and optional timelock settings.
|
|
11
|
+
* If optional parameters are not provided, the wallet will fetch them from the
|
|
12
|
+
* Ark server.
|
|
13
|
+
*/
|
|
6
14
|
export interface WalletConfig {
|
|
7
|
-
network: NetworkName;
|
|
8
15
|
identity: Identity;
|
|
16
|
+
arkServerUrl: string;
|
|
9
17
|
esploraUrl?: string;
|
|
10
|
-
arkServerUrl?: string;
|
|
11
18
|
arkServerPublicKey?: string;
|
|
12
19
|
boardingTimelock?: RelativeTimelock;
|
|
13
20
|
exitTimelock?: RelativeTimelock;
|
|
14
21
|
}
|
|
15
22
|
export interface WalletBalance {
|
|
16
|
-
|
|
23
|
+
boarding: {
|
|
17
24
|
confirmed: number;
|
|
18
25
|
unconfirmed: number;
|
|
19
26
|
total: number;
|
|
20
27
|
};
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
total: number;
|
|
26
|
-
};
|
|
28
|
+
settled: number;
|
|
29
|
+
preconfirmed: number;
|
|
30
|
+
available: number;
|
|
31
|
+
recoverable: number;
|
|
27
32
|
total: number;
|
|
28
33
|
}
|
|
29
34
|
export interface SendBitcoinParams {
|
|
@@ -37,32 +42,9 @@ export interface Recipient {
|
|
|
37
42
|
amount: number;
|
|
38
43
|
}
|
|
39
44
|
export interface SettleParams {
|
|
40
|
-
inputs:
|
|
41
|
-
tapLeafScript: TapLeafScript;
|
|
42
|
-
} & Outpoint & EncodedVtxoScript))[];
|
|
45
|
+
inputs: ExtendedCoin[];
|
|
43
46
|
outputs: Output[];
|
|
44
47
|
}
|
|
45
|
-
export interface VtxoTaprootAddress {
|
|
46
|
-
address: string;
|
|
47
|
-
scripts: {
|
|
48
|
-
exit: string[];
|
|
49
|
-
forfeit: string[];
|
|
50
|
-
};
|
|
51
|
-
}
|
|
52
|
-
export interface AddressInfo {
|
|
53
|
-
offchain: VtxoTaprootAddress;
|
|
54
|
-
boarding: VtxoTaprootAddress;
|
|
55
|
-
}
|
|
56
|
-
export interface Addresses {
|
|
57
|
-
onchain: string;
|
|
58
|
-
offchain?: string;
|
|
59
|
-
boarding?: string;
|
|
60
|
-
bip21: string;
|
|
61
|
-
}
|
|
62
|
-
export interface TapscriptInfo {
|
|
63
|
-
offchain?: string[];
|
|
64
|
-
boarding?: string[];
|
|
65
|
-
}
|
|
66
48
|
export interface Status {
|
|
67
49
|
confirmed: boolean;
|
|
68
50
|
block_height?: number;
|
|
@@ -70,8 +52,8 @@ export interface Status {
|
|
|
70
52
|
block_time?: number;
|
|
71
53
|
}
|
|
72
54
|
export interface VirtualStatus {
|
|
73
|
-
state: "
|
|
74
|
-
|
|
55
|
+
state: "preconfirmed" | "settled" | "swept" | "spent";
|
|
56
|
+
commitmentTxIds?: string[];
|
|
75
57
|
batchExpiry?: number;
|
|
76
58
|
}
|
|
77
59
|
export interface Outpoint {
|
|
@@ -85,7 +67,10 @@ export interface Coin extends Outpoint {
|
|
|
85
67
|
export interface VirtualCoin extends Coin {
|
|
86
68
|
virtualStatus: VirtualStatus;
|
|
87
69
|
spentBy?: string;
|
|
70
|
+
settledBy?: string;
|
|
71
|
+
arkTxId?: string;
|
|
88
72
|
createdAt: Date;
|
|
73
|
+
isUnrolled: boolean;
|
|
89
74
|
}
|
|
90
75
|
export declare enum TxType {
|
|
91
76
|
TxSent = "SENT",
|
|
@@ -93,8 +78,8 @@ export declare enum TxType {
|
|
|
93
78
|
}
|
|
94
79
|
export interface TxKey {
|
|
95
80
|
boardingTxid: string;
|
|
96
|
-
|
|
97
|
-
|
|
81
|
+
commitmentTxid: string;
|
|
82
|
+
arkTxid: string;
|
|
98
83
|
}
|
|
99
84
|
export interface ArkTransaction {
|
|
100
85
|
key: TxKey;
|
|
@@ -103,21 +88,37 @@ export interface ArkTransaction {
|
|
|
103
88
|
settled: boolean;
|
|
104
89
|
createdAt: number;
|
|
105
90
|
}
|
|
106
|
-
export type
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
91
|
+
export type TapLeaves = {
|
|
92
|
+
forfeitTapLeafScript: TapLeafScript;
|
|
93
|
+
intentTapLeafScript: TapLeafScript;
|
|
94
|
+
};
|
|
95
|
+
export type ExtendedCoin = TapLeaves & EncodedVtxoScript & Coin & {
|
|
96
|
+
extraWitness?: Bytes[];
|
|
97
|
+
};
|
|
98
|
+
export type ExtendedVirtualCoin = TapLeaves & EncodedVtxoScript & VirtualCoin & {
|
|
99
|
+
extraWitness?: Bytes[];
|
|
100
|
+
};
|
|
101
|
+
export declare function isSpendable(vtxo: VirtualCoin): boolean;
|
|
102
|
+
export declare function isRecoverable(vtxo: VirtualCoin): boolean;
|
|
103
|
+
export declare function isSubdust(vtxo: VirtualCoin, dust: bigint): boolean;
|
|
104
|
+
export type GetVtxosFilter = {
|
|
105
|
+
withRecoverable?: boolean;
|
|
106
|
+
withUnrolled?: boolean;
|
|
107
|
+
};
|
|
108
|
+
/**
|
|
109
|
+
* Core wallet interface for Bitcoin transactions with Ark protocol support.
|
|
110
|
+
*
|
|
111
|
+
* This interface defines the contract that all wallet implementations must follow.
|
|
112
|
+
* It provides methods for address management, balance checking, virtual UTXO
|
|
113
|
+
* operations, and transaction management including sending, settling, and unrolling.
|
|
114
|
+
*/
|
|
112
115
|
export interface IWallet {
|
|
113
|
-
getAddress(): Promise<
|
|
114
|
-
|
|
116
|
+
getAddress(): Promise<string>;
|
|
117
|
+
getBoardingAddress(): Promise<string>;
|
|
115
118
|
getBalance(): Promise<WalletBalance>;
|
|
116
|
-
|
|
117
|
-
getVtxos(): Promise<ExtendedVirtualCoin[]>;
|
|
119
|
+
getVtxos(filter?: GetVtxosFilter): Promise<ExtendedVirtualCoin[]>;
|
|
118
120
|
getBoardingUtxos(): Promise<ExtendedCoin[]>;
|
|
119
121
|
getTransactionHistory(): Promise<ArkTransaction[]>;
|
|
120
|
-
sendBitcoin(params: SendBitcoinParams
|
|
122
|
+
sendBitcoin(params: SendBitcoinParams): Promise<string>;
|
|
121
123
|
settle(params?: SettleParams, eventCallback?: (event: SettlementEvent) => void): Promise<string>;
|
|
122
|
-
exit(outpoints?: Outpoint[]): Promise<void>;
|
|
123
124
|
}
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import { P2TR } from "@scure/btc-signer/payment";
|
|
2
|
+
import { Coin, SendBitcoinParams } from ".";
|
|
3
|
+
import { Identity } from "../identity";
|
|
4
|
+
import { Network, NetworkName } from "../networks";
|
|
5
|
+
import { OnchainProvider } from "../providers/onchain";
|
|
6
|
+
import { Transaction } from "@scure/btc-signer";
|
|
7
|
+
import { AnchorBumper } from "../utils/anchor";
|
|
8
|
+
/**
|
|
9
|
+
* Onchain Bitcoin wallet implementation for traditional Bitcoin transactions.
|
|
10
|
+
*
|
|
11
|
+
* This wallet handles regular Bitcoin transactions on the blockchain without
|
|
12
|
+
* using the Ark protocol. It supports P2TR (Pay-to-Taproot) addresses and
|
|
13
|
+
* provides basic Bitcoin wallet functionality.
|
|
14
|
+
*
|
|
15
|
+
* @example
|
|
16
|
+
* ```typescript
|
|
17
|
+
* const wallet = new OnchainWallet(identity, 'mainnet');
|
|
18
|
+
* const balance = await wallet.getBalance();
|
|
19
|
+
* const txid = await wallet.send({
|
|
20
|
+
* address: 'bc1...',
|
|
21
|
+
* amount: 50000
|
|
22
|
+
* });
|
|
23
|
+
* ```
|
|
24
|
+
*/
|
|
25
|
+
export declare class OnchainWallet implements AnchorBumper {
|
|
26
|
+
private identity;
|
|
27
|
+
static MIN_FEE_RATE: number;
|
|
28
|
+
static DUST_AMOUNT: number;
|
|
29
|
+
readonly onchainP2TR: P2TR;
|
|
30
|
+
readonly provider: OnchainProvider;
|
|
31
|
+
readonly network: Network;
|
|
32
|
+
constructor(identity: Identity, network: NetworkName, provider?: OnchainProvider);
|
|
33
|
+
get address(): string;
|
|
34
|
+
getCoins(): Promise<Coin[]>;
|
|
35
|
+
getBalance(): Promise<number>;
|
|
36
|
+
send(params: SendBitcoinParams): Promise<string>;
|
|
37
|
+
bumpP2A(parent: Transaction): Promise<[string, string]>;
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* Select coins to reach a target amount, prioritizing those closer to expiry
|
|
41
|
+
* @param coins List of coins to select from
|
|
42
|
+
* @param targetAmount Target amount to reach in satoshis
|
|
43
|
+
* @param forceChange If true, ensure the coin selection will require a change output
|
|
44
|
+
* @returns Selected coins and change amount, or null if insufficient funds
|
|
45
|
+
*/
|
|
46
|
+
export declare function selectCoins(coins: Coin[], targetAmount: number, forceChange?: boolean): {
|
|
47
|
+
inputs: Coin[];
|
|
48
|
+
changeAmount: bigint;
|
|
49
|
+
};
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { ExtendedCoin, IWallet } from ".";
|
|
2
|
+
import { SettlementEvent } from "../providers/ark";
|
|
3
|
+
/**
|
|
4
|
+
* Ramps is a class wrapping IWallet.settle method to provide a more convenient interface for onboarding and offboarding operations.
|
|
5
|
+
*
|
|
6
|
+
* @example
|
|
7
|
+
* ```typescript
|
|
8
|
+
* const ramps = new Ramps(wallet);
|
|
9
|
+
* await ramps.onboard(); // onboard all boarding utxos
|
|
10
|
+
* await ramps.offboard(myOnchainAddress); // collaborative exit all vtxos to onchain address
|
|
11
|
+
* ```
|
|
12
|
+
*/
|
|
13
|
+
export declare class Ramps {
|
|
14
|
+
readonly wallet: IWallet;
|
|
15
|
+
constructor(wallet: IWallet);
|
|
16
|
+
/**
|
|
17
|
+
* Onboard boarding utxos.
|
|
18
|
+
*
|
|
19
|
+
* @param boardingUtxos - The boarding utxos to onboard. If not provided, all boarding utxos will be used.
|
|
20
|
+
* @param amount - The amount to onboard. If not provided, the total amount of boarding utxos will be onboarded.
|
|
21
|
+
* @param eventCallback - The callback to receive settlement events. optional.
|
|
22
|
+
*/
|
|
23
|
+
onboard(boardingUtxos?: ExtendedCoin[], amount?: bigint, eventCallback?: (event: SettlementEvent) => void): ReturnType<IWallet["settle"]>;
|
|
24
|
+
/**
|
|
25
|
+
* Offboard vtxos, or "collaborative exit" vtxos to onchain address.
|
|
26
|
+
*
|
|
27
|
+
* @param destinationAddress - The destination address to offboard to.
|
|
28
|
+
* @param amount - The amount to offboard. If not provided, the total amount of vtxos will be offboarded.
|
|
29
|
+
* @param eventCallback - The callback to receive settlement events. optional.
|
|
30
|
+
*/
|
|
31
|
+
offboard(destinationAddress: string, amount?: bigint, eventCallback?: (event: SettlementEvent) => void): ReturnType<IWallet["settle"]>;
|
|
32
|
+
}
|
|
@@ -11,6 +11,8 @@ export declare class IndexedDBVtxoRepository implements VtxoRepository {
|
|
|
11
11
|
addOrUpdate(vtxos: ExtendedVirtualCoin[]): Promise<void>;
|
|
12
12
|
deleteAll(): Promise<void>;
|
|
13
13
|
getSpendableVtxos(): Promise<ExtendedVirtualCoin[]>;
|
|
14
|
+
getSweptVtxos(): Promise<ExtendedVirtualCoin[]>;
|
|
15
|
+
getSpentVtxos(): Promise<ExtendedVirtualCoin[]>;
|
|
14
16
|
getAllVtxos(): Promise<{
|
|
15
17
|
spendable: ExtendedVirtualCoin[];
|
|
16
18
|
spent: ExtendedVirtualCoin[];
|
|
@@ -3,6 +3,8 @@ export interface VtxoRepository {
|
|
|
3
3
|
addOrUpdate(vtxos: ExtendedVirtualCoin[]): Promise<void>;
|
|
4
4
|
deleteAll(): Promise<void>;
|
|
5
5
|
getSpendableVtxos(): Promise<ExtendedVirtualCoin[]>;
|
|
6
|
+
getSweptVtxos(): Promise<ExtendedVirtualCoin[]>;
|
|
7
|
+
getSpentVtxos(): Promise<ExtendedVirtualCoin[]>;
|
|
6
8
|
getAllVtxos(): Promise<{
|
|
7
9
|
spendable: ExtendedVirtualCoin[];
|
|
8
10
|
spent: ExtendedVirtualCoin[];
|
|
@@ -1,7 +1,9 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
1
|
+
import { SettleParams, SendBitcoinParams, GetVtxosFilter } from "..";
|
|
2
|
+
/**
|
|
3
|
+
* Request is the namespace that contains the request types for the service worker.
|
|
4
|
+
*/
|
|
3
5
|
export declare namespace Request {
|
|
4
|
-
type Type = "INIT_WALLET" | "SETTLE" | "GET_ADDRESS" | "
|
|
6
|
+
type Type = "INIT_WALLET" | "SETTLE" | "GET_ADDRESS" | "GET_BOARDING_ADDRESS" | "GET_BALANCE" | "GET_VTXOS" | "GET_VIRTUAL_COINS" | "GET_BOARDING_UTXOS" | "SEND_BITCOIN" | "GET_TRANSACTION_HISTORY" | "GET_STATUS" | "CLEAR" | "SIGN";
|
|
5
7
|
interface Base {
|
|
6
8
|
type: Type;
|
|
7
9
|
id: string;
|
|
@@ -11,7 +13,6 @@ export declare namespace Request {
|
|
|
11
13
|
type: "INIT_WALLET";
|
|
12
14
|
privateKey: string;
|
|
13
15
|
arkServerUrl: string;
|
|
14
|
-
network: NetworkName;
|
|
15
16
|
arkServerPublicKey?: string;
|
|
16
17
|
}
|
|
17
18
|
function isInitWallet(message: Base): message is InitWallet;
|
|
@@ -24,20 +25,17 @@ export declare namespace Request {
|
|
|
24
25
|
type: "GET_ADDRESS";
|
|
25
26
|
}
|
|
26
27
|
function isGetAddress(message: Base): message is GetAddress;
|
|
27
|
-
interface
|
|
28
|
-
type: "
|
|
28
|
+
interface GetBoardingAddress extends Base {
|
|
29
|
+
type: "GET_BOARDING_ADDRESS";
|
|
29
30
|
}
|
|
30
|
-
function
|
|
31
|
+
function isGetBoardingAddress(message: Base): message is GetBoardingAddress;
|
|
31
32
|
interface GetBalance extends Base {
|
|
32
33
|
type: "GET_BALANCE";
|
|
33
34
|
}
|
|
34
35
|
function isGetBalance(message: Base): message is GetBalance;
|
|
35
|
-
interface GetCoins extends Base {
|
|
36
|
-
type: "GET_COINS";
|
|
37
|
-
}
|
|
38
|
-
function isGetCoins(message: Base): message is GetCoins;
|
|
39
36
|
interface GetVtxos extends Base {
|
|
40
37
|
type: "GET_VTXOS";
|
|
38
|
+
filter?: GetVtxosFilter;
|
|
41
39
|
}
|
|
42
40
|
function isGetVtxos(message: Base): message is GetVtxos;
|
|
43
41
|
interface GetVirtualCoins extends Base {
|
|
@@ -51,7 +49,6 @@ export declare namespace Request {
|
|
|
51
49
|
interface SendBitcoin extends Base {
|
|
52
50
|
type: "SEND_BITCOIN";
|
|
53
51
|
params: SendBitcoinParams;
|
|
54
|
-
zeroFee?: boolean;
|
|
55
52
|
}
|
|
56
53
|
function isSendBitcoin(message: Base): message is SendBitcoin;
|
|
57
54
|
interface GetTransactionHistory extends Base {
|
|
@@ -65,9 +62,10 @@ export declare namespace Request {
|
|
|
65
62
|
interface Clear extends Base {
|
|
66
63
|
type: "CLEAR";
|
|
67
64
|
}
|
|
68
|
-
interface
|
|
69
|
-
type: "
|
|
70
|
-
|
|
65
|
+
interface Sign extends Base {
|
|
66
|
+
type: "SIGN";
|
|
67
|
+
tx: string;
|
|
68
|
+
inputIndexes?: number[];
|
|
71
69
|
}
|
|
72
|
-
function
|
|
70
|
+
function isSign(message: Base): message is Sign;
|
|
73
71
|
}
|
|
@@ -1,7 +1,10 @@
|
|
|
1
|
-
import { WalletBalance,
|
|
1
|
+
import { WalletBalance, VirtualCoin, ArkTransaction, IWallet } from "..";
|
|
2
2
|
import { SettlementEvent } from "../../providers/ark";
|
|
3
|
+
/**
|
|
4
|
+
* Response is the namespace that contains the response types for the service worker.
|
|
5
|
+
*/
|
|
3
6
|
export declare namespace Response {
|
|
4
|
-
type Type = "WALLET_INITIALIZED" | "SETTLE_EVENT" | "SETTLE_SUCCESS" | "ADDRESS" | "
|
|
7
|
+
type Type = "WALLET_INITIALIZED" | "SETTLE_EVENT" | "SETTLE_SUCCESS" | "ADDRESS" | "BOARDING_ADDRESS" | "BALANCE" | "VTXOS" | "VIRTUAL_COINS" | "BOARDING_UTXOS" | "SEND_BITCOIN_SUCCESS" | "TRANSACTION_HISTORY" | "WALLET_STATUS" | "ERROR" | "CLEAR_RESPONSE" | "SIGN_SUCCESS";
|
|
5
8
|
interface Base {
|
|
6
9
|
type: Type;
|
|
7
10
|
success: boolean;
|
|
@@ -30,17 +33,17 @@ export declare namespace Response {
|
|
|
30
33
|
interface Address extends Base {
|
|
31
34
|
type: "ADDRESS";
|
|
32
35
|
success: true;
|
|
33
|
-
|
|
36
|
+
address: string;
|
|
34
37
|
}
|
|
35
38
|
function isAddress(response: Base): response is Address;
|
|
36
|
-
function
|
|
37
|
-
|
|
38
|
-
|
|
39
|
+
function isBoardingAddress(response: Base): response is BoardingAddress;
|
|
40
|
+
function address(id: string, address: string): Address;
|
|
41
|
+
interface BoardingAddress extends Base {
|
|
42
|
+
type: "BOARDING_ADDRESS";
|
|
39
43
|
success: true;
|
|
40
|
-
|
|
44
|
+
address: string;
|
|
41
45
|
}
|
|
42
|
-
function
|
|
43
|
-
function addressInfo(id: string, addressInfo: WalletAddressInfo): AddressInfo;
|
|
46
|
+
function boardingAddress(id: string, address: string): BoardingAddress;
|
|
44
47
|
interface Balance extends Base {
|
|
45
48
|
type: "BALANCE";
|
|
46
49
|
success: true;
|
|
@@ -48,13 +51,6 @@ export declare namespace Response {
|
|
|
48
51
|
}
|
|
49
52
|
function isBalance(response: Base): response is Balance;
|
|
50
53
|
function balance(id: string, balance: WalletBalance): Balance;
|
|
51
|
-
interface Coins extends Base {
|
|
52
|
-
type: "COINS";
|
|
53
|
-
success: true;
|
|
54
|
-
coins: Coin[];
|
|
55
|
-
}
|
|
56
|
-
function isCoins(response: Base): response is Coins;
|
|
57
|
-
function coins(id: string, coins: Coin[]): Coins;
|
|
58
54
|
interface Vtxos extends Base {
|
|
59
55
|
type: "VTXOS";
|
|
60
56
|
success: true;
|
|
@@ -104,9 +100,11 @@ export declare namespace Response {
|
|
|
104
100
|
}
|
|
105
101
|
function isClearResponse(response: Base): response is ClearResponse;
|
|
106
102
|
function clearResponse(id: string, success: boolean): ClearResponse;
|
|
107
|
-
interface
|
|
108
|
-
type: "
|
|
103
|
+
interface SignSuccess extends Base {
|
|
104
|
+
type: "SIGN_SUCCESS";
|
|
109
105
|
success: true;
|
|
106
|
+
tx: string;
|
|
110
107
|
}
|
|
111
|
-
function
|
|
108
|
+
function signSuccess(id: string, tx: string): SignSuccess;
|
|
109
|
+
function isSignSuccess(response: Base): response is SignSuccess;
|
|
112
110
|
}
|
|
@@ -1 +1,9 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* setupServiceWorker sets up the service worker.
|
|
3
|
+
* @param path - the path to the service worker script
|
|
4
|
+
* @example
|
|
5
|
+
* ```typescript
|
|
6
|
+
* const serviceWorker = await setupServiceWorker("/service-worker.js");
|
|
7
|
+
* ```
|
|
8
|
+
*/
|
|
1
9
|
export declare function setupServiceWorker(path: string): Promise<ServiceWorker>;
|
|
@@ -1,8 +1,35 @@
|
|
|
1
|
-
import { IWallet, WalletBalance, SendBitcoinParams, SettleParams,
|
|
1
|
+
import { IWallet, WalletBalance, SendBitcoinParams, SettleParams, ArkTransaction, WalletConfig, ExtendedCoin, ExtendedVirtualCoin, GetVtxosFilter } from "..";
|
|
2
2
|
import { Response } from "./response";
|
|
3
3
|
import { SettlementEvent } from "../../providers/ark";
|
|
4
|
-
|
|
4
|
+
import { Identity } from "../../identity";
|
|
5
|
+
import { SignerSession } from "../../tree/signingSession";
|
|
6
|
+
import { Transaction } from "@scure/btc-signer";
|
|
7
|
+
/**
|
|
8
|
+
* Service Worker-based wallet implementation for browser environments.
|
|
9
|
+
*
|
|
10
|
+
* This wallet uses a service worker as a backend to handle wallet logic,
|
|
11
|
+
* providing secure key storage and transaction signing in web applications.
|
|
12
|
+
* The service worker runs in a separate thread and can persist data between
|
|
13
|
+
* browser sessions.
|
|
14
|
+
*
|
|
15
|
+
* @example
|
|
16
|
+
* ```typescript
|
|
17
|
+
* // Create and initialize the service worker wallet
|
|
18
|
+
* const serviceWorker = await setupServiceWorker("/service-worker.js");
|
|
19
|
+
* const wallet = new ServiceWorkerWallet(serviceWorker);
|
|
20
|
+
* await wallet.init({
|
|
21
|
+
* privateKey: 'your_private_key_hex',
|
|
22
|
+
* arkServerUrl: 'https://ark.example.com'
|
|
23
|
+
* });
|
|
24
|
+
*
|
|
25
|
+
* // Use like any other wallet
|
|
26
|
+
* const address = await wallet.getAddress();
|
|
27
|
+
* const balance = await wallet.getBalance();
|
|
28
|
+
* ```
|
|
29
|
+
*/
|
|
30
|
+
export declare class ServiceWorkerWallet implements IWallet, Identity {
|
|
5
31
|
readonly serviceWorker: ServiceWorker;
|
|
32
|
+
private cachedXOnlyPublicKey;
|
|
6
33
|
constructor(serviceWorker: ServiceWorker);
|
|
7
34
|
getStatus(): Promise<Response.WalletStatus["status"]>;
|
|
8
35
|
init(config: Omit<WalletConfig, "identity"> & {
|
|
@@ -10,14 +37,15 @@ export declare class ServiceWorkerWallet implements IWallet {
|
|
|
10
37
|
}, failIfInitialized?: boolean): Promise<void>;
|
|
11
38
|
clear(): Promise<void>;
|
|
12
39
|
private sendMessage;
|
|
13
|
-
getAddress(): Promise<
|
|
14
|
-
|
|
40
|
+
getAddress(): Promise<string>;
|
|
41
|
+
getBoardingAddress(): Promise<string>;
|
|
15
42
|
getBalance(): Promise<WalletBalance>;
|
|
16
|
-
|
|
17
|
-
getVtxos(): Promise<ExtendedVirtualCoin[]>;
|
|
43
|
+
getVtxos(filter?: GetVtxosFilter): Promise<ExtendedVirtualCoin[]>;
|
|
18
44
|
getBoardingUtxos(): Promise<ExtendedCoin[]>;
|
|
19
|
-
sendBitcoin(params: SendBitcoinParams
|
|
45
|
+
sendBitcoin(params: SendBitcoinParams): Promise<string>;
|
|
20
46
|
settle(params?: SettleParams, callback?: (event: SettlementEvent) => void): Promise<string>;
|
|
21
47
|
getTransactionHistory(): Promise<ArkTransaction[]>;
|
|
22
|
-
|
|
48
|
+
xOnlyPublicKey(): Uint8Array;
|
|
49
|
+
signerSession(): SignerSession;
|
|
50
|
+
sign(tx: Transaction, inputIndexes?: number[]): Promise<Transaction>;
|
|
23
51
|
}
|
|
@@ -1,9 +1,14 @@
|
|
|
1
1
|
import { VtxoRepository } from "./db/vtxo";
|
|
2
|
+
/**
|
|
3
|
+
* Worker is a class letting to interact with ServiceWorkerWallet from the client
|
|
4
|
+
* it aims to be run in a service worker context
|
|
5
|
+
*/
|
|
2
6
|
export declare class Worker {
|
|
3
7
|
private readonly vtxoRepository;
|
|
4
8
|
private readonly messageCallback;
|
|
5
9
|
private wallet;
|
|
6
10
|
private arkProvider;
|
|
11
|
+
private indexerProvider;
|
|
7
12
|
private vtxoSubscription;
|
|
8
13
|
constructor(vtxoRepository?: VtxoRepository, messageCallback?: (message: ExtendableMessageEvent) => void);
|
|
9
14
|
start(withServiceWorkerUpdate?: boolean): Promise<void>;
|
|
@@ -15,13 +20,12 @@ export declare class Worker {
|
|
|
15
20
|
private handleSettle;
|
|
16
21
|
private handleSendBitcoin;
|
|
17
22
|
private handleGetAddress;
|
|
18
|
-
private
|
|
23
|
+
private handleGetBoardingAddress;
|
|
19
24
|
private handleGetBalance;
|
|
20
|
-
private handleGetCoins;
|
|
21
25
|
private handleGetVtxos;
|
|
22
26
|
private handleGetBoardingUtxos;
|
|
23
27
|
private handleGetTransactionHistory;
|
|
24
28
|
private handleGetStatus;
|
|
25
|
-
private
|
|
29
|
+
private handleSign;
|
|
26
30
|
private handleMessage;
|
|
27
31
|
}
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
import { Transaction } from "@scure/btc-signer";
|
|
2
|
+
import { ChainTx, IndexerProvider } from "../providers/indexer";
|
|
3
|
+
import { AnchorBumper } from "../utils/anchor";
|
|
4
|
+
import { OnchainProvider } from "../providers/onchain";
|
|
5
|
+
import { Outpoint } from ".";
|
|
6
|
+
import { Wallet } from "./wallet";
|
|
7
|
+
export declare namespace Unroll {
|
|
8
|
+
enum StepType {
|
|
9
|
+
UNROLL = 0,
|
|
10
|
+
WAIT = 1,
|
|
11
|
+
DONE = 2
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* Unroll step where the transaction has to be broadcasted in a 1C1P package
|
|
15
|
+
*/
|
|
16
|
+
type UnrollStep = {
|
|
17
|
+
tx: Transaction;
|
|
18
|
+
};
|
|
19
|
+
/**
|
|
20
|
+
* Wait step where the transaction has to be confirmed onchain
|
|
21
|
+
*/
|
|
22
|
+
type WaitStep = {
|
|
23
|
+
txid: string;
|
|
24
|
+
};
|
|
25
|
+
/**
|
|
26
|
+
* Done step where the unrolling process is complete
|
|
27
|
+
*/
|
|
28
|
+
type DoneStep = {
|
|
29
|
+
vtxoTxid: string;
|
|
30
|
+
};
|
|
31
|
+
type Step = ({
|
|
32
|
+
type: StepType.DONE;
|
|
33
|
+
} & DoneStep) | ({
|
|
34
|
+
type: StepType.UNROLL;
|
|
35
|
+
} & UnrollStep) | ({
|
|
36
|
+
type: StepType.WAIT;
|
|
37
|
+
} & WaitStep);
|
|
38
|
+
/**
|
|
39
|
+
* Manages the unrolling process of a VTXO back to the Bitcoin blockchain.
|
|
40
|
+
*
|
|
41
|
+
* The Session class implements an async iterator that processes the unrolling steps:
|
|
42
|
+
* 1. **WAIT**: Waits for a transaction to be confirmed onchain (if it's in mempool)
|
|
43
|
+
* 2. **UNROLL**: Broadcasts the next transaction in the chain to the blockchain
|
|
44
|
+
* 3. **DONE**: Indicates the unrolling process is complete
|
|
45
|
+
*
|
|
46
|
+
* The unrolling process works by traversing the transaction chain from the root (most recent)
|
|
47
|
+
* to the leaf (oldest), broadcasting each transaction that isn't already onchain.
|
|
48
|
+
*
|
|
49
|
+
* @example
|
|
50
|
+
* ```typescript
|
|
51
|
+
* const session = await Unroll.Session.create(vtxoOutpoint, bumper, explorer, indexer);
|
|
52
|
+
*
|
|
53
|
+
* // iterate over the steps
|
|
54
|
+
* for await (const doneStep of session) {
|
|
55
|
+
* switch (doneStep.type) {
|
|
56
|
+
* case Unroll.StepType.WAIT:
|
|
57
|
+
* console.log(`Transaction ${doneStep.txid} confirmed`);
|
|
58
|
+
* break;
|
|
59
|
+
* case Unroll.StepType.UNROLL:
|
|
60
|
+
* console.log(`Broadcasting transaction ${doneStep.tx.id}`);
|
|
61
|
+
* break;
|
|
62
|
+
* case Unroll.StepType.DONE:
|
|
63
|
+
* console.log(`Unrolling complete for VTXO ${doneStep.vtxoTxid}`);
|
|
64
|
+
* break;
|
|
65
|
+
* }
|
|
66
|
+
* }
|
|
67
|
+
* ```
|
|
68
|
+
**/
|
|
69
|
+
class Session implements AsyncIterable<Step> {
|
|
70
|
+
readonly toUnroll: Outpoint & {
|
|
71
|
+
chain: ChainTx[];
|
|
72
|
+
};
|
|
73
|
+
readonly bumper: AnchorBumper;
|
|
74
|
+
readonly explorer: OnchainProvider;
|
|
75
|
+
readonly indexer: IndexerProvider;
|
|
76
|
+
constructor(toUnroll: Outpoint & {
|
|
77
|
+
chain: ChainTx[];
|
|
78
|
+
}, bumper: AnchorBumper, explorer: OnchainProvider, indexer: IndexerProvider);
|
|
79
|
+
static create(toUnroll: Outpoint, bumper: AnchorBumper, explorer: OnchainProvider, indexer: IndexerProvider): Promise<Session>;
|
|
80
|
+
/**
|
|
81
|
+
* Get the next step to be executed
|
|
82
|
+
* @returns The next step to be executed + the function to execute it
|
|
83
|
+
*/
|
|
84
|
+
next(): Promise<Step & {
|
|
85
|
+
do: () => Promise<void>;
|
|
86
|
+
}>;
|
|
87
|
+
/**
|
|
88
|
+
* Iterate over the steps to be executed and execute them
|
|
89
|
+
* @returns An async iterator over the executed steps
|
|
90
|
+
*/
|
|
91
|
+
[Symbol.asyncIterator](): AsyncIterator<Step>;
|
|
92
|
+
}
|
|
93
|
+
/**
|
|
94
|
+
* Complete the unroll of a VTXO by broadcasting the transaction that spends the CSV path.
|
|
95
|
+
* @param wallet the wallet owning the VTXO(s)
|
|
96
|
+
* @param vtxoTxids the txids of the VTXO(s) to complete unroll
|
|
97
|
+
* @param outputAddress the address to send the unrolled funds to
|
|
98
|
+
* @throws if the VTXO(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
|
|
99
|
+
* @returns the txid of the transaction spending the unrolled funds
|
|
100
|
+
*/
|
|
101
|
+
function completeUnroll(wallet: Wallet, vtxoTxids: string[], outputAddress: string): Promise<string>;
|
|
102
|
+
}
|