@arkade-os/sdk 0.4.15 → 0.4.17
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 +60 -28
- 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 +140 -77
- 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 +60 -28
- 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 +140 -77
- 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 +39 -65
- 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
|
@@ -7,6 +7,7 @@ import { ReadonlyWallet, Wallet } from '../wallet/wallet.js';
|
|
|
7
7
|
import { hex } from "@scure/base";
|
|
8
8
|
import { MessageBusNotInitializedError, ServiceWorkerTimeoutError, } from './errors.js';
|
|
9
9
|
export class MessageBus {
|
|
10
|
+
/** Create the service-worker message bus with repositories and handler configuration. */
|
|
10
11
|
constructor(walletRepository, contractRepository, { messageHandlers, tickIntervalMs = 10000, messageTimeoutMs = 30000, debug = false, buildServices, }) {
|
|
11
12
|
this.walletRepository = walletRepository;
|
|
12
13
|
this.contractRepository = contractRepository;
|
|
@@ -22,6 +23,7 @@ export class MessageBus {
|
|
|
22
23
|
this.debug = debug;
|
|
23
24
|
this.buildServicesFn = buildServices ?? this.buildServices.bind(this);
|
|
24
25
|
}
|
|
26
|
+
/** Start the message bus and attach service-worker event listeners. */
|
|
25
27
|
async start() {
|
|
26
28
|
if (this.running)
|
|
27
29
|
return;
|
|
@@ -42,6 +44,7 @@ export class MessageBus {
|
|
|
42
44
|
}
|
|
43
45
|
});
|
|
44
46
|
}
|
|
47
|
+
/** Stop the message bus, cancel ticks, and stop all registered handlers. */
|
|
45
48
|
async stop() {
|
|
46
49
|
if (this.debug)
|
|
47
50
|
console.log("MessageBus stopping");
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { IntentFeeConfig, OffchainInput, OnchainInput, FeeOutput, FeeAmount } from "./types.js";
|
|
2
2
|
/**
|
|
3
|
-
* Estimator evaluates CEL expressions to calculate fees for
|
|
3
|
+
* Estimator evaluates CEL expressions to calculate fees for Arkade intents
|
|
4
4
|
*/
|
|
5
5
|
export declare class Estimator {
|
|
6
6
|
readonly config: IntentFeeConfig;
|
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* FeeAmount is a wrapper around a number that represents a fee amount in satoshis floating point.
|
|
3
3
|
* @param value - The fee amount in floating point.
|
|
4
|
-
* @method satoshis - Returns the fee amount in satoshis as a integer.
|
|
5
4
|
* @example
|
|
6
5
|
* const fee = new FeeAmount(1.23456789);
|
|
7
6
|
* console.log(fee.value); // 1.23456789
|
|
@@ -11,7 +10,9 @@ export declare class FeeAmount {
|
|
|
11
10
|
readonly value: number;
|
|
12
11
|
static ZERO: FeeAmount;
|
|
13
12
|
constructor(value: number);
|
|
13
|
+
/** Returns the fee amount rounded up to whole satoshis. */
|
|
14
14
|
get satoshis(): number;
|
|
15
|
+
/** Add two fee amounts together. */
|
|
15
16
|
add(other: FeeAmount): FeeAmount;
|
|
16
17
|
}
|
|
17
18
|
export interface IntentFeeConfig {
|
|
@@ -2,10 +2,12 @@ import { Bytes } from "@scure/btc-signer/utils.js";
|
|
|
2
2
|
import { TapLeafScript, VtxoScript } from "../script/base";
|
|
3
3
|
import { ExtendedCoin, Status } from "../wallet";
|
|
4
4
|
/**
|
|
5
|
-
* ArkNotes are special virtual
|
|
6
|
-
* and spent without requiring any transactions.
|
|
7
|
-
*
|
|
8
|
-
* preimage and value.
|
|
5
|
+
* ArkNotes are special virtual outputs in the Arkade protocol that
|
|
6
|
+
* can be created and spent without requiring any transactions.
|
|
7
|
+
* The server mints them, and they are encoded as base58 strings
|
|
8
|
+
* with a human-readable prefix, a preimage and a value.
|
|
9
|
+
*
|
|
10
|
+
* @see VtxoScript
|
|
9
11
|
*
|
|
10
12
|
* @example
|
|
11
13
|
* ```typescript
|
|
@@ -29,6 +31,7 @@ export declare class ArkNote implements ExtendedCoin {
|
|
|
29
31
|
static readonly Length: number;
|
|
30
32
|
static readonly FakeOutpointIndex = 0;
|
|
31
33
|
readonly vtxoScript: VtxoScript;
|
|
34
|
+
/** Hashlock script backing the note. */
|
|
32
35
|
readonly txid: string;
|
|
33
36
|
readonly vout = 0;
|
|
34
37
|
readonly forfeitTapLeafScript: TapLeafScript;
|
|
@@ -36,9 +39,46 @@ export declare class ArkNote implements ExtendedCoin {
|
|
|
36
39
|
readonly tapTree: Bytes;
|
|
37
40
|
readonly status: Status;
|
|
38
41
|
readonly extraWitness?: Bytes[] | undefined;
|
|
42
|
+
/**
|
|
43
|
+
* Create an ArkNote from a preimage and value.
|
|
44
|
+
*
|
|
45
|
+
* @param preimage - 32-byte preimage revealed to spend the note
|
|
46
|
+
* @param value - Note value in satoshis
|
|
47
|
+
* @param HRP - Optional human-readable prefix for string encoding
|
|
48
|
+
*/
|
|
39
49
|
constructor(preimage: Uint8Array, value: number, HRP?: string);
|
|
50
|
+
/**
|
|
51
|
+
* Encode the note as raw bytes.
|
|
52
|
+
*
|
|
53
|
+
* @returns Serialized note bytes
|
|
54
|
+
* @see decode
|
|
55
|
+
*/
|
|
40
56
|
encode(): Uint8Array;
|
|
57
|
+
/**
|
|
58
|
+
* Decode a note from raw bytes.
|
|
59
|
+
*
|
|
60
|
+
* @param data - Serialized note bytes
|
|
61
|
+
* @param hrp - Human-readable prefix expected for future string encoding
|
|
62
|
+
* @returns Decoded ArkNote
|
|
63
|
+
* @throws Error if the payload length is invalid
|
|
64
|
+
* @see encode
|
|
65
|
+
*/
|
|
41
66
|
static decode(data: Uint8Array, hrp?: string): ArkNote;
|
|
67
|
+
/**
|
|
68
|
+
* Decode a note from its base58 string form.
|
|
69
|
+
*
|
|
70
|
+
* @param noteStr - Base58-encoded note string
|
|
71
|
+
* @param hrp - Human-readable prefix expected on the note string
|
|
72
|
+
* @returns Decoded ArkNote
|
|
73
|
+
* @throws Error if the prefix or base58 payload is invalid
|
|
74
|
+
* @see toString
|
|
75
|
+
*/
|
|
42
76
|
static fromString(noteStr: string, hrp?: string): ArkNote;
|
|
77
|
+
/**
|
|
78
|
+
* Encode the note to its human-readable base58 string form.
|
|
79
|
+
*
|
|
80
|
+
* @returns Base58-encoded note string
|
|
81
|
+
* @see fromString
|
|
82
|
+
*/
|
|
43
83
|
toString(): string;
|
|
44
84
|
}
|
|
@@ -8,7 +8,7 @@ import type { Identity } from "../identity";
|
|
|
8
8
|
*
|
|
9
9
|
* Reuses the same toSpend/toSign transaction construction as Intent proofs,
|
|
10
10
|
* but with the standard BIP-322 tagged hash ("BIP0322-signed-message")
|
|
11
|
-
* instead of the
|
|
11
|
+
* instead of the Arkade-specific tag.
|
|
12
12
|
*
|
|
13
13
|
* @see https://github.com/bitcoin/bips/blob/master/bip-0322.mediawiki
|
|
14
14
|
*
|
|
@@ -5,7 +5,7 @@ import { Contract } from "./types";
|
|
|
5
5
|
* Format: arkcontract={type}&{key1}={value1}&{key2}={value2}...
|
|
6
6
|
*
|
|
7
7
|
* This format is compatible with NArk and allows contracts to be
|
|
8
|
-
* shared/imported across different
|
|
8
|
+
* shared/imported across different Arkade SDKs.
|
|
9
9
|
*
|
|
10
10
|
* @example
|
|
11
11
|
* ```typescript
|
|
@@ -4,45 +4,6 @@ import { Contract, ContractEventCallback, ContractState, ContractWithVtxos, GetC
|
|
|
4
4
|
import { ContractWatcherConfig } from "./contractWatcher";
|
|
5
5
|
import { VirtualCoin } from "../wallet";
|
|
6
6
|
import { ContractRepository } from "../repositories";
|
|
7
|
-
/**
|
|
8
|
-
* Contract lifecycle and VTXO orchestration API.
|
|
9
|
-
*
|
|
10
|
-
* Responsibilities:
|
|
11
|
-
* - Create and persist contracts
|
|
12
|
-
* - Query stored contracts (optionally with their VTXOs)
|
|
13
|
-
* - Provide spendable path selection for a contract
|
|
14
|
-
* - Emit contract-related events (VTXO received/spent, expiry, connection reset)
|
|
15
|
-
*
|
|
16
|
-
* Notes:
|
|
17
|
-
* - Implementations typically start watching automatically during initialization
|
|
18
|
-
* (so `onContractEvent()` is just for subscribing).
|
|
19
|
-
*
|
|
20
|
-
* @example
|
|
21
|
-
* ```typescript
|
|
22
|
-
* const manager = await ContractManager.create({
|
|
23
|
-
* indexerProvider,
|
|
24
|
-
* contractRepository,
|
|
25
|
-
* walletRepository,
|
|
26
|
-
* getDefaultAddress,
|
|
27
|
-
* });
|
|
28
|
-
*
|
|
29
|
-
* const unsubscribe = manager.onContractEvent((event) => {
|
|
30
|
-
* console.log(event.type, event.timestamp);
|
|
31
|
-
* });
|
|
32
|
-
*
|
|
33
|
-
* const contract = await manager.createContract({
|
|
34
|
-
* label: "Lightning Receive",
|
|
35
|
-
* type: "vhtlc",
|
|
36
|
-
* params: { sender: "ab12...", receiver: "cd34..." },
|
|
37
|
-
* script: "5120...",
|
|
38
|
-
* address: "tark1...",
|
|
39
|
-
* });
|
|
40
|
-
*
|
|
41
|
-
* // Later:
|
|
42
|
-
* unsubscribe();
|
|
43
|
-
* manager.dispose();
|
|
44
|
-
* ```
|
|
45
|
-
*/
|
|
46
7
|
export type RefreshVtxosOptions = {
|
|
47
8
|
scripts?: string[];
|
|
48
9
|
after?: number;
|
|
@@ -70,9 +31,9 @@ export interface IContractManager extends Disposable {
|
|
|
70
31
|
*/
|
|
71
32
|
getContracts(filter?: GetContractsFilter): Promise<Contract[]>;
|
|
72
33
|
/**
|
|
73
|
-
* List contracts and
|
|
34
|
+
* List contracts and their current virtual outputs.
|
|
74
35
|
*
|
|
75
|
-
* If no filter is provided, returns all contracts with their
|
|
36
|
+
* If no filter is provided, returns all contracts with their virtual outputs.
|
|
76
37
|
*/
|
|
77
38
|
getContractsWithVtxos(filter?: GetContractsFilter): Promise<ContractWithVtxos[]>;
|
|
78
39
|
/**
|
|
@@ -108,7 +69,7 @@ export interface IContractManager extends Disposable {
|
|
|
108
69
|
*/
|
|
109
70
|
onContractEvent(callback: ContractEventCallback): () => void;
|
|
110
71
|
/**
|
|
111
|
-
* Force a
|
|
72
|
+
* Force a virtual output refresh from the indexer.
|
|
112
73
|
*
|
|
113
74
|
* Without options, refreshes all contracts from scratch.
|
|
114
75
|
* With options, narrows the refresh to specific scripts and/or a time window.
|
|
@@ -129,7 +90,7 @@ export interface IContractManager extends Disposable {
|
|
|
129
90
|
export type GetSpendablePathsOptions = {
|
|
130
91
|
/** The contract script */
|
|
131
92
|
contractScript: string;
|
|
132
|
-
/** The specific
|
|
93
|
+
/** The specific virtual output being evaluated */
|
|
133
94
|
vtxo: VirtualCoin;
|
|
134
95
|
/** Whether collaborative spending is available (default: true) */
|
|
135
96
|
collaborative?: boolean;
|
|
@@ -155,10 +116,8 @@ export interface ContractManagerConfig {
|
|
|
155
116
|
indexerProvider: IndexerProvider;
|
|
156
117
|
/** The contract repository for persistence */
|
|
157
118
|
contractRepository: ContractRepository;
|
|
158
|
-
/** The wallet repository for
|
|
119
|
+
/** The wallet repository for virtual output storage (single source of truth) */
|
|
159
120
|
walletRepository: WalletRepository;
|
|
160
|
-
/** Function to get the wallet's default Ark address */
|
|
161
|
-
getDefaultAddress: () => Promise<string>;
|
|
162
121
|
/** Watcher configuration */
|
|
163
122
|
watcherConfig?: Partial<ContractWatcherConfig>;
|
|
164
123
|
}
|
|
@@ -172,38 +131,48 @@ export type CreateContractParams = Omit<Contract, "createdAt" | "state"> & {
|
|
|
172
131
|
/**
|
|
173
132
|
* Central manager for contract lifecycle and operations.
|
|
174
133
|
*
|
|
175
|
-
*
|
|
176
|
-
* -
|
|
177
|
-
* -
|
|
178
|
-
* -
|
|
134
|
+
* Responsibilities:
|
|
135
|
+
* - Create and persist contracts
|
|
136
|
+
* - Query stored contracts (optionally with their virtual outputs)
|
|
137
|
+
* - Provide spendable path selection for a contract
|
|
138
|
+
* - Emit contract-related events (virtual output received/spent/expired, connection reset)
|
|
139
|
+
*
|
|
140
|
+
* Notes:
|
|
141
|
+
* - Implementations typically start watching automatically during initialization
|
|
142
|
+
* (so `onContractEvent()` is just for subscribing).
|
|
179
143
|
*
|
|
180
144
|
* @example
|
|
181
145
|
* ```typescript
|
|
182
|
-
* const manager =
|
|
146
|
+
* const manager = await ContractManager.create({
|
|
183
147
|
* indexerProvider: wallet.indexerProvider,
|
|
184
148
|
* contractRepository: wallet.contractRepository,
|
|
185
|
-
* getDefaultAddress: () => wallet.getAddress(),
|
|
186
149
|
* });
|
|
187
150
|
*
|
|
188
|
-
* // Initialize (loads persisted contracts)
|
|
189
|
-
* await manager.initialize();
|
|
190
|
-
*
|
|
191
151
|
* // Create a new VHTLC contract
|
|
192
152
|
* const contract = await manager.createContract({
|
|
193
153
|
* label: "Lightning Receive",
|
|
194
154
|
* type: "vhtlc",
|
|
195
|
-
* params: { sender: "
|
|
155
|
+
* params: { sender: "ark1q...", receiver: "ark1q...", ... },
|
|
196
156
|
* script: "5120...",
|
|
197
|
-
* address: "
|
|
157
|
+
* address: "ark1q...",
|
|
198
158
|
* });
|
|
199
159
|
*
|
|
200
160
|
* // Start watching for events
|
|
201
|
-
* const
|
|
161
|
+
* const unsubscribe = manager.onContractEvent((event) => {
|
|
202
162
|
* console.log(`${event.type} on ${event.contractScript}`);
|
|
203
163
|
* });
|
|
204
164
|
*
|
|
165
|
+
* // Query contracts together with their current virtual outputs
|
|
166
|
+
* const contractsWithVtxos = await manager.getContractsWithVtxos();
|
|
167
|
+
*
|
|
205
168
|
* // Get balance across all contracts
|
|
206
|
-
* const balances =
|
|
169
|
+
* const balances = contractsWithVtxos.flatMap(({vtxos}) => vtxos).reduce((acc, vtxo) => acc + vtxo.value, 0)
|
|
170
|
+
*
|
|
171
|
+
* // Later: unsubscribe from events
|
|
172
|
+
* unsubscribe();
|
|
173
|
+
*
|
|
174
|
+
* // Clean up
|
|
175
|
+
* manager.dispose();
|
|
207
176
|
* ```
|
|
208
177
|
*/
|
|
209
178
|
export declare class ContractManager implements IContractManager {
|
|
@@ -212,13 +181,14 @@ export declare class ContractManager implements IContractManager {
|
|
|
212
181
|
private initialized;
|
|
213
182
|
private eventCallbacks;
|
|
214
183
|
private stopWatcherFn?;
|
|
184
|
+
private syncVtxosCallInflight?;
|
|
215
185
|
private constructor();
|
|
216
186
|
/**
|
|
217
187
|
* Static factory method for creating a new ContractManager.
|
|
218
188
|
* Initialize the manager by loading persisted contracts and starting to watch.
|
|
219
189
|
*
|
|
220
190
|
* After initialization, the manager automatically watches all active contracts
|
|
221
|
-
* and contracts with
|
|
191
|
+
* and contracts with virtual outputs. Use `onContractEvent()` to register event callbacks.
|
|
222
192
|
*
|
|
223
193
|
* @param config ContractManagerConfig
|
|
224
194
|
*/
|
|
@@ -279,10 +249,14 @@ export declare class ContractManager implements IContractManager {
|
|
|
279
249
|
/**
|
|
280
250
|
* Get currently spendable paths for a contract.
|
|
281
251
|
*
|
|
282
|
-
* @param contractScript - The contract script
|
|
283
252
|
* @param options - Options for getting spendable paths
|
|
284
253
|
*/
|
|
285
254
|
getSpendablePaths(options: GetSpendablePathsOptions): Promise<PathSelection[]>;
|
|
255
|
+
/**
|
|
256
|
+
* Get every currently valid spending path for a contract.
|
|
257
|
+
*
|
|
258
|
+
* @param options - Options for getting spending paths
|
|
259
|
+
*/
|
|
286
260
|
getAllSpendingPaths(options: GetAllSpendingPathsOptions): Promise<PathSelection[]>;
|
|
287
261
|
/**
|
|
288
262
|
* Register a callback for contract events.
|
|
@@ -305,7 +279,7 @@ export declare class ContractManager implements IContractManager {
|
|
|
305
279
|
*/
|
|
306
280
|
onContractEvent(callback: ContractEventCallback): () => void;
|
|
307
281
|
/**
|
|
308
|
-
* Force
|
|
282
|
+
* Force refresh virtual outputs from the indexer.
|
|
309
283
|
*
|
|
310
284
|
* Without options, clears all sync cursors and re-fetches every contract.
|
|
311
285
|
* With options, narrows the refresh to specific scripts and/or a time window.
|
|
@@ -325,14 +299,14 @@ export declare class ContractManager implements IContractManager {
|
|
|
325
299
|
private handleContractEvent;
|
|
326
300
|
private getVtxosForContracts;
|
|
327
301
|
/**
|
|
328
|
-
* Incrementally sync
|
|
302
|
+
* Incrementally sync virtual outputs for the given contracts.
|
|
329
303
|
* Uses per-script cursors to fetch only what changed since the last sync.
|
|
330
304
|
* Scripts without a cursor are bootstrapped with a full fetch.
|
|
331
305
|
*/
|
|
332
306
|
private deltaSyncContracts;
|
|
333
307
|
/**
|
|
334
|
-
* Fetch all pending (
|
|
335
|
-
* repository. This catches
|
|
308
|
+
* Fetch all pending (unfinalized) virtual outputs and upsert them into the
|
|
309
|
+
* repository. This catches virtual outputs whose state changed outside the delta
|
|
336
310
|
* window (e.g. a spend that hasn't settled yet).
|
|
337
311
|
*/
|
|
338
312
|
private reconcilePendingFrontier;
|
|
@@ -3,33 +3,47 @@ import { WalletRepository } from "../repositories/walletRepository";
|
|
|
3
3
|
import { Contract, ContractEventCallback } from "./types";
|
|
4
4
|
/**
|
|
5
5
|
* Configuration for the ContractWatcher.
|
|
6
|
+
*
|
|
7
|
+
* @see ContractWatcher
|
|
8
|
+
*
|
|
9
|
+
* @example
|
|
10
|
+
* ```typescript
|
|
11
|
+
* const watcher = new ContractWatcher({
|
|
12
|
+
* indexerProvider,
|
|
13
|
+
* walletRepository,
|
|
14
|
+
* })
|
|
15
|
+
* ```
|
|
6
16
|
*/
|
|
7
17
|
export interface ContractWatcherConfig {
|
|
8
|
-
/**
|
|
18
|
+
/** Indexer provider used for subscriptions and queries. */
|
|
9
19
|
indexerProvider: IndexerProvider;
|
|
10
|
-
/**
|
|
20
|
+
/** Wallet repository used to store virtual output state between watcher updates. */
|
|
11
21
|
walletRepository: WalletRepository;
|
|
12
22
|
/**
|
|
13
23
|
* Interval for failsafe polling (ms).
|
|
14
24
|
* Polls even when subscription is active to catch missed events.
|
|
15
|
-
*
|
|
25
|
+
*
|
|
26
|
+
* @defaultValue `60_000` (1 minute)
|
|
16
27
|
*/
|
|
17
28
|
failsafePollIntervalMs?: number;
|
|
18
29
|
/**
|
|
19
30
|
* Initial reconnection delay (ms).
|
|
20
31
|
* Uses exponential backoff on repeated failures.
|
|
21
|
-
*
|
|
32
|
+
*
|
|
33
|
+
* @defaultValue `1_000` (1 second)
|
|
22
34
|
*/
|
|
23
35
|
reconnectDelayMs?: number;
|
|
24
36
|
/**
|
|
25
37
|
* Maximum reconnection delay (ms).
|
|
26
|
-
*
|
|
38
|
+
*
|
|
39
|
+
* @defaultValue `30_000` (30 seconds)
|
|
27
40
|
*/
|
|
28
41
|
maxReconnectDelayMs?: number;
|
|
29
42
|
/**
|
|
30
43
|
* Maximum reconnection attempts before giving up.
|
|
31
44
|
* Set to 0 for unlimited attempts.
|
|
32
|
-
*
|
|
45
|
+
*
|
|
46
|
+
* @defaultValue `0` (unlimited)
|
|
33
47
|
*/
|
|
34
48
|
maxReconnectAttempts?: number;
|
|
35
49
|
}
|
|
@@ -38,7 +52,7 @@ export interface ContractWatcherConfig {
|
|
|
38
52
|
*/
|
|
39
53
|
type ConnectionState = "disconnected" | "connecting" | "connected" | "reconnecting";
|
|
40
54
|
/**
|
|
41
|
-
* Watches multiple contracts for
|
|
55
|
+
* Watches multiple contracts for virtual output state changes with resilient connection handling.
|
|
42
56
|
*
|
|
43
57
|
* Features:
|
|
44
58
|
* - Automatic reconnection with exponential backoff
|
|
@@ -78,13 +92,20 @@ export declare class ContractWatcher {
|
|
|
78
92
|
private reconnectAttempts;
|
|
79
93
|
private reconnectTimeoutId?;
|
|
80
94
|
private failsafePollIntervalId?;
|
|
95
|
+
/**
|
|
96
|
+
* Create a contract watcher with the given providers and polling settings.
|
|
97
|
+
*
|
|
98
|
+
* @param config - Contract watcher configuration
|
|
99
|
+
* @see ContractWatcherConfig
|
|
100
|
+
*/
|
|
81
101
|
constructor(config: ContractWatcherConfig);
|
|
82
102
|
/**
|
|
83
103
|
* Add a contract to be watched.
|
|
84
104
|
*
|
|
85
|
-
* Active contracts are immediately subscribed.
|
|
86
|
-
*
|
|
87
|
-
*
|
|
105
|
+
* Active contracts are immediately subscribed.
|
|
106
|
+
*
|
|
107
|
+
* All contracts are polled to discover any existing virtual outputs
|
|
108
|
+
* (which may cause them to be watched even if inactive).
|
|
88
109
|
*/
|
|
89
110
|
addContract(contract: Contract): Promise<void>;
|
|
90
111
|
/**
|
|
@@ -108,19 +129,19 @@ export declare class ContractWatcher {
|
|
|
108
129
|
*
|
|
109
130
|
* Returns scripts for:
|
|
110
131
|
* - All active contracts
|
|
111
|
-
* - All contracts with known
|
|
132
|
+
* - All contracts with known virtual outputs (regardless of state)
|
|
112
133
|
*
|
|
113
134
|
* This ensures we continue monitoring contracts even after they're
|
|
114
|
-
* deactivated, as long as they have unspent
|
|
135
|
+
* deactivated, as long as they have unspent virtual outputs.
|
|
115
136
|
*/
|
|
116
137
|
private getScriptsToWatch;
|
|
117
138
|
/**
|
|
118
|
-
* Get
|
|
119
|
-
*
|
|
139
|
+
* Get virtual outputs for contracts, grouped by contract script.
|
|
140
|
+
* @see WalletRepository for `repo`
|
|
120
141
|
*/
|
|
121
142
|
private getContractVtxos;
|
|
122
143
|
/**
|
|
123
|
-
* Start watching for
|
|
144
|
+
* Start watching for virtual output events across all active contracts.
|
|
124
145
|
*/
|
|
125
146
|
startWatching(callback: ContractEventCallback): Promise<() => void>;
|
|
126
147
|
/**
|
|
@@ -168,7 +189,7 @@ export declare class ContractWatcher {
|
|
|
168
189
|
/**
|
|
169
190
|
* Update the subscription with scripts that should be watched.
|
|
170
191
|
*
|
|
171
|
-
* Watches both active contracts and contracts with
|
|
192
|
+
* Watches both active contracts and contracts with virtual outputs.
|
|
172
193
|
*/
|
|
173
194
|
private updateSubscription;
|
|
174
195
|
/**
|
|
@@ -180,12 +201,12 @@ export declare class ContractWatcher {
|
|
|
180
201
|
*/
|
|
181
202
|
private handleSubscriptionUpdate;
|
|
182
203
|
/**
|
|
183
|
-
* Process
|
|
204
|
+
* Process virtual outputs from subscription and route to correct contracts.
|
|
184
205
|
* Uses the scripts from the subscription response to determine contract ownership.
|
|
185
206
|
*/
|
|
186
207
|
private processSubscriptionVtxos;
|
|
187
208
|
/**
|
|
188
|
-
* Emit a
|
|
209
|
+
* Emit a virtual output event for a contract.
|
|
189
210
|
*/
|
|
190
211
|
private emitVtxoEvent;
|
|
191
212
|
}
|
|
@@ -10,7 +10,7 @@ export interface DefaultContractParams {
|
|
|
10
10
|
csvTimelock: RelativeTimelock;
|
|
11
11
|
}
|
|
12
12
|
/**
|
|
13
|
-
* Handler for default wallet
|
|
13
|
+
* Handler for default wallet virtual outputs.
|
|
14
14
|
*
|
|
15
15
|
* Default contracts use the standard forfeit + exit tapscript:
|
|
16
16
|
* - forfeit: (Alice + Server) multisig for collaborative spending
|
|
@@ -11,7 +11,7 @@ export interface DelegateContractParams {
|
|
|
11
11
|
csvTimelock: RelativeTimelock;
|
|
12
12
|
}
|
|
13
13
|
/**
|
|
14
|
-
* Handler for delegate wallet
|
|
14
|
+
* Handler for delegate wallet virtual outputs.
|
|
15
15
|
*
|
|
16
16
|
* Delegate contracts extend the default tapscript with an additional delegate path:
|
|
17
17
|
* - forfeit: (Alice + Server) multisig for collaborative spending
|
|
@@ -23,6 +23,6 @@ export declare function resolveRole(contract: Contract, context: PathContext): "
|
|
|
23
23
|
*/
|
|
24
24
|
export declare function isCltvSatisfied(context: PathContext, locktime: bigint): boolean;
|
|
25
25
|
/**
|
|
26
|
-
* Check if a CSV timelock is currently satisfied for the given context/
|
|
26
|
+
* Check if a CSV timelock is currently satisfied for the given context/virtual output.
|
|
27
27
|
*/
|
|
28
28
|
export declare function isCsvSpendable(context: PathContext, sequence?: number): boolean;
|
|
@@ -7,7 +7,7 @@ import { ContractFilter } from "../repositories";
|
|
|
7
7
|
*/
|
|
8
8
|
export type ContractState = "active" | "inactive";
|
|
9
9
|
/**
|
|
10
|
-
* Represents a contract that can receive and manage
|
|
10
|
+
* Represents a contract that can receive and manage virtual outputs.
|
|
11
11
|
*
|
|
12
12
|
* A contract is defined by its type and parameters, which together
|
|
13
13
|
* determine the VtxoScript (spending paths). The wallet's default
|
|
@@ -29,14 +29,14 @@ export type ContractState = "active" | "inactive";
|
|
|
29
29
|
* // ... timelocks
|
|
30
30
|
* },
|
|
31
31
|
* script: "5120...",
|
|
32
|
-
* address: "
|
|
32
|
+
* address: "ark1q...",
|
|
33
33
|
* state: "active",
|
|
34
34
|
* createdAt: 1704067200000,
|
|
35
35
|
* };
|
|
36
36
|
* ```
|
|
37
37
|
*/
|
|
38
38
|
export interface Contract {
|
|
39
|
-
/** Human-readable label for display purposes */
|
|
39
|
+
/** Human-readable label for display purposes. */
|
|
40
40
|
label?: string;
|
|
41
41
|
/**
|
|
42
42
|
* Contract type identifier.
|
|
@@ -50,15 +50,15 @@ export interface Contract {
|
|
|
50
50
|
* The ContractHandler for this type knows how to interpret these.
|
|
51
51
|
*/
|
|
52
52
|
params: Record<string, string>;
|
|
53
|
-
/** The pkScript hex
|
|
53
|
+
/** The pkScript hex, used as the unique identifier and primary key for contracts. */
|
|
54
54
|
script: string;
|
|
55
|
-
/**
|
|
55
|
+
/** Address derived from the contract script. */
|
|
56
56
|
address: string;
|
|
57
|
-
/** Current state of the contract */
|
|
57
|
+
/** Current state of the contract. */
|
|
58
58
|
state: ContractState;
|
|
59
|
-
/** Unix timestamp
|
|
59
|
+
/** Unix timestamp in milliseconds when this contract was created. */
|
|
60
60
|
createdAt: number;
|
|
61
|
-
/** Unix timestamp
|
|
61
|
+
/** Unix timestamp in milliseconds when this contract expires. */
|
|
62
62
|
expiresAt?: number;
|
|
63
63
|
/**
|
|
64
64
|
* Optional metadata for external integrations.
|
|
@@ -66,50 +66,54 @@ export interface Contract {
|
|
|
66
66
|
metadata?: Record<string, unknown>;
|
|
67
67
|
}
|
|
68
68
|
/**
|
|
69
|
-
* A
|
|
69
|
+
* A virtual output that has been associated with a specific contract.
|
|
70
70
|
*/
|
|
71
71
|
export interface ContractVtxo extends ExtendedVirtualCoin {
|
|
72
|
-
/** The contract script this
|
|
72
|
+
/** The contract script this virtual output belongs to. */
|
|
73
73
|
contractScript: string;
|
|
74
74
|
}
|
|
75
75
|
/**
|
|
76
|
-
* Result of path selection
|
|
76
|
+
* Result of path selection, including the tapleaf to use and any extra witness data.
|
|
77
77
|
*/
|
|
78
78
|
export interface PathSelection {
|
|
79
|
-
/**
|
|
79
|
+
/** Tapleaf script to use for spending. */
|
|
80
80
|
leaf: TapLeafScript;
|
|
81
|
-
/** Additional witness elements
|
|
81
|
+
/** Additional witness elements, for example a preimage for HTLC-like paths. */
|
|
82
82
|
extraWitness?: Bytes[];
|
|
83
|
-
/**
|
|
83
|
+
/**
|
|
84
|
+
* nSequence for the spending input, BIP-68 encoded when the leaf
|
|
85
|
+
* uses CSV. Decode with `sequenceToTimelock`; do NOT use as an
|
|
86
|
+
* absolute `Transaction.lockTime`.
|
|
87
|
+
*/
|
|
84
88
|
sequence?: number;
|
|
85
89
|
}
|
|
86
90
|
/**
|
|
87
91
|
* Context for path selection decisions.
|
|
88
92
|
*/
|
|
89
93
|
export interface PathContext {
|
|
90
|
-
/**
|
|
94
|
+
/** Whether collaborative spending is available through server cooperation. */
|
|
91
95
|
collaborative: boolean;
|
|
92
|
-
/** Current time in milliseconds */
|
|
96
|
+
/** Current time in milliseconds. */
|
|
93
97
|
currentTime: number;
|
|
94
|
-
/** Current block height
|
|
98
|
+
/** Current block height, when known. */
|
|
95
99
|
blockHeight?: number;
|
|
96
100
|
/**
|
|
97
|
-
* Wallet
|
|
98
|
-
* Used by handlers to determine wallet's role in multi-party contracts.
|
|
101
|
+
* Wallet public key encoded as 32-byte x-only hex.
|
|
102
|
+
* Used by handlers to determine the wallet's role in multi-party contracts.
|
|
99
103
|
*/
|
|
100
104
|
walletPubKey?: string;
|
|
101
105
|
/**
|
|
102
|
-
* Explicit role override
|
|
103
|
-
* If not provided, handler may derive role from walletPubKey
|
|
106
|
+
* Explicit role override for multi-party contracts such as VHTLC.
|
|
107
|
+
* If not provided, the handler may derive the role from `walletPubKey`.
|
|
104
108
|
*/
|
|
105
109
|
role?: string;
|
|
106
|
-
/** The specific
|
|
110
|
+
/** The specific virtual output being evaluated. */
|
|
107
111
|
vtxo?: VirtualCoin;
|
|
108
112
|
}
|
|
109
113
|
/**
|
|
110
114
|
* Handler for a specific contract type.
|
|
111
115
|
*
|
|
112
|
-
* Each contract type (default
|
|
116
|
+
* Each contract type (`default`, `vhtlc`, etc.) has a handler that knows how to:
|
|
113
117
|
* 1. Create the VtxoScript from parameters
|
|
114
118
|
* 2. Serialize/deserialize parameters for storage
|
|
115
119
|
* 3. Select the appropriate spending path based on context
|
|
@@ -134,14 +138,20 @@ export interface PathContext {
|
|
|
134
138
|
* ```
|
|
135
139
|
*/
|
|
136
140
|
export interface ContractHandler<P = Record<string, unknown>, S extends VtxoScript = VtxoScript> {
|
|
137
|
-
/**
|
|
141
|
+
/** Contract type managed by this handler. */
|
|
138
142
|
readonly type: string;
|
|
139
143
|
/**
|
|
140
144
|
* Create the VtxoScript from serialized parameters.
|
|
145
|
+
*
|
|
146
|
+
* @param params - Serialized contract parameters
|
|
147
|
+
* @returns Contract script instance
|
|
141
148
|
*/
|
|
142
149
|
createScript(params: Record<string, string>): S;
|
|
143
150
|
/**
|
|
144
151
|
* Serialize typed parameters to string key-value pairs.
|
|
152
|
+
*
|
|
153
|
+
* @param params - Typed contract parameters
|
|
154
|
+
* @returns Serialized key-value representation
|
|
145
155
|
*/
|
|
146
156
|
serializeParams(params: P): Record<string, string>;
|
|
147
157
|
/**
|
|
@@ -203,7 +213,7 @@ export type ContractEventCallback = (event: ContractEvent) => void;
|
|
|
203
213
|
*/
|
|
204
214
|
export type GetContractsFilter = ContractFilter;
|
|
205
215
|
/**
|
|
206
|
-
* Contract with its
|
|
216
|
+
* Contract with its virtual outputs included.
|
|
207
217
|
*/
|
|
208
218
|
export type ContractWithVtxos = {
|
|
209
219
|
contract: Contract;
|
|
@@ -217,6 +227,6 @@ export interface ContractBalance {
|
|
|
217
227
|
total: number;
|
|
218
228
|
/** Spendable balance in satoshis */
|
|
219
229
|
spendable: number;
|
|
220
|
-
/** Number of
|
|
230
|
+
/** Number of virtual outputs in this contract */
|
|
221
231
|
vtxoCount: number;
|
|
222
232
|
}
|