@arkade-os/sdk 0.4.0-next.4 → 0.4.0-next.5
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/dist/cjs/wallet/serviceWorker/wallet-message-handler.js +32 -0
- package/dist/cjs/wallet/serviceWorker/wallet.js +21 -0
- package/dist/cjs/worker/messageBus.js +6 -0
- package/dist/esm/wallet/serviceWorker/wallet-message-handler.js +32 -0
- package/dist/esm/wallet/serviceWorker/wallet.js +21 -0
- package/dist/esm/worker/messageBus.js +6 -0
- package/dist/types/wallet/serviceWorker/wallet-message-handler.d.ts +30 -2
- package/dist/types/wallet/serviceWorker/wallet.d.ts +5 -1
- package/dist/types/worker/messageBus.d.ts +1 -0
- package/package.json +1 -1
|
@@ -289,6 +289,10 @@ class WalletMessageHandler {
|
|
|
289
289
|
payload: { txid },
|
|
290
290
|
});
|
|
291
291
|
}
|
|
292
|
+
case "DELEGATE": {
|
|
293
|
+
const response = await this.handleDelegate(message);
|
|
294
|
+
return this.tagged({ id, ...response });
|
|
295
|
+
}
|
|
292
296
|
default:
|
|
293
297
|
console.error("Unknown message type", message);
|
|
294
298
|
throw new Error("Unknown message");
|
|
@@ -502,6 +506,34 @@ class WalletMessageHandler {
|
|
|
502
506
|
payload: { tx: signature },
|
|
503
507
|
};
|
|
504
508
|
}
|
|
509
|
+
async handleDelegate(message) {
|
|
510
|
+
const wallet = this.requireWallet();
|
|
511
|
+
if (!wallet.delegatorManager) {
|
|
512
|
+
throw new Error("Delegator not configured");
|
|
513
|
+
}
|
|
514
|
+
const { vtxoOutpoints, destination, delegateAt } = message.payload;
|
|
515
|
+
const allVtxos = await wallet.getVtxos();
|
|
516
|
+
const outpointSet = new Set(vtxoOutpoints.map((o) => `${o.txid}:${o.vout}`));
|
|
517
|
+
const filtered = allVtxos.filter((v) => outpointSet.has(`${v.txid}:${v.vout}`));
|
|
518
|
+
const result = await wallet.delegatorManager.delegate(filtered, destination, delegateAt !== undefined ? new Date(delegateAt) : undefined);
|
|
519
|
+
return {
|
|
520
|
+
tag: this.messageTag,
|
|
521
|
+
type: "DELEGATE_SUCCESS",
|
|
522
|
+
payload: {
|
|
523
|
+
delegated: result.delegated.map((o) => ({
|
|
524
|
+
txid: o.txid,
|
|
525
|
+
vout: o.vout,
|
|
526
|
+
})),
|
|
527
|
+
failed: result.failed.map((f) => ({
|
|
528
|
+
outpoints: f.outpoints.map((o) => ({
|
|
529
|
+
txid: o.txid,
|
|
530
|
+
vout: o.vout,
|
|
531
|
+
})),
|
|
532
|
+
error: String(f.error),
|
|
533
|
+
})),
|
|
534
|
+
},
|
|
535
|
+
};
|
|
536
|
+
}
|
|
505
537
|
async handleGetVtxos(message) {
|
|
506
538
|
if (!this.readonlyWallet) {
|
|
507
539
|
throw new Error("Wallet handler not initialized");
|
|
@@ -124,6 +124,7 @@ class ServiceWorkerReadonlyWallet {
|
|
|
124
124
|
url: initConfig.arkServerUrl,
|
|
125
125
|
publicKey: initConfig.arkServerPublicKey,
|
|
126
126
|
},
|
|
127
|
+
delegatorUrl: initConfig.delegatorUrl,
|
|
127
128
|
timeoutMs: options.messageBusTimeoutMs,
|
|
128
129
|
}, options.messageBusTimeoutMs);
|
|
129
130
|
// Initialize the wallet handler
|
|
@@ -540,6 +541,7 @@ class ServiceWorkerWallet extends ServiceWorkerReadonlyWallet {
|
|
|
540
541
|
url: initConfig.arkServerUrl,
|
|
541
542
|
publicKey: initConfig.arkServerPublicKey,
|
|
542
543
|
},
|
|
544
|
+
delegatorUrl: initConfig.delegatorUrl,
|
|
543
545
|
timeoutMs: options.messageBusTimeoutMs,
|
|
544
546
|
}, options.messageBusTimeoutMs);
|
|
545
547
|
// Initialize the service worker with the config
|
|
@@ -654,5 +656,24 @@ class ServiceWorkerWallet extends ServiceWorkerReadonlyWallet {
|
|
|
654
656
|
throw new Error(`Send failed: ${error}`);
|
|
655
657
|
}
|
|
656
658
|
}
|
|
659
|
+
async delegate(vtxoOutpoints, destination, delegateAt) {
|
|
660
|
+
const message = {
|
|
661
|
+
tag: this.messageTag,
|
|
662
|
+
type: "DELEGATE",
|
|
663
|
+
id: (0, utils_2.getRandomId)(),
|
|
664
|
+
payload: {
|
|
665
|
+
vtxoOutpoints,
|
|
666
|
+
destination,
|
|
667
|
+
delegateAt: delegateAt?.getTime(),
|
|
668
|
+
},
|
|
669
|
+
};
|
|
670
|
+
try {
|
|
671
|
+
const response = await this.sendMessage(message);
|
|
672
|
+
return response.payload;
|
|
673
|
+
}
|
|
674
|
+
catch (error) {
|
|
675
|
+
throw new Error(`Delegation failed: ${error}`);
|
|
676
|
+
}
|
|
677
|
+
}
|
|
657
678
|
}
|
|
658
679
|
exports.ServiceWorkerWallet = ServiceWorkerWallet;
|
|
@@ -4,6 +4,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
4
4
|
exports.MessageBus = void 0;
|
|
5
5
|
const service_worker_manager_1 = require("./browser/service-worker-manager");
|
|
6
6
|
const ark_1 = require("../providers/ark");
|
|
7
|
+
const delegator_1 = require("../providers/delegator");
|
|
7
8
|
const identity_1 = require("../identity");
|
|
8
9
|
const wallet_1 = require("../wallet/wallet");
|
|
9
10
|
const base_1 = require("@scure/base");
|
|
@@ -139,6 +140,9 @@ class MessageBus {
|
|
|
139
140
|
walletRepository: this.walletRepository,
|
|
140
141
|
contractRepository: this.contractRepository,
|
|
141
142
|
};
|
|
143
|
+
const delegatorProvider = config.delegatorUrl
|
|
144
|
+
? new delegator_1.RestDelegatorProvider(config.delegatorUrl)
|
|
145
|
+
: undefined;
|
|
142
146
|
if ("privateKey" in config.wallet) {
|
|
143
147
|
const identity = identity_1.SingleKey.fromHex(config.wallet.privateKey);
|
|
144
148
|
const wallet = await wallet_1.Wallet.create({
|
|
@@ -146,6 +150,7 @@ class MessageBus {
|
|
|
146
150
|
arkServerUrl: config.arkServer.url,
|
|
147
151
|
arkServerPublicKey: config.arkServer.publicKey,
|
|
148
152
|
storage,
|
|
153
|
+
delegatorProvider,
|
|
149
154
|
});
|
|
150
155
|
return { wallet, arkProvider, readonlyWallet: wallet };
|
|
151
156
|
}
|
|
@@ -156,6 +161,7 @@ class MessageBus {
|
|
|
156
161
|
arkServerUrl: config.arkServer.url,
|
|
157
162
|
arkServerPublicKey: config.arkServer.publicKey,
|
|
158
163
|
storage,
|
|
164
|
+
delegatorProvider,
|
|
159
165
|
});
|
|
160
166
|
return { readonlyWallet, arkProvider };
|
|
161
167
|
}
|
|
@@ -286,6 +286,10 @@ export class WalletMessageHandler {
|
|
|
286
286
|
payload: { txid },
|
|
287
287
|
});
|
|
288
288
|
}
|
|
289
|
+
case "DELEGATE": {
|
|
290
|
+
const response = await this.handleDelegate(message);
|
|
291
|
+
return this.tagged({ id, ...response });
|
|
292
|
+
}
|
|
289
293
|
default:
|
|
290
294
|
console.error("Unknown message type", message);
|
|
291
295
|
throw new Error("Unknown message");
|
|
@@ -499,6 +503,34 @@ export class WalletMessageHandler {
|
|
|
499
503
|
payload: { tx: signature },
|
|
500
504
|
};
|
|
501
505
|
}
|
|
506
|
+
async handleDelegate(message) {
|
|
507
|
+
const wallet = this.requireWallet();
|
|
508
|
+
if (!wallet.delegatorManager) {
|
|
509
|
+
throw new Error("Delegator not configured");
|
|
510
|
+
}
|
|
511
|
+
const { vtxoOutpoints, destination, delegateAt } = message.payload;
|
|
512
|
+
const allVtxos = await wallet.getVtxos();
|
|
513
|
+
const outpointSet = new Set(vtxoOutpoints.map((o) => `${o.txid}:${o.vout}`));
|
|
514
|
+
const filtered = allVtxos.filter((v) => outpointSet.has(`${v.txid}:${v.vout}`));
|
|
515
|
+
const result = await wallet.delegatorManager.delegate(filtered, destination, delegateAt !== undefined ? new Date(delegateAt) : undefined);
|
|
516
|
+
return {
|
|
517
|
+
tag: this.messageTag,
|
|
518
|
+
type: "DELEGATE_SUCCESS",
|
|
519
|
+
payload: {
|
|
520
|
+
delegated: result.delegated.map((o) => ({
|
|
521
|
+
txid: o.txid,
|
|
522
|
+
vout: o.vout,
|
|
523
|
+
})),
|
|
524
|
+
failed: result.failed.map((f) => ({
|
|
525
|
+
outpoints: f.outpoints.map((o) => ({
|
|
526
|
+
txid: o.txid,
|
|
527
|
+
vout: o.vout,
|
|
528
|
+
})),
|
|
529
|
+
error: String(f.error),
|
|
530
|
+
})),
|
|
531
|
+
},
|
|
532
|
+
};
|
|
533
|
+
}
|
|
502
534
|
async handleGetVtxos(message) {
|
|
503
535
|
if (!this.readonlyWallet) {
|
|
504
536
|
throw new Error("Wallet handler not initialized");
|
|
@@ -121,6 +121,7 @@ export class ServiceWorkerReadonlyWallet {
|
|
|
121
121
|
url: initConfig.arkServerUrl,
|
|
122
122
|
publicKey: initConfig.arkServerPublicKey,
|
|
123
123
|
},
|
|
124
|
+
delegatorUrl: initConfig.delegatorUrl,
|
|
124
125
|
timeoutMs: options.messageBusTimeoutMs,
|
|
125
126
|
}, options.messageBusTimeoutMs);
|
|
126
127
|
// Initialize the wallet handler
|
|
@@ -536,6 +537,7 @@ export class ServiceWorkerWallet extends ServiceWorkerReadonlyWallet {
|
|
|
536
537
|
url: initConfig.arkServerUrl,
|
|
537
538
|
publicKey: initConfig.arkServerPublicKey,
|
|
538
539
|
},
|
|
540
|
+
delegatorUrl: initConfig.delegatorUrl,
|
|
539
541
|
timeoutMs: options.messageBusTimeoutMs,
|
|
540
542
|
}, options.messageBusTimeoutMs);
|
|
541
543
|
// Initialize the service worker with the config
|
|
@@ -650,4 +652,23 @@ export class ServiceWorkerWallet extends ServiceWorkerReadonlyWallet {
|
|
|
650
652
|
throw new Error(`Send failed: ${error}`);
|
|
651
653
|
}
|
|
652
654
|
}
|
|
655
|
+
async delegate(vtxoOutpoints, destination, delegateAt) {
|
|
656
|
+
const message = {
|
|
657
|
+
tag: this.messageTag,
|
|
658
|
+
type: "DELEGATE",
|
|
659
|
+
id: getRandomId(),
|
|
660
|
+
payload: {
|
|
661
|
+
vtxoOutpoints,
|
|
662
|
+
destination,
|
|
663
|
+
delegateAt: delegateAt?.getTime(),
|
|
664
|
+
},
|
|
665
|
+
};
|
|
666
|
+
try {
|
|
667
|
+
const response = await this.sendMessage(message);
|
|
668
|
+
return response.payload;
|
|
669
|
+
}
|
|
670
|
+
catch (error) {
|
|
671
|
+
throw new Error(`Delegation failed: ${error}`);
|
|
672
|
+
}
|
|
673
|
+
}
|
|
653
674
|
}
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
/// <reference lib="webworker" />
|
|
2
2
|
import { getActiveServiceWorker, setupServiceWorkerOnce, } from './browser/service-worker-manager.js';
|
|
3
3
|
import { RestArkProvider } from '../providers/ark.js';
|
|
4
|
+
import { RestDelegatorProvider } from '../providers/delegator.js';
|
|
4
5
|
import { ReadonlySingleKey, SingleKey } from '../identity/index.js';
|
|
5
6
|
import { ReadonlyWallet, Wallet } from '../wallet/wallet.js';
|
|
6
7
|
import { hex } from "@scure/base";
|
|
@@ -136,6 +137,9 @@ export class MessageBus {
|
|
|
136
137
|
walletRepository: this.walletRepository,
|
|
137
138
|
contractRepository: this.contractRepository,
|
|
138
139
|
};
|
|
140
|
+
const delegatorProvider = config.delegatorUrl
|
|
141
|
+
? new RestDelegatorProvider(config.delegatorUrl)
|
|
142
|
+
: undefined;
|
|
139
143
|
if ("privateKey" in config.wallet) {
|
|
140
144
|
const identity = SingleKey.fromHex(config.wallet.privateKey);
|
|
141
145
|
const wallet = await Wallet.create({
|
|
@@ -143,6 +147,7 @@ export class MessageBus {
|
|
|
143
147
|
arkServerUrl: config.arkServer.url,
|
|
144
148
|
arkServerPublicKey: config.arkServer.publicKey,
|
|
145
149
|
storage,
|
|
150
|
+
delegatorProvider,
|
|
146
151
|
});
|
|
147
152
|
return { wallet, arkProvider, readonlyWallet: wallet };
|
|
148
153
|
}
|
|
@@ -153,6 +158,7 @@ export class MessageBus {
|
|
|
153
158
|
arkServerUrl: config.arkServer.url,
|
|
154
159
|
arkServerPublicKey: config.arkServer.publicKey,
|
|
155
160
|
storage,
|
|
161
|
+
delegatorProvider,
|
|
156
162
|
});
|
|
157
163
|
return { readonlyWallet, arkProvider };
|
|
158
164
|
}
|
|
@@ -318,8 +318,35 @@ export type ResponseBurn = ResponseEnvelope & {
|
|
|
318
318
|
txid: string;
|
|
319
319
|
};
|
|
320
320
|
};
|
|
321
|
-
export type
|
|
322
|
-
|
|
321
|
+
export type RequestDelegate = RequestEnvelope & {
|
|
322
|
+
type: "DELEGATE";
|
|
323
|
+
payload: {
|
|
324
|
+
vtxoOutpoints: {
|
|
325
|
+
txid: string;
|
|
326
|
+
vout: number;
|
|
327
|
+
}[];
|
|
328
|
+
destination: string;
|
|
329
|
+
delegateAt?: number;
|
|
330
|
+
};
|
|
331
|
+
};
|
|
332
|
+
export type ResponseDelegate = ResponseEnvelope & {
|
|
333
|
+
type: "DELEGATE_SUCCESS";
|
|
334
|
+
payload: {
|
|
335
|
+
delegated: {
|
|
336
|
+
txid: string;
|
|
337
|
+
vout: number;
|
|
338
|
+
}[];
|
|
339
|
+
failed: {
|
|
340
|
+
outpoints: {
|
|
341
|
+
txid: string;
|
|
342
|
+
vout: number;
|
|
343
|
+
}[];
|
|
344
|
+
error: string;
|
|
345
|
+
}[];
|
|
346
|
+
};
|
|
347
|
+
};
|
|
348
|
+
export type WalletUpdaterRequest = RequestInitWallet | RequestSettle | RequestSendBitcoin | RequestGetAddress | RequestGetBoardingAddress | RequestGetBalance | RequestGetVtxos | RequestGetBoardingUtxos | RequestGetTransactionHistory | RequestGetStatus | RequestClear | RequestReloadWallet | RequestSignTransaction | RequestCreateContract | RequestGetContracts | RequestGetContractsWithVtxos | RequestUpdateContract | RequestDeleteContract | RequestGetSpendablePaths | RequestGetAllSpendingPaths | RequestIsContractManagerWatching | RequestSend | RequestGetAssetDetails | RequestIssue | RequestReissue | RequestBurn | RequestDelegate;
|
|
349
|
+
export type WalletUpdaterResponse = ResponseEnvelope & (ResponseInitWallet | ResponseSettle | ResponseSettleEvent | ResponseSendBitcoin | ResponseGetAddress | ResponseGetBoardingAddress | ResponseGetBalance | ResponseGetVtxos | ResponseGetBoardingUtxos | ResponseGetTransactionHistory | ResponseGetStatus | ResponseClear | ResponseReloadWallet | ResponseUtxoUpdate | ResponseVtxoUpdate | ResponseSignTransaction | ResponseCreateContract | ResponseGetContracts | ResponseGetContractsWithVtxos | ResponseUpdateContract | ResponseDeleteContract | ResponseGetSpendablePaths | ResponseGetAllSpendingPaths | ResponseIsContractManagerWatching | ResponseContractEvent | ResponseSend | ResponseGetAssetDetails | ResponseIssue | ResponseReissue | ResponseBurn | ResponseDelegate);
|
|
323
350
|
export declare class WalletMessageHandler implements MessageHandler<WalletUpdaterRequest, WalletUpdaterResponse> {
|
|
324
351
|
readonly messageTag: string;
|
|
325
352
|
private wallet;
|
|
@@ -360,6 +387,7 @@ export declare class WalletMessageHandler implements MessageHandler<WalletUpdate
|
|
|
360
387
|
private handleSettle;
|
|
361
388
|
private handleSendBitcoin;
|
|
362
389
|
private handleSignTransaction;
|
|
390
|
+
private handleDelegate;
|
|
363
391
|
private handleGetVtxos;
|
|
364
392
|
private clear;
|
|
365
393
|
private ensureContractEventBroadcasting;
|
|
@@ -3,7 +3,7 @@ import { SettlementEvent } from "../../providers/ark";
|
|
|
3
3
|
import { Identity, ReadonlyIdentity } from "../../identity";
|
|
4
4
|
import { WalletRepository } from "../../repositories/walletRepository";
|
|
5
5
|
import { ContractRepository } from "../../repositories/contractRepository";
|
|
6
|
-
import { ResponseGetStatus, WalletUpdaterRequest, WalletUpdaterResponse } from "./wallet-message-handler";
|
|
6
|
+
import { ResponseGetStatus, WalletUpdaterRequest, WalletUpdaterResponse, ResponseDelegate } from "./wallet-message-handler";
|
|
7
7
|
import type { IContractManager } from "../../contracts/contractManager";
|
|
8
8
|
type PrivateKeyIdentity = Identity & {
|
|
9
9
|
toHex(): string;
|
|
@@ -134,5 +134,9 @@ export declare class ServiceWorkerWallet extends ServiceWorkerReadonlyWallet imp
|
|
|
134
134
|
sendBitcoin(params: SendBitcoinParams): Promise<string>;
|
|
135
135
|
settle(params?: SettleParams, callback?: (event: SettlementEvent) => void): Promise<string>;
|
|
136
136
|
send(...recipients: Recipient[]): Promise<string>;
|
|
137
|
+
delegate(vtxoOutpoints: {
|
|
138
|
+
txid: string;
|
|
139
|
+
vout: number;
|
|
140
|
+
}[], destination: string, delegateAt?: Date): Promise<ResponseDelegate["payload"]>;
|
|
137
141
|
}
|
|
138
142
|
export {};
|