forked from iotaledger/iota-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwallet.ts
1942 lines (1819 loc) · 59.3 KB
/
wallet.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright 2024 IOTA Stiftung
// SPDX-License-Identifier: Apache-2.0
import { WalletMethodHandler } from './wallet-method-handler';
import {
Balance,
SyncOptions,
SendParams,
SendNativeTokenParams,
SendNftParams,
AccountOutputParams,
FilterOptions,
CreateNativeTokenParams,
MintNftParams,
OutputWithExtendedMetadata,
OutputParams,
OutputsToClaim,
TransactionWithMetadata,
TransactionOptions,
ParticipationOverview,
ParticipationEventId,
ParticipationEventStatus,
ParticipationEventType,
ParticipationEventWithNodes,
ParticipationEventRegistrationOptions,
ParticipationEventMap,
SignedTransactionData,
PreparedTransaction,
PreparedCreateNativeTokenTransactionData,
ConsolidationParams,
CreateDelegationTransaction,
BeginStakingParams,
SendManaParams,
} from '../types/wallet';
import { Client, Node, Burn, PreparedTransactionData } from '../client';
import {
Output,
FoundryOutput,
Response,
PreparedCreateNativeTokenTransaction,
u64,
u256,
NftId,
TokenId,
OutputId,
AccountId,
FoundryId,
TransactionId,
NumericString,
Bech32Address,
DelegationId,
BlockId,
} from '../types';
import { plainToInstance } from 'class-transformer';
import { bigIntToHex, hexToBigInt } from '../types/utils/hex-encoding';
import {
WalletOptions,
WalletEventType,
WalletEvent,
CreateDelegationParams,
PreparedCreateDelegationTransactionData,
} from '../types/wallet';
import { Auth, ClientOptions, LedgerNanoStatus } from '../types/client';
import { SecretManager } from '../secret_manager';
import { PreparedCreateDelegationTransaction } from '../types/wallet/create-delegation-transaction';
/** The Wallet class. */
export class Wallet {
private methodHandler: WalletMethodHandler;
/**
* @param methodHandler The Rust method handler created in `WalletMethodHandler.create()`.
*/
constructor(methodHandler: WalletMethodHandler) {
this.methodHandler = methodHandler;
}
/**
* @param options The wallet options.
*/
static async create(options: WalletOptions): Promise<Wallet> {
return new Wallet(await WalletMethodHandler.create(options));
}
/**
* Backup the data to a Stronghold snapshot.
*/
async backupToStrongholdSnapshot(
destination: string,
password: string,
): Promise<void> {
await this.methodHandler.callMethod({
name: 'backupToStrongholdSnapshot',
data: {
destination,
password,
},
});
}
/**
* Change the Stronghold password.
*/
async changeStrongholdPassword(
currentPassword: string,
newPassword: string,
): Promise<void> {
await this.methodHandler.callMethod({
name: 'changeStrongholdPassword',
data: {
currentPassword,
newPassword,
},
});
}
/**
* Clear the Stronghold password from memory.
*/
async clearStrongholdPassword(): Promise<void> {
await this.methodHandler.callMethod({
name: 'clearStrongholdPassword',
});
}
/**
* Destroy the Wallet and drop its database connection.
*/
async destroy(): Promise<void> {
return this.methodHandler.destroy();
}
/**
* Emit a provided event for testing of the event system.
*/
async emitTestEvent(event: WalletEvent): Promise<void> {
await this.methodHandler.callMethod({
name: 'emitTestEvent',
data: { event },
});
}
/**
* Get client.
*/
async getClient(): Promise<Client> {
return this.methodHandler.getClient();
}
/**
* Get secret manager.
*/
async getSecretManager(): Promise<SecretManager> {
return this.methodHandler.getSecretManager();
}
/**
* Get the status for a Ledger Nano.
*/
async getLedgerNanoStatus(): Promise<LedgerNanoStatus> {
const response = await this.methodHandler.callMethod({
name: 'getLedgerNanoStatus',
});
return JSON.parse(response).payload;
}
/**
* Check if the Stronghold password has been set.
*/
async isStrongholdPasswordAvailable(): Promise<boolean> {
const response = await this.methodHandler.callMethod({
name: 'isStrongholdPasswordAvailable',
});
return JSON.parse(response).payload;
}
/**
* Listen to wallet events with a callback. An empty array will listen to all possible events.
*/
async listen(
eventTypes: WalletEventType[],
callback: (error: Error, event: WalletEvent) => void,
): Promise<void> {
return this.methodHandler.listen(eventTypes, callback);
}
/**
* Clear the callbacks for provided events. An empty array will clear all listeners.
*/
async clearListeners(eventTypes: WalletEventType[]): Promise<void> {
const response = await this.methodHandler.callMethod({
name: 'clearListeners',
data: { eventTypes },
});
return JSON.parse(response).payload;
}
/**
* Restore a backup from a Stronghold file
* Replaces client_options, coin_type, secret_manager and accounts. Returns an error if accounts were already created
* If Stronghold is used as secret_manager, the existing Stronghold file will be overwritten. If a mnemonic was
* stored, it will be gone.
* if ignore_if_coin_type_mismatch is provided client options will not be restored
* if ignore_if_coin_type_mismatch == true, client options coin type and accounts will not be restored if the cointype doesn't match
* If a bech32 hrp is provided to ignore_if_bech32_hrp_mismatch, that doesn't match the one of the current address, the wallet will not be restored.
*/
async restoreFromStrongholdSnapshot(
source: string,
password: string,
ignoreIfCoinTypeMismatch?: boolean,
ignoreIfBech32Mismatch?: string,
): Promise<void> {
await this.methodHandler.callMethod({
name: 'restoreFromStrongholdSnapshot',
data: {
source,
password,
ignoreIfCoinTypeMismatch,
ignoreIfBech32Mismatch,
},
});
}
/**
* Set ClientOptions.
*/
async setClientOptions(clientOptions: ClientOptions): Promise<void> {
await this.methodHandler.callMethod({
name: 'setClientOptions',
data: { clientOptions },
});
}
/**
* Set the Stronghold password.
*/
async setStrongholdPassword(password: string): Promise<void> {
await this.methodHandler.callMethod({
name: 'setStrongholdPassword',
data: { password },
});
}
/**
* Set the interval after which the Stronghold password gets cleared from memory.
*/
async setStrongholdPasswordClearInterval(
intervalInMilliseconds?: number,
): Promise<void> {
await this.methodHandler.callMethod({
name: 'setStrongholdPasswordClearInterval',
data: { intervalInMilliseconds },
});
}
/**
* Start the background syncing process for all accounts.
*/
async startBackgroundSync(
options?: SyncOptions,
intervalInMilliseconds?: number,
): Promise<void> {
await this.methodHandler.callMethod({
name: 'startBackgroundSync',
data: {
options,
intervalInMilliseconds,
},
});
}
/**
* Stop the background syncing process for all accounts.
*/
async stopBackgroundSync(): Promise<void> {
await this.methodHandler.callMethod({
name: 'stopBackgroundSync',
});
}
/**
* Store a mnemonic in the Stronghold snapshot.
*/
async storeMnemonic(mnemonic: string): Promise<void> {
await this.methodHandler.callMethod({
name: 'storeMnemonic',
data: { mnemonic },
});
}
/**
* Update the authentication for the provided node.
*/
async updateNodeAuth(url: string, auth?: Auth): Promise<void> {
await this.methodHandler.callMethod({
name: 'updateNodeAuth',
data: { url, auth },
});
}
/**
* Returns the accounts of the wallet.
*
* @returns The accounts of the wallet.
*/
async accounts(): Promise<OutputWithExtendedMetadata[]> {
const response = await this.methodHandler.callMethod({
name: 'accounts',
});
const parsed = JSON.parse(response) as Response<
OutputWithExtendedMetadata[]
>;
return plainToInstance(OutputWithExtendedMetadata, parsed.payload);
}
/**
* A generic function that can be used to burn native tokens, nfts, foundries and accounts.
* @param burn The outputs or native tokens to burn
* @param transactionOptions Additional transaction options.
* @returns The transaction.
*/
async burn(
burn: Burn,
transactionOptions?: TransactionOptions,
): Promise<TransactionWithMetadata> {
return (await this.prepareBurn(burn, transactionOptions)).send();
}
/**
* A generic function that can be used to prepare to burn native tokens, nfts, foundries and accounts.
* @param burn The outputs or native tokens to burn
* @param transactionOptions Additional transaction options
* @returns The prepared transaction.
*/
async prepareBurn(
burn: Burn,
transactionOptions?: TransactionOptions,
): Promise<PreparedTransaction> {
const response = await this.methodHandler.callMethod({
name: 'prepareBurn',
data: {
burn,
options: transactionOptions,
},
});
const parsed = JSON.parse(
response,
) as Response<PreparedTransactionData>;
return new PreparedTransaction(
plainToInstance(PreparedTransactionData, parsed.payload),
this,
);
}
/**
* Burn native tokens. This doesn't require the foundry output which minted them, but will not increase
* the foundries `melted_tokens` field, which makes it impossible to destroy the foundry output. Therefore it's
* recommended to use melting, if the foundry output is available.
* @param tokenId The native token id.
* @param burnAmount The to be burned amount.
* @param transactionOptions Additional transaction options.
* @returns The prepared transaction.
*/
async prepareBurnNativeToken(
tokenId: TokenId,
burnAmount: u256,
transactionOptions?: TransactionOptions,
): Promise<PreparedTransaction> {
const response = await this.methodHandler.callMethod({
name: 'prepareBurn',
data: {
burn: {
nativeTokens: new Map([[tokenId, burnAmount]]),
},
options: transactionOptions,
},
});
const parsed = JSON.parse(
response,
) as Response<PreparedTransactionData>;
return new PreparedTransaction(
plainToInstance(PreparedTransactionData, parsed.payload),
this,
);
}
/**
* Burn an nft output.
* @param nftId The NftId.
* @param transactionOptions Additional transaction options.
* @returns The prepared transaction.
*/
async prepareBurnNft(
nftId: NftId,
transactionOptions?: TransactionOptions,
): Promise<PreparedTransaction> {
const response = await this.methodHandler.callMethod({
name: 'prepareBurn',
data: {
burn: {
nfts: [nftId],
},
options: transactionOptions,
},
});
const parsed = JSON.parse(
response,
) as Response<PreparedTransactionData>;
return new PreparedTransaction(
plainToInstance(PreparedTransactionData, parsed.payload),
this,
);
}
/**
* Claim basic or nft outputs that have additional unlock conditions
* to their `AddressUnlockCondition` from the wallet.
* @param outputIds The outputs to claim.
* @returns The resulting transaction.
*/
async claimOutputs(
outputIds: OutputId[],
): Promise<TransactionWithMetadata> {
return (await this.prepareClaimOutputs(outputIds)).send();
}
/**
* Claim basic or nft outputs that have additional unlock conditions
* to their `AddressUnlockCondition` from the wallet.
* @param outputIds The outputs to claim.
* @returns The prepared transaction.
*/
async prepareClaimOutputs(
outputIds: OutputId[],
): Promise<PreparedTransaction> {
const response = await this.methodHandler.callMethod({
name: 'prepareClaimOutputs',
data: {
outputIdsToClaim: outputIds,
},
});
const parsed = JSON.parse(
response,
) as Response<PreparedTransactionData>;
return new PreparedTransaction(
plainToInstance(PreparedTransactionData, parsed.payload),
this,
);
}
/**
* Consolidate basic outputs with only an `AddressUnlockCondition` from a wallet
* by sending them to an own address again if the output amount is greater or
* equal to the output consolidation threshold.
* @param params Consolidation options.
* @returns The consolidation transaction.
*/
async consolidateOutputs(
params: ConsolidationParams,
): Promise<TransactionWithMetadata> {
return (await this.prepareConsolidateOutputs(params)).send();
}
/**
* Consolidate basic outputs with only an `AddressUnlockCondition` from a wallet
* by sending them to an own address again if the output amount is greater or
* equal to the output consolidation threshold.
* @param params Consolidation options.
* @returns The prepared consolidation transaction.
*/
async prepareConsolidateOutputs(
params: ConsolidationParams,
): Promise<PreparedTransaction> {
const response = await this.methodHandler.callMethod({
name: 'prepareConsolidateOutputs',
data: {
params,
},
});
const parsed = JSON.parse(
response,
) as Response<PreparedTransactionData>;
return new PreparedTransaction(
plainToInstance(PreparedTransactionData, parsed.payload),
this,
);
}
/**
* Creates an account output.
* @param params The account output options.
* @param transactionOptions Additional transaction options.
* @returns The transaction.
*/
async createAccountOutput(
params?: AccountOutputParams,
transactionOptions?: TransactionOptions,
): Promise<TransactionWithMetadata> {
return (
await this.prepareCreateAccountOutput(params, transactionOptions)
).send();
}
/**
* Creates an account output.
* @param params The account output options.
* @param transactionOptions Additional transaction options.
* @returns The prepared transaction.
*/
async prepareCreateAccountOutput(
params?: AccountOutputParams,
transactionOptions?: TransactionOptions,
): Promise<PreparedTransaction> {
const response = await this.methodHandler.callMethod({
name: 'prepareCreateAccountOutput',
data: {
params,
options: transactionOptions,
},
});
const parsed = JSON.parse(
response,
) as Response<PreparedTransactionData>;
return new PreparedTransaction(
plainToInstance(PreparedTransactionData, parsed.payload),
this,
);
}
/**
* Melt native tokens. This happens with the foundry output which minted them, by increasing its
* `melted_tokens` field.
* @param tokenId The native token id.
* @param meltAmount To be melted amount.
* @param transactionOptions Additional transaction options.
* @returns The transaction.
*/
async meltNativeToken(
tokenId: TokenId,
meltAmount: bigint,
transactionOptions?: TransactionOptions,
): Promise<TransactionWithMetadata> {
return (
await this.prepareMeltNativeToken(
tokenId,
meltAmount,
transactionOptions,
)
).send();
}
/**
* Melt native tokens. This happens with the foundry output which minted them, by increasing its
* `melted_tokens` field.
* @param tokenId The native token id.
* @param meltAmount To be melted amount.
* @param transactionOptions Additional transaction options.
* @returns The prepared transaction.
*/
async prepareMeltNativeToken(
tokenId: TokenId,
meltAmount: u256,
transactionOptions?: TransactionOptions,
): Promise<PreparedTransaction> {
const response = await this.methodHandler.callMethod({
name: 'prepareMeltNativeToken',
data: {
tokenId,
meltAmount: bigIntToHex(meltAmount),
options: transactionOptions,
},
});
const parsed = JSON.parse(
response,
) as Response<PreparedTransactionData>;
return new PreparedTransaction(
plainToInstance(PreparedTransactionData, parsed.payload),
this,
);
}
/**
* Deregister a participation event.
*
* @param eventId The id of the participation event to deregister.
*/
async deregisterParticipationEvent(
eventId: ParticipationEventId,
): Promise<void> {
const response = await this.methodHandler.callMethod({
name: 'deregisterParticipationEvent',
data: {
eventId,
},
});
return JSON.parse(response).payload;
}
/**
* Destroy an account output.
*
* @param accountId The AccountId.
* @param transactionOptions Additional transaction options.
* @returns The prepared transaction.
*/
async prepareDestroyAccount(
accountId: AccountId,
transactionOptions?: TransactionOptions,
): Promise<PreparedTransaction> {
const response = await this.methodHandler.callMethod({
name: 'prepareBurn',
data: {
burn: {
accounts: [accountId],
},
options: transactionOptions,
},
});
const parsed = JSON.parse(
response,
) as Response<PreparedTransactionData>;
return new PreparedTransaction(
plainToInstance(PreparedTransactionData, parsed.payload),
this,
);
}
/**
* Function to destroy a foundry output with a circulating supply of 0.
* Native tokens in the foundry (minted by other foundries) will be transacted to the controlling account.
* @param foundryId The FoundryId.
* @param transactionOptions Additional transaction options.
* @returns The prepared transaction.
*/
async prepareDestroyFoundry(
foundryId: FoundryId,
transactionOptions?: TransactionOptions,
): Promise<PreparedTransaction> {
const response = await this.methodHandler.callMethod({
name: 'prepareBurn',
data: {
burn: {
foundries: [foundryId],
},
options: transactionOptions,
},
});
const parsed = JSON.parse(
response,
) as Response<PreparedTransactionData>;
return new PreparedTransaction(
plainToInstance(PreparedTransactionData, parsed.payload),
this,
);
}
/**
* Get the account balance.
*
* @returns The account balance.
*/
async getBalance(): Promise<Balance> {
const response = await this.methodHandler.callMethod({
name: 'getBalance',
});
const payload = JSON.parse(response).payload;
return this.adjustBalancePayload(payload);
}
/**
* Converts hex encoded or decimal strings of amounts to `bigint`
* for the balance payload.
*/
private adjustBalancePayload(payload: any): Balance {
for (let i = 0; i < payload.nativeTokens.length; i++) {
payload.nativeTokens[i].total = hexToBigInt(
payload.nativeTokens[i].total,
);
payload.nativeTokens[i].available = hexToBigInt(
payload.nativeTokens[i].available,
);
}
payload.baseCoin.total = BigInt(payload.baseCoin.total);
payload.baseCoin.available = BigInt(payload.baseCoin.available);
payload.requiredStorageDeposit.account = BigInt(
payload.requiredStorageDeposit.account,
);
payload.requiredStorageDeposit.basic = BigInt(
payload.requiredStorageDeposit.basic,
);
payload.requiredStorageDeposit.foundry = BigInt(
payload.requiredStorageDeposit.foundry,
);
payload.requiredStorageDeposit.nft = BigInt(
payload.requiredStorageDeposit.nft,
);
return payload;
}
/**
* Get the data for an output.
* @param outputId The output to get.
* @returns The `OutputWithExtendedMetadata`.
*/
async getOutput(outputId: OutputId): Promise<OutputWithExtendedMetadata> {
const response = await this.methodHandler.callMethod({
name: 'getOutput',
data: {
outputId,
},
});
const parsed = JSON.parse(
response,
) as Response<OutputWithExtendedMetadata>;
return plainToInstance(OutputWithExtendedMetadata, parsed.payload);
}
/**
* Get a participation event.
*
* @param eventId The ID of the event to get.
*/
async getParticipationEvent(
eventId: ParticipationEventId,
): Promise<ParticipationEventWithNodes> {
const response = await this.methodHandler.callMethod({
name: 'getParticipationEvent',
data: {
eventId,
},
});
return JSON.parse(response).payload;
}
/**
* Get IDs of participation events of a certain type.
*
* @param node The node to get events from.
* @param eventType The type of events to get.
*/
async getParticipationEventIds(
node: Node,
eventType?: ParticipationEventType,
): Promise<ParticipationEventId[]> {
const response = await this.methodHandler.callMethod({
name: 'getParticipationEventIds',
data: {
node,
eventType,
},
});
return JSON.parse(response).payload;
}
/**
* Get all participation events.
*/
async getParticipationEvents(): Promise<ParticipationEventMap> {
const response = await this.methodHandler.callMethod({
name: 'getParticipationEvents',
});
return JSON.parse(response).payload;
}
/**
* Get the participation event status by its ID.
*
* @param eventId The ID of the event status to get.
*/
async getParticipationEventStatus(
eventId: ParticipationEventId,
): Promise<ParticipationEventStatus> {
const response = await this.methodHandler.callMethod({
name: 'getParticipationEventStatus',
data: {
eventId,
},
});
return JSON.parse(response).payload;
}
/**
* Get a `FoundryOutput` by native token ID. It will try to get the foundry from
* the account, if it isn't in the wallet it will try to get it from the node.
*
* @param tokenId The native token ID to get the foundry for.
* @returns The `FoundryOutput` that minted the token.
*/
async getFoundryOutput(tokenId: TokenId): Promise<FoundryOutput> {
const response = await this.methodHandler.callMethod({
name: 'getFoundryOutput',
data: {
tokenId,
},
});
return Output.parse(JSON.parse(response).payload) as FoundryOutput;
}
/**
* Get outputs with additional unlock conditions.
*
* @param outputs The type of outputs to claim.
* @returns The output IDs of the unlockable outputs.
*/
async claimableOutputs(outputs: OutputsToClaim): Promise<OutputId[]> {
const response = await this.methodHandler.callMethod({
name: 'claimableOutputs',
data: {
outputsToClaim: outputs,
},
});
return JSON.parse(response).payload;
}
/**
* Get a transaction stored in the wallet.
*
* @param transactionId The ID of the transaction to get.
* @returns The transaction.
*/
async getTransaction(
transactionId: TransactionId,
): Promise<TransactionWithMetadata> {
const response = await this.methodHandler.callMethod({
name: 'getTransaction',
data: {
transactionId,
},
});
const parsed = JSON.parse(
response,
) as Response<TransactionWithMetadata>;
return plainToInstance(TransactionWithMetadata, parsed.payload);
}
/**
* Get the transaction with inputs of an incoming transaction stored in the wallet
* List might not be complete, if the node pruned the data already
*
* @param transactionId The ID of the transaction to get.
* @returns The transaction.
*/
async getIncomingTransaction(
transactionId: TransactionId,
): Promise<TransactionWithMetadata> {
const response = await this.methodHandler.callMethod({
name: 'getIncomingTransaction',
data: {
transactionId,
},
});
const parsed = JSON.parse(
response,
) as Response<TransactionWithMetadata>;
return plainToInstance(TransactionWithMetadata, parsed.payload);
}
/**
* Get the address of the wallet.
*
* @returns The address.
*/
async address(): Promise<Bech32Address> {
const response = await this.methodHandler.callMethod({
name: 'getAddress',
});
return JSON.parse(response).payload;
}
/**
* List all outputs of the wallet.
*
* @param filterOptions Options to filter the to be returned outputs.
* @returns The outputs with metadata.
*/
async outputs(
filterOptions?: FilterOptions,
): Promise<OutputWithExtendedMetadata[]> {
const response = await this.methodHandler.callMethod({
name: 'outputs',
data: { filterOptions },
});
const parsed = JSON.parse(response) as Response<
OutputWithExtendedMetadata[]
>;
return plainToInstance(OutputWithExtendedMetadata, parsed.payload);
}
/**
* List all the pending transactions of the wallet.
*
* @returns The transactions.
*/
async pendingTransactions(): Promise<TransactionWithMetadata[]> {
const response = await this.methodHandler.callMethod({
name: 'pendingTransactions',
});
const parsed = JSON.parse(response) as Response<
TransactionWithMetadata[]
>;
return plainToInstance(TransactionWithMetadata, parsed.payload);
}
/**
* Returns the implicit account creation address of the wallet if it is Ed25519 based.
*
* @returns The implicit account creation address.
*/
async implicitAccountCreationAddress(): Promise<Bech32Address> {
const response = await this.methodHandler.callMethod({
name: 'implicitAccountCreationAddress',
});
return JSON.parse(response).payload;
}
/**
* Transitions an implicit account to an account.
*
* @param outputId Identifier of the implicit account output.
* @returns The created transaction.
*/
async implicitAccountTransition(
outputId: OutputId,
): Promise<TransactionWithMetadata> {
return (await this.prepareImplicitAccountTransition(outputId)).send();
}
/**
* Prepares to transition an implicit account to an account.
*
* @param outputId Identifier of the implicit account output.
* @returns The prepared transaction.
*/
async prepareImplicitAccountTransition(
outputId: OutputId,
): Promise<PreparedTransaction> {
const response = await this.methodHandler.callMethod({
name: 'prepareImplicitAccountTransition',
data: { outputId },
});
const parsed = JSON.parse(
response,
) as Response<PreparedTransactionData>;
return new PreparedTransaction(
plainToInstance(PreparedTransactionData, parsed.payload),
this,
);
}
/**
* Returns the implicit accounts of the wallet.
*
* @returns The implicit accounts of the wallet.
*/
async implicitAccounts(): Promise<OutputWithExtendedMetadata[]> {
const response = await this.methodHandler.callMethod({
name: 'implicitAccounts',
});
const parsed = JSON.parse(response) as Response<
OutputWithExtendedMetadata[]
>;
return plainToInstance(OutputWithExtendedMetadata, parsed.payload);
}
/**
* List all incoming transactions of the wallet.
*
* @returns The incoming transactions with their inputs.
*/
async incomingTransactions(): Promise<TransactionWithMetadata[]> {
const response = await this.methodHandler.callMethod({
name: 'incomingTransactions',
});
const parsed = JSON.parse(response) as Response<
TransactionWithMetadata[]
>;
return plainToInstance(TransactionWithMetadata, parsed.payload);
}
/**
* List all the transactions of the wallet.
*
* @returns The transactions.
*/
async transactions(): Promise<TransactionWithMetadata[]> {
const response = await this.methodHandler.callMethod({
name: 'transactions',
});
const parsed = JSON.parse(response) as Response<
TransactionWithMetadata[]
>;
return plainToInstance(TransactionWithMetadata, parsed.payload);