@arkade-os/sdk 0.4.14 → 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 +287 -215
- 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 +25 -1
- package/dist/cjs/contracts/handlers/vhtlc.js +2 -4
- 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/index.js +6 -0
- package/dist/cjs/identity/seedIdentity.js +5 -5
- package/dist/cjs/identity/singleKey.js +4 -0
- package/dist/cjs/index.js +5 -3
- 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 +25 -4
- 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 +123 -82
- package/dist/cjs/wallet/wallet.js +231 -98
- 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 +24 -1
- package/dist/esm/contracts/handlers/vhtlc.js +3 -5
- 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/index.js +5 -0
- package/dist/esm/identity/seedIdentity.js +5 -5
- package/dist/esm/identity/singleKey.js +4 -0
- package/dist/esm/index.js +3 -2
- 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 +25 -4
- 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 +122 -81
- package/dist/esm/wallet/wallet.js +232 -99
- 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 +11 -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 +36 -0
- package/dist/types/identity/seedIdentity.d.ts +10 -8
- package/dist/types/identity/singleKey.d.ts +4 -0
- package/dist/types/index.d.ts +3 -3
- 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 +18 -10
|
@@ -49,7 +49,7 @@ export class AsyncStorageTaskQueue {
|
|
|
49
49
|
// ── Config persistence (for background handler rehydration) ──────
|
|
50
50
|
/**
|
|
51
51
|
* Persist a config blob alongside the queue data.
|
|
52
|
-
* Used by
|
|
52
|
+
* Used by @see ExpoWallet.setup to store the wallet parameters
|
|
53
53
|
* that the background handler needs to reconstruct providers.
|
|
54
54
|
*/
|
|
55
55
|
async persistConfig(config) {
|
|
@@ -3,7 +3,7 @@ export const CONTRACT_POLL_TASK_TYPE = "contract-poll";
|
|
|
3
3
|
* Polls the indexer for the latest VTXO state of every contract and
|
|
4
4
|
* persists the results to the wallet repository.
|
|
5
5
|
*
|
|
6
|
-
* Replicates the polling subset of
|
|
6
|
+
* Replicates the polling subset of @see ContractManager.initialize:
|
|
7
7
|
* 1. Load all contracts from the contract repository.
|
|
8
8
|
* 2. Mark expired active contracts as inactive.
|
|
9
9
|
* 3. Paginated fetch of spendable VTXOs from the indexer.
|
|
@@ -26,7 +26,7 @@ export const contractPollProcessor = {
|
|
|
26
26
|
contract.state = "inactive";
|
|
27
27
|
await contractRepository.saveContract(contract);
|
|
28
28
|
}
|
|
29
|
-
// Paginated fetch of spendable
|
|
29
|
+
// Paginated fetch of spendable virtual outputs
|
|
30
30
|
const pageSize = 100;
|
|
31
31
|
let pageIndex = 0;
|
|
32
32
|
let hasMore = true;
|
|
@@ -4,7 +4,7 @@ import { getRandomId, extendVirtualCoin, extendVtxoFromContract, } from '../../w
|
|
|
4
4
|
*
|
|
5
5
|
* For each task in the inbox:
|
|
6
6
|
* 1. Find the processor whose `taskType` matches `task.type`.
|
|
7
|
-
* 2. Execute it, producing a
|
|
7
|
+
* 2. Execute it, producing a @see TaskResult.
|
|
8
8
|
* 3. Push the result to the outbox and remove the task from the inbox.
|
|
9
9
|
*
|
|
10
10
|
* Tasks with no matching processor produce a `"noop"` result.
|
|
@@ -53,8 +53,8 @@ export async function runTasks(queue, processors, deps) {
|
|
|
53
53
|
return results;
|
|
54
54
|
}
|
|
55
55
|
/**
|
|
56
|
-
* Build the
|
|
57
|
-
* (e.g.
|
|
56
|
+
* Build the @see TaskDependencies needed by task processors
|
|
57
|
+
* (e.g. `src/worker/expo/processors/contractPollProcessor.ts`)
|
|
58
58
|
*
|
|
59
59
|
* This is the same construction that `defineExpoBackgroundTask` does
|
|
60
60
|
* internally, extracted so that consumers with custom schedulers
|
|
@@ -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,9 +116,9 @@ 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
|
|
121
|
+
/** Function to get the wallet's default Arkade address */
|
|
161
122
|
getDefaultAddress: () => Promise<string>;
|
|
162
123
|
/** Watcher configuration */
|
|
163
124
|
watcherConfig?: Partial<ContractWatcherConfig>;
|
|
@@ -172,38 +133,50 @@ export type CreateContractParams = Omit<Contract, "createdAt" | "state"> & {
|
|
|
172
133
|
/**
|
|
173
134
|
* Central manager for contract lifecycle and operations.
|
|
174
135
|
*
|
|
175
|
-
*
|
|
176
|
-
* -
|
|
177
|
-
* -
|
|
178
|
-
* -
|
|
136
|
+
* Responsibilities:
|
|
137
|
+
* - Create and persist contracts
|
|
138
|
+
* - Query stored contracts (optionally with their virtual outputs)
|
|
139
|
+
* - Provide spendable path selection for a contract
|
|
140
|
+
* - Emit contract-related events (virtual output received/spent/expired, connection reset)
|
|
141
|
+
*
|
|
142
|
+
* Notes:
|
|
143
|
+
* - Implementations typically start watching automatically during initialization
|
|
144
|
+
* (so `onContractEvent()` is just for subscribing).
|
|
179
145
|
*
|
|
180
146
|
* @example
|
|
181
147
|
* ```typescript
|
|
182
|
-
* const manager =
|
|
148
|
+
* const manager = await ContractManager.create({
|
|
183
149
|
* indexerProvider: wallet.indexerProvider,
|
|
184
150
|
* contractRepository: wallet.contractRepository,
|
|
151
|
+
* walletRepository: wallet.walletRepository,
|
|
185
152
|
* getDefaultAddress: () => wallet.getAddress(),
|
|
186
153
|
* });
|
|
187
154
|
*
|
|
188
|
-
* // Initialize (loads persisted contracts)
|
|
189
|
-
* await manager.initialize();
|
|
190
|
-
*
|
|
191
155
|
* // Create a new VHTLC contract
|
|
192
156
|
* const contract = await manager.createContract({
|
|
193
157
|
* label: "Lightning Receive",
|
|
194
158
|
* type: "vhtlc",
|
|
195
|
-
* params: { sender: "
|
|
159
|
+
* params: { sender: "ark1q...", receiver: "ark1q...", ... },
|
|
196
160
|
* script: "5120...",
|
|
197
|
-
* address: "
|
|
161
|
+
* address: "ark1q...",
|
|
198
162
|
* });
|
|
199
163
|
*
|
|
200
164
|
* // Start watching for events
|
|
201
|
-
* const
|
|
165
|
+
* const unsubscribe = manager.onContractEvent((event) => {
|
|
202
166
|
* console.log(`${event.type} on ${event.contractScript}`);
|
|
203
167
|
* });
|
|
204
168
|
*
|
|
169
|
+
* // Query contracts together with their current virtual outputs
|
|
170
|
+
* const contractsWithVtxos = await manager.getContractsWithVtxos();
|
|
171
|
+
*
|
|
205
172
|
* // Get balance across all contracts
|
|
206
|
-
* const balances =
|
|
173
|
+
* const balances = contractsWithVtxos.flatMap(({vtxos}) => vtxos).reduce((acc, vtxo) => acc + vtxo.value, 0)
|
|
174
|
+
*
|
|
175
|
+
* // Later: unsubscribe from events
|
|
176
|
+
* unsubscribe();
|
|
177
|
+
*
|
|
178
|
+
* // Clean up
|
|
179
|
+
* manager.dispose();
|
|
207
180
|
* ```
|
|
208
181
|
*/
|
|
209
182
|
export declare class ContractManager implements IContractManager {
|
|
@@ -218,7 +191,7 @@ export declare class ContractManager implements IContractManager {
|
|
|
218
191
|
* Initialize the manager by loading persisted contracts and starting to watch.
|
|
219
192
|
*
|
|
220
193
|
* After initialization, the manager automatically watches all active contracts
|
|
221
|
-
* and contracts with
|
|
194
|
+
* and contracts with virtual outputs. Use `onContractEvent()` to register event callbacks.
|
|
222
195
|
*
|
|
223
196
|
* @param config ContractManagerConfig
|
|
224
197
|
*/
|
|
@@ -279,10 +252,14 @@ export declare class ContractManager implements IContractManager {
|
|
|
279
252
|
/**
|
|
280
253
|
* Get currently spendable paths for a contract.
|
|
281
254
|
*
|
|
282
|
-
* @param contractScript - The contract script
|
|
283
255
|
* @param options - Options for getting spendable paths
|
|
284
256
|
*/
|
|
285
257
|
getSpendablePaths(options: GetSpendablePathsOptions): Promise<PathSelection[]>;
|
|
258
|
+
/**
|
|
259
|
+
* Get every currently valid spending path for a contract.
|
|
260
|
+
*
|
|
261
|
+
* @param options - Options for getting spending paths
|
|
262
|
+
*/
|
|
286
263
|
getAllSpendingPaths(options: GetAllSpendingPathsOptions): Promise<PathSelection[]>;
|
|
287
264
|
/**
|
|
288
265
|
* Register a callback for contract events.
|
|
@@ -305,7 +282,7 @@ export declare class ContractManager implements IContractManager {
|
|
|
305
282
|
*/
|
|
306
283
|
onContractEvent(callback: ContractEventCallback): () => void;
|
|
307
284
|
/**
|
|
308
|
-
* Force
|
|
285
|
+
* Force refresh virtual outputs from the indexer.
|
|
309
286
|
*
|
|
310
287
|
* Without options, clears all sync cursors and re-fetches every contract.
|
|
311
288
|
* With options, narrows the refresh to specific scripts and/or a time window.
|
|
@@ -325,14 +302,14 @@ export declare class ContractManager implements IContractManager {
|
|
|
325
302
|
private handleContractEvent;
|
|
326
303
|
private getVtxosForContracts;
|
|
327
304
|
/**
|
|
328
|
-
* Incrementally sync
|
|
305
|
+
* Incrementally sync virtual outputs for the given contracts.
|
|
329
306
|
* Uses per-script cursors to fetch only what changed since the last sync.
|
|
330
307
|
* Scripts without a cursor are bootstrapped with a full fetch.
|
|
331
308
|
*/
|
|
332
309
|
private deltaSyncContracts;
|
|
333
310
|
/**
|
|
334
|
-
* Fetch all pending (
|
|
335
|
-
* repository. This catches
|
|
311
|
+
* Fetch all pending (unfinalized) virtual outputs and upsert them into the
|
|
312
|
+
* repository. This catches virtual outputs whose state changed outside the delta
|
|
336
313
|
* window (e.g. a spend that hasn't settled yet).
|
|
337
314
|
*/
|
|
338
315
|
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
|
|
@@ -13,6 +13,16 @@ export declare function sequenceToTimelock(sequence: number): RelativeTimelock;
|
|
|
13
13
|
*/
|
|
14
14
|
export declare function resolveRole(contract: Contract, context: PathContext): "sender" | "receiver" | undefined;
|
|
15
15
|
/**
|
|
16
|
-
* Check if
|
|
16
|
+
* Check if an absolute (CLTV) locktime is currently satisfied.
|
|
17
|
+
*
|
|
18
|
+
* Following the BIP65 convention:
|
|
19
|
+
* - locktime < 500_000_000 → interpreted as a block height; compared against `context.blockHeight`
|
|
20
|
+
* - locktime >= 500_000_000 → interpreted as a Unix timestamp (seconds); compared against `context.currentTime`
|
|
21
|
+
*
|
|
22
|
+
* Returns false if the relevant context field is missing.
|
|
23
|
+
*/
|
|
24
|
+
export declare function isCltvSatisfied(context: PathContext, locktime: bigint): boolean;
|
|
25
|
+
/**
|
|
26
|
+
* Check if a CSV timelock is currently satisfied for the given context/virtual output.
|
|
17
27
|
*/
|
|
18
28
|
export declare function isCsvSpendable(context: PathContext, sequence?: number): boolean;
|