-
Notifications
You must be signed in to change notification settings - Fork 12
/
index.js
2539 lines (2265 loc) · 64.1 KB
/
index.js
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
/* eslint-disable max-len */
import { NativeModules } from 'react-native';
import { decode as base64_decode, encode as base64_encode } from 'base-64';
const { HaskellShelley } = NativeModules;
// export default HaskellShelley;
function Uint8ArrayFromB64(base64_string) {
return Uint8Array.from(base64_decode(base64_string), c => c.charCodeAt(0));
}
function b64FromUint8Array(uint8Array) {
return base64_encode(String.fromCharCode.apply(null, uint8Array));
}
class Ptr {
static _wrap(ptr, klass) {
if (ptr === '0') {
return undefined;
}
const obj = Object.create(klass.prototype);
obj.ptr = ptr;
return obj;
}
static _assertClass(ptr, klass) {
if (!(ptr instanceof klass)) {
throw new Error(`expected instance of ${klass.name}`);
}
return ptr.ptr;
}
constructor() {
throw new Error("Can't be initialized with constructor");
}
/**
* Frees the pointer
* @returns {Promise<void>}
*/
async free() {
if (!this.ptr) {
return;
}
const ptr = this.ptr;
this.ptr = null;
await HaskellShelley.ptrFree(ptr);
}
}
/**
* @param {string} password
* @param {string} salt
* @param {string} nonce
* @param {string} data
* @returns {Promise<string>}
*/
export const encrypt_with_password = async (
password,
salt,
nonce,
data,
) => {
return await HaskellShelley.encryptWithPassword(password, salt, nonce, data);
};
/**
* @param {string} password
* @param {string} data
* @returns {Promise<string>}
*/
export const decrypt_with_password = async (password, data) => {
return await HaskellShelley.decryptWithPassword(password, data);
};
/**
* @param {TransactionHash} txBodyHash
* @param {ByronAddress} addr
* @param {Bip32PrivateKey} key
* @returns {Promise<BootstrapWitness>}
*/
export const make_icarus_bootstrap_witness = async (txBodyHash, addr, key) => {
const txBodyHashPtr = Ptr._assertClass(txBodyHash, TransactionHash);
const addrPtr = Ptr._assertClass(addr, ByronAddress);
const keyPtr = Ptr._assertClass(key, Bip32PrivateKey);
const ret = await HaskellShelley.makeIcarusBootstrapWitness(txBodyHashPtr, addrPtr, keyPtr);
return Ptr._wrap(ret, BootstrapWitness);
};
/**
* @param {TransactionHash} txBodyHash
* @param {PrivateKey} sk
* @returns {Promise<Vkeywitness>}
*/
export const make_vkey_witness = async (txBodyHash, sk) => {
const txBodyHashPtr = Ptr._assertClass(txBodyHash, TransactionHash);
const skPtr = Ptr._assertClass(sk, PrivateKey);
const ret = await HaskellShelley.makeVkeyWitness(txBodyHashPtr, skPtr);
return Ptr._wrap(ret, Vkeywitness);
};
/**
* @param {Value} assets
* @param {BigNum} minUtxoVal
* @returns {Promise<BigNum>}
*/
export const min_ada_required = async (assets, minUtxoVal) => {
const assetsPtr = Ptr._assertClass(assets, Value);
const minUtxoValPtr = Ptr._assertClass(minUtxoVal, BigNum);
const ret = await HaskellShelley.minAdaRequired(assetsPtr, minUtxoValPtr);
return Ptr._wrap(ret, BigNum);
};
/**
* @param {string} json
* @param {MetadataJsonSchema} schema
* @returns {Promise<TransactionMetadatum>}
*/
export const encode_json_str_to_metadatum = async (json, schema) => {
const ret = await HaskellShelley.encodeJsonStrToMetadatum(json, schema);
return Ptr._wrap(ret, TransactionMetadatum);
};
/**
* @param {TransactionMetadatum} metadatum
* @param {MetadataJsonSchema} schema
* @returns {Promise<string>}
*/
export const decode_metadatum_to_json_str = async (metadatum, schema) => {
const metadatumPtr = Ptr._assertClass(metadatum, TransactionMetadatum);
return await HaskellShelley.decodeMetadatumToJsonStr(metadatumPtr, schema);
};
/**
* @param {TransactionBody} txBody
* @returns {Promise<TransactionHash>}
*/
export const hash_transaction = async (txBody) => {
const txBodyPtr = Ptr._assertClass(txBody, TransactionBody);
const ret = await HaskellShelley.hashTransaction(txBodyPtr);
return Ptr._wrap(ret, TransactionHash);
};
export class BigNum extends Ptr {
/**
* @param {string} string
* @returns {Promise<BigNum>}
*/
static async from_str(string) {
const ret = await HaskellShelley.bigNumFromStr(string);
return Ptr._wrap(ret, BigNum);
}
/**
* String representation of the BigNum value for use from environments
* that don't support BigInt
* @returns {Promise<string>}
*/
async to_str() {
return await HaskellShelley.bigNumToStr(this.ptr);
}
/**
* @param {BigNum} other
* @returns {Promise<BigNum>}
*/
async checked_add(other) {
const otherPtr = Ptr._assertClass(other, BigNum);
const ret = await HaskellShelley.bigNumCheckedAdd(this.ptr, otherPtr);
return Ptr._wrap(ret, BigNum);
}
/**
* @param {BigNum} other
* @returns {Promise<BigNum>}
*/
async checked_sub(other) {
const otherPtr = Ptr._assertClass(other, BigNum);
const ret = await HaskellShelley.bigNumCheckedSub(this.ptr, otherPtr);
return Ptr._wrap(ret, BigNum);
}
/**
* @param {BigNum} other
* @returns {Promise<BigNum>}
*/
async clamped_sub(other) {
const otherPtr = Ptr._assertClass(other, BigNum);
const ret = await HaskellShelley.bigNumClampedSub(this.ptr, otherPtr);
return Ptr._wrap(ret, BigNum);
}
/**
* @param {BigNum} rhs
* @returns {Promise<number | undefined>}
*/
async compare(rhs) {
const rhsPtr = Ptr._assertClass(rhs, BigNum);
return await HaskellShelley.bigNumCompare(this.ptr, rhsPtr);
}
}
export const Coin = BigNum;
export class Value extends Ptr {
/**
* @param {BigNum} coin
* @returns {Promise<Value>}
*/
static async new(coin) {
const coinPtr = Ptr._assertClass(coin, BigNum);
const ret = await HaskellShelley.valueNew(coinPtr);
return Ptr._wrap(ret, Value);
}
/**
* TODO: should we return Coin instead?
* @returns {Promise<BigNum>}
*/
async coin() {
const ret = await HaskellShelley.valueCoin(this.ptr);
return Ptr._wrap(ret, BigNum);
}
/**
* @param {BigNum} coin
* @returns {Promise<void>}
*/
async set_coin(coin) {
// recall: we expose Coin as a reference to BigNum
const coinPtr = Ptr._assertClass(coin, BigNum);
return await HaskellShelley.valueSetCoin(this.ptr, coinPtr);
}
/**
* @returns {Promise<MultiAsset | undefined>}
*/
async multiasset() {
const ret = await HaskellShelley.valueMultiasset(this.ptr);
return Ptr._wrap(ret, MultiAsset);
}
/**
* @param {MultiAsset} multiasset
* @returns {Promise<void>}
*/
async set_multiasset(multiasset) {
const multiassetPtr = Ptr._assertClass(multiasset, MultiAsset);
return await HaskellShelley.valueSetMultiasset(this.ptr, multiassetPtr);
}
/**
* @param {Value} rhs
* @returns {Promise<Value>}
*/
async checked_add(rhs) {
const rhsPtr = Ptr._assertClass(rhs, Value);
const ret = await HaskellShelley.valueCheckedAdd(this.ptr, rhsPtr);
return Ptr._wrap(ret, Value);
}
/**
* @param {Value} rhs
* @returns {Promise<Value>}
*/
async checked_sub(rhs) {
const rhsPtr = Ptr._assertClass(rhs, Value);
const ret = await HaskellShelley.valueCheckedSub(this.ptr, rhsPtr);
return Ptr._wrap(ret, Value);
}
/**
* @param {Value} rhs
* @returns {Promise<Value>}
*/
async clamped_sub(rhs) {
const rhsPtr = Ptr._assertClass(rhs, Value);
const ret = await HaskellShelley.valueClampedSub(this.ptr, rhsPtr);
return Ptr._wrap(ret, Value);
}
/**
* @param {Value} rhs
* @returns {Promise<number | undefined>}
*/
async compare(rhs) {
const rhsPtr = Ptr._assertClass(rhs, Value);
return await HaskellShelley.valueCompare(this.ptr, rhsPtr);
}
}
export class Int extends Ptr {
/**
* @param {BigNum} x
* @returns {Promise<Int>}
*/
static async new(x) {
const ptr = Ptr._assertClass(x, BigNum);
const ret = await HaskellShelley.intNew(ptr);
return Ptr._wrap(ret, Int);
}
/**
* @returns {Promise<number>}
*/
async as_i32() {
return await HaskellShelley.intAsi32(this.ptr);
}
}
export class AssetName extends Ptr {
/**
* @returns {Promise<Uint8Array>}
*/
async to_bytes() {
const b64 = await HaskellShelley.assetNameToBytes(this.ptr);
return Uint8ArrayFromB64(b64);
}
/**
* @param {Uint8Array} bytes
* @returns {Promise<AssetName>}
*/
static async from_bytes(bytes) {
const ret = await HaskellShelley.assetNameFromBytes(b64FromUint8Array(bytes));
return Ptr._wrap(ret, AssetName);
}
/**
* @param {Uint8Array} name
* @returns {Promise<AssetName>}
*/
static async new(name) {
const ret = await HaskellShelley.assetNameNew(b64FromUint8Array(name));
return Ptr._wrap(ret, AssetName);
}
/**
* @returns {Promise<Uint8Array>}
*/
async name() {
const b64 = await HaskellShelley.assetNameName(this.ptr);
return Uint8ArrayFromB64(b64);
}
}
export class AssetNames extends Ptr {
/**
* @returns {Promise<AssetNames>}
*/
static async new() {
const ret = await HaskellShelley.assetNamesNew();
return Ptr._wrap(ret, AssetNames);
}
/**
* @returns {Promise<number>}
*/
async len() {
return HaskellShelley.assetNamesLen(this.ptr);
}
/**
* @param {number} index
* @returns {Promise<AssetName>}
*/
async get(index) {
const ret = await HaskellShelley.assetNamesGet(this.ptr, index);
return Ptr._wrap(ret, AssetName);
}
/**
* @param {AssetName} item
* @returns {Promise<void>}
*/
add(item) {
const itemPtr = Ptr._assertClass(item, AssetName);
return HaskellShelley.assetNamesAdd(this.ptr, itemPtr);
}
}
/**
* ED25519 key used as public key
*/
export class PublicKey extends Ptr {
/**
* Get private key from its bech32 representation
* Example:
* ```javascript
* const pkey = PublicKey.from_bech32("ed25519_pk1dgaagyh470y66p899txcl3r0jaeaxu6yd7z2dxyk55qcycdml8gszkxze2");
* ```
* @param {string} bech32_str
* @returns {Promise<PublicKey>}
*/
static async from_bech32(bech32_str) {
const ret = await HaskellShelley.publicKeyFromBech32(bech32_str);
return Ptr._wrap(ret, PublicKey);
}
/**
* @returns {Promise<string>}
*/
to_bech32() {
return HaskellShelley.publicKeyToBech32(this.ptr);
}
/**
* @param {Uint8Array} bytes
* @returns {Promise<PublicKey>}
*/
static async from_bytes(bytes) {
const ret = await HaskellShelley.publicKeyFromBytes(b64FromUint8Array(bytes));
return Ptr._wrap(ret, PublicKey);
}
/**
* @returns {Promise<Uint8Array>}
*/
async as_bytes() {
const b64 = await HaskellShelley.publicKeyAsBytes(this.ptr);
return Uint8ArrayFromB64(b64);
}
// TODO
// /**
// * @param {Uint8Array} data
// * @param {Ed25519Signature} signature
// * @returns {Promise<boolean>}
// */
// static async verify(data, signature) {
// const signaturePtr = Ptr._assertClass(signature, Ed25519Signature);
// return HaskellShelley.publicKeyVerify(b64FromUint8Array(data), signaturePtr);
// }
/**
* @returns {Promise<Ed25519KeyHash>}
*/
async hash() {
const ret = await HaskellShelley.publicKeyHash(this.ptr);
return Ptr._wrap(ret, Ed25519KeyHash);
}
}
export class PrivateKey extends Ptr {
/**
* @returns {Promise<PublicKey>}
*/
async to_public() {
const ret = await HaskellShelley.privateKeyToPublic(this.ptr);
return Ptr._wrap(ret, PublicKey);
}
/**
* @param {Uint8Array} bytes
* @returns {Promise<PrivateKey>}
*/
static async from_normal_bytes(bytes) {
const ret = await HaskellShelley.privateKeyFromNormalBytes(b64FromUint8Array(bytes));
return Ptr._wrap(ret, PrivateKey);
}
/**
* @returns {Promise<Uint8Array>}
*/
async as_bytes() {
const b64 = await HaskellShelley.privateKeyAsBytes(this.ptr);
return Uint8ArrayFromB64(b64);
}
/**
* @param {Uint8Array} bytes
* @returns {Promise<PrivateKey>}
*/
static async from_extended_bytes(bytes) {
const ret = await HaskellShelley.privateKeyFromExtendedBytes(b64FromUint8Array(bytes));
return Ptr._wrap(ret, PrivateKey);
}
/**
* @param {Uint8Array} message
* @returns {Promise<Ed25519Signature>}
*/
async sign(message) {
const ret = await HaskellShelley.privateKeySign(this.ptr, b64FromUint8Array(message));
return Ptr._wrap(ret, Ed25519Signature);
}
}
export class Bip32PublicKey extends Ptr {
/**
* derive this private key with the given index.
*
* # Security considerations
*
* * hard derivation index cannot be soft derived with the public key
*
* # Hard derivation vs Soft derivation
*
* If you pass an index below 0x80000000 then it is a soft derivation.
* The advantage of soft derivation is that it is possible to derive the
* public key too. I.e. derivation the private key with a soft derivation
* index and then retrieving the associated public key is equivalent to
* deriving the public key associated to the parent private key.
*
* Hard derivation index does not allow public key derivation.
*
* This is why deriving the private key should not fail while deriving
* the public key may fail (if the derivation index is invalid).
* @param {number} index
* @returns {Promise<Bip32PublicKey>}
*/
async derive(index) {
const ret = await HaskellShelley.bip32PublicKeyDerive(this.ptr, index);
return Ptr._wrap(ret, Bip32PublicKey);
}
/**
* @returns {Promise<PublicKey>}
*/
async to_raw_key() {
const ret = await HaskellShelley.bip32PublicKeyToRawKey(this.ptr);
return Ptr._wrap(ret, PublicKey);
}
/**
* @param {Uint8Array} bytes
* @returns {Promise<Bip32PublicKey>}
*/
static async from_bytes(bytes) {
const ret = await HaskellShelley.bip32PublicKeyFromBytes(b64FromUint8Array(bytes));
return Ptr._wrap(ret, Bip32PublicKey);
}
/**
* @returns {Promise<Uint8Array>}
*/
async as_bytes() {
const b64 = await HaskellShelley.bip32PublicKeyAsBytes(this.ptr);
return Uint8ArrayFromB64(b64);
}
/**
* @param {string} bech32Str
* @returns {Promise<Bip32PublicKey>}
*/
static async from_bech32(bech32Str) {
const ret = await HaskellShelley.bip32PublicKeyFromBech32(bech32Str);
return Ptr._wrap(ret, Bip32PublicKey);
}
/**
* @returns {Promise<string>}
*/
to_bech32() {
return HaskellShelley.bip32PublicKeyToBech32(this.ptr);
}
/**
* @returns {Promise<Uint8Array>}
*/
async chaincode() {
const b64 = await HaskellShelley.bip32PublicKeyChaincode(this.ptr);
return Uint8ArrayFromB64(b64);
}
}
/**
*/
export class Bip32PrivateKey extends Ptr {
/**
* derive this private key with the given index.
*
* # Security considerations
*
* * hard derivation index cannot be soft derived with the public key
*
* # Hard derivation vs Soft derivation
*
* If you pass an index below 0x80000000 then it is a soft derivation.
* The advantage of soft derivation is that it is possible to derive the
* public key too. I.e. derivation the private key with a soft derivation
* index and then retrieving the associated public key is equivalent to
* deriving the public key associated to the parent private key.
*
* Hard derivation index does not allow public key derivation.
*
* This is why deriving the private key should not fail while deriving
* the public key may fail (if the derivation index is invalid).
* @param {number} index
* @returns {Promise<Bip32PrivateKey>}
*/
async derive(index) {
const ret = await HaskellShelley.bip32PrivateKeyDerive(this.ptr, index);
return Ptr._wrap(ret, Bip32PrivateKey);
}
/**
* @returns {Promise<Bip32PrivateKey>}
*/
static async generate_ed25519_bip32() {
const ret = await HaskellShelley.bip32PrivateKeyGenerateEd25519Bip32();
return Ptr._wrap(ret, Bip32PrivateKey);
}
/**
* @returns {Promise<PrivateKey>}
*/
async to_raw_key() {
const ret = await HaskellShelley.bip32PrivateKeyToRawKey(this.ptr);
return Ptr._wrap(ret, PrivateKey);
}
/**
* @returns {Promise<Bip32PublicKey>}
*/
async to_public() {
const ret = await HaskellShelley.bip32PrivateKeyToPublic(this.ptr);
return Ptr._wrap(ret, Bip32PublicKey);
}
/**
* @param {Uint8Array} bytes
* @returns {Promise<Bip32PrivateKey>}
*/
static async from_bytes(bytes) {
const ret = await HaskellShelley.bip32PrivateKeyFromBytes(b64FromUint8Array(bytes));
return Ptr._wrap(ret, Bip32PrivateKey);
}
/**
* @returns {Promise<Uint8Array>}
*/
async as_bytes() {
const b64 = await HaskellShelley.bip32PrivateKeyAsBytes(this.ptr);
return Uint8ArrayFromB64(b64);
}
/**
* @param {string} bech32Str
* @returns {Promise<Bip32PrivateKey>}
*/
static async from_bech32(bech32Str) {
const ret = await HaskellShelley.bip32PrivateKeyFromBech32(bech32Str);
return Ptr._wrap(ret, Bip32PrivateKey);
}
/**
* @returns {Promise<string>}
*/
to_bech32() {
return HaskellShelley.bip32PrivateKeyToBech32(this.ptr);
}
/**
* @param {Uint8Array} entropy
* @param {Uint8Array} password
* @returns {Promise<Bip32PrivateKey>}
*/
static async from_bip39_entropy(entropy, password) {
const ret = await HaskellShelley.bip32PrivateKeyFromBip39Entropy(b64FromUint8Array(entropy), b64FromUint8Array(password));
return Ptr._wrap(ret, Bip32PrivateKey);
}
}
export class ByronAddress extends Ptr {
/**
* @returns {Promise<string>}
*/
async to_base58() {
return HaskellShelley.byronAddressToBase58(this.ptr);
}
/**
* @param {string} string
* @returns {Promise<ByronAddress>}
*/
static async from_base58(string) {
const ret = await HaskellShelley.byronAddressFromBase58(string);
return Ptr._wrap(ret, ByronAddress);
}
/**
* @param {string} string
* @returns {Promise<boolean>}
*/
static async is_valid(string) {
return HaskellShelley.byronAddressIsValid(string);
}
/**
* @returns {Promise<Address>}
*/
async to_address() {
const ret = await HaskellShelley.byronAddressToAddress(this.ptr);
return Ptr._wrap(ret, Address);
}
/**
* @param {Address} addr
* @returns {Promise<ByronAddress | undefined>}
*/
static async from_address(addr) {
const addrPtr = Ptr._assertClass(addr, Address);
const ret = await HaskellShelley.byronAddressFromAddress(addrPtr);
return Ptr._wrap(ret, ByronAddress);
}
/**
* @returns {Promise<number>}
*/
byron_protocol_magic() {
return HaskellShelley.byronAddressByronProtocolMagic(this.ptr);
}
/**
* @returns {Promise<Uint8Array>}
*/
async attributes() {
const b64 = await HaskellShelley.byronAddressAttributes(this.ptr);
return Uint8ArrayFromB64(b64);
}
/**
* @param {Bip32PublicKey} key
* @param {number} protocolMagic
* @returns {Promise<ByronAddress>}
*/
static async icarus_from_key(key, protocolMagic) {
const keyPtr = Ptr._assertClass(key, Bip32PublicKey);
const ret = await HaskellShelley.byronAddressIcarusFromKey(keyPtr, protocolMagic);
return Ptr._wrap(ret, ByronAddress);
}
}
export class Address extends Ptr {
/**
* @returns {Promise<Uint8Array>}
*/
async to_bytes() {
const b64 = await HaskellShelley.addressToBytes(this.ptr);
return Uint8ArrayFromB64(b64);
}
/**
* @param {Uint8Array} bytes
* @returns {Promise<Address>}
*/
static async from_bytes(bytes) {
const ret = await HaskellShelley.addressFromBytes(b64FromUint8Array(bytes));
return Ptr._wrap(ret, Address);
}
/**
* @param {string | void} prefix
* @returns {Promise<string>}
*/
to_bech32(prefix) {
if (prefix == null)
return HaskellShelley.addressToBech32(this.ptr);
return HaskellShelley.addressToBech32WithPrefix(this.ptr, prefix);
}
/**
* @param {string} string
* @returns {Promise<Address>}
*/
static async from_bech32(string) {
const ret = await HaskellShelley.addressFromBech32(string);
return Ptr._wrap(ret, Address);
}
/**
* @returns {Promise<number>}
*/
network_id() {
return HaskellShelley.addressNetworkId(this.ptr);
}
}
export class Ed25519Signature extends Ptr {
/**
* @returns {Promise<Uint8Array>}
*/
async to_bytes() {
const b64 = await HaskellShelley.ed25519SignatureToBytes(this.ptr);
return Uint8ArrayFromB64(b64);
}
/**
* @param {Uint8Array} bytes
* @returns {Promise<Ed25519Signature>}
*/
static async from_bytes(bytes) {
const ret = await HaskellShelley.ed25519SignatureFromBytes(b64FromUint8Array(bytes));
return Ptr._wrap(ret, Ed25519Signature);
}
/**
* @returns {Promise<string>}
*/
async to_hex() {
return await HaskellShelley.ed25519SignatureToHex(this.ptr);
}
}
export class Ed25519KeyHash extends Ptr {
/**
* @returns {Promise<Uint8Array>}
*/
async to_bytes() {
const b64 = await HaskellShelley.ed25519KeyHashToBytes(this.ptr);
return Uint8ArrayFromB64(b64);
}
/**
* @param {Uint8Array} bytes
* @returns {Promise<Ed25519KeyHash>}
*/
static async from_bytes(bytes) {
const ret = await HaskellShelley.ed25519KeyHashFromBytes(b64FromUint8Array(bytes));
return Ptr._wrap(ret, Ed25519KeyHash);
}
}
export class ScriptHash extends Ptr {
/**
* @returns {Promise<Uint8Array>}
*/
async to_bytes() {
const b64 = await HaskellShelley.scriptHashToBytes(this.ptr);
return Uint8ArrayFromB64(b64);
}
/**
* @param {Uint8Array} bytes
* @returns {Promise<ScriptHash>}
*/
static async from_bytes(bytes) {
const ret = await HaskellShelley.scriptHashFromBytes(b64FromUint8Array(bytes));
return Ptr._wrap(ret, ScriptHash);
}
}
export class ScriptHashes extends Ptr {
/**
* @returns {Promise<Uint8Array>}
*/
async to_bytes() {
const b64 = await HaskellShelley.scriptHashesToBytes(this.ptr);
return Uint8ArrayFromB64(b64);
}
/**
* @param {Uint8Array} bytes
* @returns {Promise<ScriptHashes>}
*/
static async from_bytes(bytes) {
const ret = await HaskellShelley.scriptHashesFromBytes(b64FromUint8Array(bytes));
return Ptr._wrap(ret, ScriptHashes);
}
/**
* @returns {Promise<ScriptHashes>}
*/
static async new() {
const ret = await HaskellShelley.scriptHashesNew();
return Ptr._wrap(ret, ScriptHashes);
}
/**
* @returns {Promise<number>}
*/
async len() {
return HaskellShelley.scriptHashesLen(this.ptr);
}
/**
* @param {number} index
* @returns {Promise<ScriptHash>}
*/
async get(index) {
const ret = await HaskellShelley.scriptHashesGet(this.ptr, index);
return Ptr._wrap(ret, ScriptHash);
}
/**
* @param {ScriptHash} item
* @returns {Promise<void>}
*/
add(item) {
const itemPtr = Ptr._assertClass(item, ScriptHash);
return HaskellShelley.scriptHashesAdd(this.ptr, itemPtr);
}
}
/**
* PolicyID is defined in rust as:
* pub type PolicyID = ScriptHash
* so we just expose a reference to ScriptHash
*/
export const PolicyID = ScriptHash;
export const PolicyIDs = ScriptHashes;
export class Assets extends Ptr {
/**
* @returns {Promise<Assets>}
*/
static async new() {
const ret = await HaskellShelley.assetsNew();
return Ptr._wrap(ret, Assets);
}
/**
* @returns {Promise<number>}
*/
async len() {
return HaskellShelley.assetsLen(this.ptr);
}
/**
* @param {AssetName} key
* @param {BigNum} value
* @returns {Promise<BigNum>}
*/
async insert(key, value) {
const keyPtr = Ptr._assertClass(key, AssetName);
const valuePtr = Ptr._assertClass(value, BigNum);
const ret = await HaskellShelley.assetsInsert(this.ptr, keyPtr, valuePtr);
return Ptr._wrap(ret, BigNum);
}
/**
* @param {AssetName} key
* @returns {Promise<BigNum | undefined>}
*/
async get(key) {
const keyPtr = Ptr._assertClass(key, AssetName);
const ret = await HaskellShelley.assetsGet(this.ptr, keyPtr);
return Ptr._wrap(ret, BigNum);
}
/**
* @returns {Promise<AssetNames>}
*/
async keys() {
const ret = await HaskellShelley.assetsKeys(this.ptr);
return Ptr._wrap(ret, AssetNames);
}
}
export class MultiAsset extends Ptr {
/**
* @returns {Promise<MultiAsset>}
*/
static async new() {
const ret = await HaskellShelley.multiAssetNew();
return Ptr._wrap(ret, MultiAsset);
}
/**
* @returns {Promise<number>}
*/
async len() {
return HaskellShelley.multiAssetLen(this.ptr);
}
/**
* @param {PolicyID} key
* @param {Assets} value
* @returns {Promise<Assets>}
*/
async insert(key, value) {
// recall: PolicyID is a reference to ScriptHash
const keyPtr = Ptr._assertClass(key, ScriptHash);
const valuePtr = Ptr._assertClass(value, Assets);
const ret = await HaskellShelley.multiAssetInsert(this.ptr, keyPtr, valuePtr);
return Ptr._wrap(ret, Assets);
}
/**
* @param {PolicyID} key
* @returns {Promise<Assets | undefined>}
*/
async get(key) {
const keyPtr = Ptr._assertClass(key, PolicyID);
const ret = await HaskellShelley.multiAssetGet(this.ptr, keyPtr);
return Ptr._wrap(ret, Assets);
}
/**
* @returns {Promise<PolicyIDs>}
*/