@amadeus-protocol/sdk 1.0.0

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.
Files changed (87) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +410 -0
  3. package/dist/api/chain.d.ts +119 -0
  4. package/dist/api/chain.d.ts.map +1 -0
  5. package/dist/api/chain.js +147 -0
  6. package/dist/api/chain.js.map +1 -0
  7. package/dist/api/contract.d.ts +62 -0
  8. package/dist/api/contract.d.ts.map +1 -0
  9. package/dist/api/contract.js +76 -0
  10. package/dist/api/contract.js.map +1 -0
  11. package/dist/api/epoch.d.ts +68 -0
  12. package/dist/api/epoch.d.ts.map +1 -0
  13. package/dist/api/epoch.js +99 -0
  14. package/dist/api/epoch.js.map +1 -0
  15. package/dist/api/index.d.ts +8 -0
  16. package/dist/api/index.d.ts.map +1 -0
  17. package/dist/api/index.js +8 -0
  18. package/dist/api/index.js.map +1 -0
  19. package/dist/api/peer.d.ts +80 -0
  20. package/dist/api/peer.d.ts.map +1 -0
  21. package/dist/api/peer.js +95 -0
  22. package/dist/api/peer.js.map +1 -0
  23. package/dist/api/proof.d.ts +25 -0
  24. package/dist/api/proof.d.ts.map +1 -0
  25. package/dist/api/proof.js +30 -0
  26. package/dist/api/proof.js.map +1 -0
  27. package/dist/api/transaction.d.ts +71 -0
  28. package/dist/api/transaction.d.ts.map +1 -0
  29. package/dist/api/transaction.js +85 -0
  30. package/dist/api/transaction.js.map +1 -0
  31. package/dist/api/wallet.d.ts +39 -0
  32. package/dist/api/wallet.d.ts.map +1 -0
  33. package/dist/api/wallet.js +51 -0
  34. package/dist/api/wallet.js.map +1 -0
  35. package/dist/client.d.ts +70 -0
  36. package/dist/client.d.ts.map +1 -0
  37. package/dist/client.js +280 -0
  38. package/dist/client.js.map +1 -0
  39. package/dist/constants.d.ts +42 -0
  40. package/dist/constants.d.ts.map +1 -0
  41. package/dist/constants.js +42 -0
  42. package/dist/constants.js.map +1 -0
  43. package/dist/conversion.d.ts +32 -0
  44. package/dist/conversion.d.ts.map +1 -0
  45. package/dist/conversion.js +50 -0
  46. package/dist/conversion.js.map +1 -0
  47. package/dist/crypto.d.ts +93 -0
  48. package/dist/crypto.d.ts.map +1 -0
  49. package/dist/crypto.js +156 -0
  50. package/dist/crypto.js.map +1 -0
  51. package/dist/encoding.d.ts +108 -0
  52. package/dist/encoding.d.ts.map +1 -0
  53. package/dist/encoding.js +165 -0
  54. package/dist/encoding.js.map +1 -0
  55. package/dist/encryption.d.ts +76 -0
  56. package/dist/encryption.d.ts.map +1 -0
  57. package/dist/encryption.js +137 -0
  58. package/dist/encryption.js.map +1 -0
  59. package/dist/index.d.ts +47 -0
  60. package/dist/index.d.ts.map +1 -0
  61. package/dist/index.js +47 -0
  62. package/dist/index.js.map +1 -0
  63. package/dist/schemas.d.ts +67 -0
  64. package/dist/schemas.d.ts.map +1 -0
  65. package/dist/schemas.js +103 -0
  66. package/dist/schemas.js.map +1 -0
  67. package/dist/sdk.d.ts +100 -0
  68. package/dist/sdk.d.ts.map +1 -0
  69. package/dist/sdk.js +120 -0
  70. package/dist/sdk.js.map +1 -0
  71. package/dist/serialization.d.ts +42 -0
  72. package/dist/serialization.d.ts.map +1 -0
  73. package/dist/serialization.js +247 -0
  74. package/dist/serialization.js.map +1 -0
  75. package/dist/transaction-builder.d.ts +267 -0
  76. package/dist/transaction-builder.d.ts.map +1 -0
  77. package/dist/transaction-builder.js +397 -0
  78. package/dist/transaction-builder.js.map +1 -0
  79. package/dist/types.d.ts +550 -0
  80. package/dist/types.d.ts.map +1 -0
  81. package/dist/types.js +78 -0
  82. package/dist/types.js.map +1 -0
  83. package/dist/validation.d.ts +15 -0
  84. package/dist/validation.d.ts.map +1 -0
  85. package/dist/validation.js +39 -0
  86. package/dist/validation.js.map +1 -0
  87. package/package.json +80 -0
@@ -0,0 +1,32 @@
1
+ /**
2
+ * Token Conversion Utilities
3
+ *
4
+ * This module provides functions for converting between atomic units
5
+ * and human-readable token amounts for the AMA token.
6
+ */
7
+ /**
8
+ * Convert atomic AMA units to human-readable AMA amount
9
+ *
10
+ * @param atomicAma - Atomic units (as number or string)
11
+ * @returns Human-readable AMA amount
12
+ * @throws Error if the value is invalid
13
+ *
14
+ * @example
15
+ * ```ts
16
+ * const ama = fromAtomicAma(1000000000) // Returns 1.0
17
+ * ```
18
+ */
19
+ export declare function fromAtomicAma(atomicAma: number | string): number;
20
+ /**
21
+ * Convert human-readable AMA amount to atomic units
22
+ *
23
+ * @param ama - Human-readable AMA amount
24
+ * @returns Atomic units
25
+ *
26
+ * @example
27
+ * ```ts
28
+ * const atomic = toAtomicAma(1.5) // Returns 1500000000
29
+ * ```
30
+ */
31
+ export declare function toAtomicAma(ama: number): number;
32
+ //# sourceMappingURL=conversion.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"conversion.d.ts","sourceRoot":"","sources":["../src/conversion.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAIH;;;;;;;;;;;GAWG;AACH,wBAAgB,aAAa,CAAC,SAAS,EAAE,MAAM,GAAG,MAAM,GAAG,MAAM,CAgBhE;AAED;;;;;;;;;;GAUG;AACH,wBAAgB,WAAW,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,CAE/C"}
@@ -0,0 +1,50 @@
1
+ /**
2
+ * Token Conversion Utilities
3
+ *
4
+ * This module provides functions for converting between atomic units
5
+ * and human-readable token amounts for the AMA token.
6
+ */
7
+ import { AMA_TOKEN_DECIMALS_MULTIPLIER } from './constants';
8
+ /**
9
+ * Convert atomic AMA units to human-readable AMA amount
10
+ *
11
+ * @param atomicAma - Atomic units (as number or string)
12
+ * @returns Human-readable AMA amount
13
+ * @throws Error if the value is invalid
14
+ *
15
+ * @example
16
+ * ```ts
17
+ * const ama = fromAtomicAma(1000000000) // Returns 1.0
18
+ * ```
19
+ */
20
+ export function fromAtomicAma(atomicAma) {
21
+ const atomicAmaAmount = typeof atomicAma === 'string' ? parseFloat(atomicAma) : atomicAma;
22
+ if (isNaN(atomicAmaAmount) || atomicAmaAmount === 0) {
23
+ return 0;
24
+ }
25
+ if (atomicAmaAmount < 0) {
26
+ throw new Error('Negative value not allowed');
27
+ }
28
+ if (atomicAmaAmount < 1) {
29
+ throw new Error('Value is less than 1');
30
+ }
31
+ if (atomicAmaAmount > Number.MAX_SAFE_INTEGER) {
32
+ throw new Error('Value exceeds maximum safe integer');
33
+ }
34
+ return atomicAmaAmount / AMA_TOKEN_DECIMALS_MULTIPLIER;
35
+ }
36
+ /**
37
+ * Convert human-readable AMA amount to atomic units
38
+ *
39
+ * @param ama - Human-readable AMA amount
40
+ * @returns Atomic units
41
+ *
42
+ * @example
43
+ * ```ts
44
+ * const atomic = toAtomicAma(1.5) // Returns 1500000000
45
+ * ```
46
+ */
47
+ export function toAtomicAma(ama) {
48
+ return ama * AMA_TOKEN_DECIMALS_MULTIPLIER;
49
+ }
50
+ //# sourceMappingURL=conversion.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"conversion.js","sourceRoot":"","sources":["../src/conversion.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,EAAE,6BAA6B,EAAE,MAAM,aAAa,CAAA;AAE3D;;;;;;;;;;;GAWG;AACH,MAAM,UAAU,aAAa,CAAC,SAA0B;IACvD,MAAM,eAAe,GAAG,OAAO,SAAS,KAAK,QAAQ,CAAC,CAAC,CAAC,UAAU,CAAC,SAAS,CAAC,CAAC,CAAC,CAAC,SAAS,CAAA;IAEzF,IAAI,KAAK,CAAC,eAAe,CAAC,IAAI,eAAe,KAAK,CAAC,EAAE,CAAC;QACrD,OAAO,CAAC,CAAA;IACT,CAAC;IACD,IAAI,eAAe,GAAG,CAAC,EAAE,CAAC;QACzB,MAAM,IAAI,KAAK,CAAC,4BAA4B,CAAC,CAAA;IAC9C,CAAC;IACD,IAAI,eAAe,GAAG,CAAC,EAAE,CAAC;QACzB,MAAM,IAAI,KAAK,CAAC,sBAAsB,CAAC,CAAA;IACxC,CAAC;IACD,IAAI,eAAe,GAAG,MAAM,CAAC,gBAAgB,EAAE,CAAC;QAC/C,MAAM,IAAI,KAAK,CAAC,oCAAoC,CAAC,CAAA;IACtD,CAAC;IACD,OAAO,eAAe,GAAG,6BAA6B,CAAA;AACvD,CAAC;AAED;;;;;;;;;;GAUG;AACH,MAAM,UAAU,WAAW,CAAC,GAAW;IACtC,OAAO,GAAG,GAAG,6BAA6B,CAAA;AAC3C,CAAC"}
@@ -0,0 +1,93 @@
1
+ /**
2
+ * Cryptographic Utilities for Amadeus Protocol
3
+ *
4
+ * This module provides cryptographic functions for key generation and BLS12-381 operations
5
+ * used by the Amadeus protocol.
6
+ */
7
+ import type { KeyPair } from './types';
8
+ /**
9
+ * Reduce a 64-byte seed to a 32-byte private key compatible with BLS12-381
10
+ *
11
+ * @param bytes64 - 64-byte seed
12
+ * @returns 32-byte private key
13
+ * @throws Error if input is not exactly 64 bytes
14
+ */
15
+ export declare function reduce512To256LE(bytes64: Uint8Array): Uint8Array;
16
+ /**
17
+ * Derive a keypair from a 64-byte seed
18
+ *
19
+ * @param seed64 - 64-byte seed
20
+ * @returns Tuple of [publicKey, privateKey] as Uint8Array
21
+ *
22
+ * @example
23
+ * ```ts
24
+ * const [pk, sk] = seed64ToKeypair(seed64)
25
+ * ```
26
+ */
27
+ export declare function seed64ToKeypair(seed64: Uint8Array): [Uint8Array, Uint8Array];
28
+ /**
29
+ * Get public key from a 64-byte seed
30
+ *
31
+ * @param seed64 - 64-byte seed
32
+ * @returns Public key as Uint8Array
33
+ *
34
+ * @example
35
+ * ```ts
36
+ * const publicKey = getPublicKey(seed64)
37
+ * ```
38
+ */
39
+ export declare function getPublicKey(seed64: Uint8Array): Uint8Array;
40
+ /**
41
+ * Derive secret key and seed64 from a Base58 encoded seed
42
+ *
43
+ * @param base58Seed64 - Base58 encoded 64-byte seed
44
+ * @returns Object containing sk (secret key) and seed64
45
+ * @throws Error if the seed is invalid
46
+ *
47
+ * @example
48
+ * ```ts
49
+ * const { sk, seed64 } = deriveSkAndSeed64FromBase58Seed(base58Seed)
50
+ * ```
51
+ */
52
+ export declare function deriveSkAndSeed64FromBase58Seed(base58Seed64: string): {
53
+ sk: Uint8Array;
54
+ seed64: Uint8Array;
55
+ };
56
+ /**
57
+ * Derive public key from a Base58 encoded seed
58
+ *
59
+ * @param base58Seed - Base58 encoded 64-byte seed
60
+ * @returns Base58 encoded public key
61
+ * @throws Error if the seed is invalid
62
+ *
63
+ * @example
64
+ * ```ts
65
+ * const publicKey = derivePublicKeyFromSeedBase58(base58Seed)
66
+ * ```
67
+ */
68
+ export declare function derivePublicKeyFromSeedBase58(base58Seed: string): string;
69
+ /**
70
+ * Generate a cryptographically secure random private key (64-byte seed)
71
+ *
72
+ * @returns 64-byte seed as Uint8Array
73
+ *
74
+ * @example
75
+ * ```ts
76
+ * const seed = generatePrivateKey()
77
+ * ```
78
+ */
79
+ export declare function generatePrivateKey(): Uint8Array;
80
+ /**
81
+ * Generate a new keypair
82
+ *
83
+ * @returns KeyPair object with Base58 encoded publicKey and privateKey
84
+ *
85
+ * @example
86
+ * ```ts
87
+ * const keypair = generateKeypair()
88
+ * console.log(keypair.publicKey) // Base58 public key
89
+ * console.log(keypair.privateKey) // Base58 private key (seed)
90
+ * ```
91
+ */
92
+ export declare function generateKeypair(): KeyPair;
93
+ //# sourceMappingURL=crypto.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"crypto.d.ts","sourceRoot":"","sources":["../src/crypto.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAUH,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,SAAS,CAAA;AAMtC;;;;;;GAMG;AACH,wBAAgB,gBAAgB,CAAC,OAAO,EAAE,UAAU,GAAG,UAAU,CAsBhE;AAED;;;;;;;;;;GAUG;AACH,wBAAgB,eAAe,CAAC,MAAM,EAAE,UAAU,GAAG,CAAC,UAAU,EAAE,UAAU,CAAC,CAI5E;AAED;;;;;;;;;;GAUG;AACH,wBAAgB,YAAY,CAAC,MAAM,EAAE,UAAU,GAAG,UAAU,CAG3D;AAED;;;;;;;;;;;GAWG;AACH,wBAAgB,+BAA+B,CAAC,YAAY,EAAE,MAAM,GAAG;IACtE,EAAE,EAAE,UAAU,CAAA;IACd,MAAM,EAAE,UAAU,CAAA;CAClB,CAWA;AAED;;;;;;;;;;;GAWG;AACH,wBAAgB,6BAA6B,CAAC,UAAU,EAAE,MAAM,GAAG,MAAM,CAUxE;AAMD;;;;;;;;;GASG;AACH,wBAAgB,kBAAkB,IAAI,UAAU,CAI/C;AAED;;;;;;;;;;;GAWG;AACH,wBAAgB,eAAe,IAAI,OAAO,CAQzC"}
package/dist/crypto.js ADDED
@@ -0,0 +1,156 @@
1
+ /**
2
+ * Cryptographic Utilities for Amadeus Protocol
3
+ *
4
+ * This module provides cryptographic functions for key generation and BLS12-381 operations
5
+ * used by the Amadeus protocol.
6
+ */
7
+ import { bls12_381 as bls } from '@noble/curves/bls12-381';
8
+ import { AMADEUS_SEED_BYTE_LENGTH } from './constants';
9
+ import { toBase58, fromBase58 } from './encoding';
10
+ // Constant: BLS12-381 group order
11
+ const BLS12_381_ORDER = BigInt('0x73eda753299d7d483339d80809a1d80553bda402fffe5bfeffffffff00000001');
12
+ // ============================================================================
13
+ // BLS12-381 KEY OPERATIONS
14
+ // ============================================================================
15
+ /**
16
+ * Reduce a 64-byte seed to a 32-byte private key compatible with BLS12-381
17
+ *
18
+ * @param bytes64 - 64-byte seed
19
+ * @returns 32-byte private key
20
+ * @throws Error if input is not exactly 64 bytes
21
+ */
22
+ export function reduce512To256LE(bytes64) {
23
+ if (!(bytes64 instanceof Uint8Array) || bytes64.length !== AMADEUS_SEED_BYTE_LENGTH) {
24
+ throw new Error('Expected 64-byte Uint8Array');
25
+ }
26
+ // Convert 64 bytes LE -> BigInt
27
+ let x = BigInt(0);
28
+ for (let i = 0; i < AMADEUS_SEED_BYTE_LENGTH; i++) {
29
+ x += BigInt(bytes64[i]) << (BigInt(8) * BigInt(i));
30
+ }
31
+ // Reduce mod the group order
32
+ x = x % BLS12_381_ORDER;
33
+ // Convert to 32-byte BIG-ENDIAN
34
+ const out = new Uint8Array(32);
35
+ for (let i = 31; i >= 0; i--) {
36
+ out[i] = Number(x & BigInt(0xff));
37
+ x >>= BigInt(8);
38
+ }
39
+ return out;
40
+ }
41
+ /**
42
+ * Derive a keypair from a 64-byte seed
43
+ *
44
+ * @param seed64 - 64-byte seed
45
+ * @returns Tuple of [publicKey, privateKey] as Uint8Array
46
+ *
47
+ * @example
48
+ * ```ts
49
+ * const [pk, sk] = seed64ToKeypair(seed64)
50
+ * ```
51
+ */
52
+ export function seed64ToKeypair(seed64) {
53
+ const sk = reduce512To256LE(seed64);
54
+ const pk = bls.longSignatures.getPublicKey(sk).toBytes();
55
+ return [pk, sk];
56
+ }
57
+ /**
58
+ * Get public key from a 64-byte seed
59
+ *
60
+ * @param seed64 - 64-byte seed
61
+ * @returns Public key as Uint8Array
62
+ *
63
+ * @example
64
+ * ```ts
65
+ * const publicKey = getPublicKey(seed64)
66
+ * ```
67
+ */
68
+ export function getPublicKey(seed64) {
69
+ const [pk] = seed64ToKeypair(seed64);
70
+ return pk;
71
+ }
72
+ /**
73
+ * Derive secret key and seed64 from a Base58 encoded seed
74
+ *
75
+ * @param base58Seed64 - Base58 encoded 64-byte seed
76
+ * @returns Object containing sk (secret key) and seed64
77
+ * @throws Error if the seed is invalid
78
+ *
79
+ * @example
80
+ * ```ts
81
+ * const { sk, seed64 } = deriveSkAndSeed64FromBase58Seed(base58Seed)
82
+ * ```
83
+ */
84
+ export function deriveSkAndSeed64FromBase58Seed(base58Seed64) {
85
+ const seed64 = fromBase58(base58Seed64);
86
+ if (seed64.length !== AMADEUS_SEED_BYTE_LENGTH) {
87
+ throw new Error('Invalid base58Seed: Must be 64 bytes (512 bits) long.');
88
+ }
89
+ const sk = reduce512To256LE(seed64);
90
+ return {
91
+ sk,
92
+ seed64
93
+ };
94
+ }
95
+ /**
96
+ * Derive public key from a Base58 encoded seed
97
+ *
98
+ * @param base58Seed - Base58 encoded 64-byte seed
99
+ * @returns Base58 encoded public key
100
+ * @throws Error if the seed is invalid
101
+ *
102
+ * @example
103
+ * ```ts
104
+ * const publicKey = derivePublicKeyFromSeedBase58(base58Seed)
105
+ * ```
106
+ */
107
+ export function derivePublicKeyFromSeedBase58(base58Seed) {
108
+ if (!base58Seed) {
109
+ throw new Error('Invalid base58Seed: Must be a non-empty base58 string.');
110
+ }
111
+ const seed64 = fromBase58(base58Seed);
112
+ if (seed64.length !== AMADEUS_SEED_BYTE_LENGTH) {
113
+ throw new Error('Invalid base58Seed: Must be 64 bytes (512 bits) long.');
114
+ }
115
+ const publicKey = getPublicKey(seed64);
116
+ return toBase58(publicKey);
117
+ }
118
+ // ============================================================================
119
+ // KEY GENERATION
120
+ // ============================================================================
121
+ /**
122
+ * Generate a cryptographically secure random private key (64-byte seed)
123
+ *
124
+ * @returns 64-byte seed as Uint8Array
125
+ *
126
+ * @example
127
+ * ```ts
128
+ * const seed = generatePrivateKey()
129
+ * ```
130
+ */
131
+ export function generatePrivateKey() {
132
+ const seed = new Uint8Array(AMADEUS_SEED_BYTE_LENGTH);
133
+ crypto.getRandomValues(seed);
134
+ return seed;
135
+ }
136
+ /**
137
+ * Generate a new keypair
138
+ *
139
+ * @returns KeyPair object with Base58 encoded publicKey and privateKey
140
+ *
141
+ * @example
142
+ * ```ts
143
+ * const keypair = generateKeypair()
144
+ * console.log(keypair.publicKey) // Base58 public key
145
+ * console.log(keypair.privateKey) // Base58 private key (seed)
146
+ * ```
147
+ */
148
+ export function generateKeypair() {
149
+ const seed64 = generatePrivateKey();
150
+ const [pk] = seed64ToKeypair(seed64);
151
+ return {
152
+ publicKey: toBase58(pk),
153
+ privateKey: toBase58(seed64)
154
+ };
155
+ }
156
+ //# sourceMappingURL=crypto.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"crypto.js","sourceRoot":"","sources":["../src/crypto.ts"],"names":[],"mappings":"AAAA;;;;;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AC;AAED,+EAA+E;AAC/E,iBAAiB;AACjB,+EAA+E;AAE/E;;;;;;;;;GASG;AACH,MAAM,UAAU,kBAAkB;IACjC,MAAM,IAAI,GAAG,IAAI,UAAU,CAAC,wBAAwB,CAAC,CAAA;IACrD,MAAM,CAAC,eAAe,CAAC,IAAI,CAAC,CAAA;IAC5B,OAAO,IAAI,CAAA;AACZ,CAAC;AAED;;;;;;;;;;;GAWG;AACH,MAAM,UAAU,eAAe;IAC9B,MAAM,MAAM,GAAG,kBAAkB,EAAE,CAAA;IACnC,MAAM,CAAC,EAAE,CAAC,GAAG,eAAe,CAAC,MAAM,CAAC,CAAA;IAEpC,OAAO;QACN,SAAS,EAAE,QAAQ,CAAC,EAAE,CAAC;QACvB,UAAU,EAAE,QAAQ,CAAC,MAAM,CAAC;KAC5B,CAAA;AACF,CAAC"}
@@ -0,0 +1,108 @@
1
+ /**
2
+ * Encoding/Decoding Utilities
3
+ *
4
+ * Provides general-purpose encoding and decoding functions for converting
5
+ * between different binary formats (Uint8Array, ArrayBuffer, Base64, Base58)
6
+ */
7
+ /**
8
+ * Convert Uint8Array to Base64 string
9
+ *
10
+ * Uses Node.js Buffer when available (more efficient), falls back to btoa for browsers.
11
+ *
12
+ * @param bytes - Bytes to encode
13
+ * @returns Base64 encoded string
14
+ *
15
+ * @example
16
+ * ```ts
17
+ * const base64 = uint8ArrayToBase64(new Uint8Array([1, 2, 3]))
18
+ * ```
19
+ */
20
+ export declare function uint8ArrayToBase64(bytes: Uint8Array): string;
21
+ /**
22
+ * Convert Base64 string to Uint8Array
23
+ *
24
+ * Uses Node.js Buffer when available (more efficient), falls back to atob for browsers.
25
+ *
26
+ * @param base64 - Base64 encoded string
27
+ * @returns Decoded bytes
28
+ *
29
+ * @example
30
+ * ```ts
31
+ * const bytes = base64ToUint8Array('AQID')
32
+ * ```
33
+ */
34
+ export declare function base64ToUint8Array(base64: string): Uint8Array;
35
+ /**
36
+ * Convert ArrayBuffer to Base64 string
37
+ *
38
+ * @param buffer - Buffer to encode
39
+ * @returns Base64 encoded string
40
+ *
41
+ * @example
42
+ * ```ts
43
+ * const base64 = arrayBufferToBase64(new ArrayBuffer(8))
44
+ * ```
45
+ */
46
+ export declare function arrayBufferToBase64(buffer: ArrayBuffer): string;
47
+ /**
48
+ * Convert Base64 string to ArrayBuffer
49
+ *
50
+ * @param base64 - Base64 encoded string
51
+ * @returns Decoded buffer
52
+ *
53
+ * @example
54
+ * ```ts
55
+ * const buffer = base64ToArrayBuffer('AQID')
56
+ * ```
57
+ */
58
+ export declare function base64ToArrayBuffer(base64: string): ArrayBuffer;
59
+ /**
60
+ * Convert Uint8Array to ArrayBuffer
61
+ *
62
+ * @param bytes - The Uint8Array to convert
63
+ * @returns ArrayBuffer
64
+ *
65
+ * @example
66
+ * ```ts
67
+ * const buffer = uint8ArrayToArrayBuffer(new Uint8Array([1, 2, 3]))
68
+ * ```
69
+ */
70
+ export declare function uint8ArrayToArrayBuffer(bytes: Uint8Array): ArrayBuffer;
71
+ /**
72
+ * Convert ArrayBuffer to Uint8Array
73
+ *
74
+ * @param buffer - The ArrayBuffer to convert
75
+ * @returns Uint8Array view of the buffer
76
+ *
77
+ * @example
78
+ * ```ts
79
+ * const bytes = arrayBufferToUint8Array(new ArrayBuffer(8))
80
+ * ```
81
+ */
82
+ export declare function arrayBufferToUint8Array(buffer: ArrayBuffer): Uint8Array;
83
+ /**
84
+ * Encode a Uint8Array to Base58 string
85
+ *
86
+ * @param buf - The bytes to encode
87
+ * @returns Base58 encoded string
88
+ *
89
+ * @example
90
+ * ```ts
91
+ * const encoded = toBase58(new Uint8Array([1, 2, 3]))
92
+ * ```
93
+ */
94
+ export declare function toBase58(buf: Uint8Array): string;
95
+ /**
96
+ * Decode a Base58 string to Uint8Array
97
+ *
98
+ * @param str - The Base58 string to decode
99
+ * @returns Decoded bytes as Uint8Array
100
+ * @throws Error if the string is invalid Base58
101
+ *
102
+ * @example
103
+ * ```ts
104
+ * const decoded = fromBase58('5Kd3N...')
105
+ * ```
106
+ */
107
+ export declare function fromBase58(str: string): Uint8Array;
108
+ //# sourceMappingURL=encoding.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"encoding.d.ts","sourceRoot":"","sources":["../src/encoding.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAeH;;;;;;;;;;;;GAYG;AACH,wBAAgB,kBAAkB,CAAC,KAAK,EAAE,UAAU,GAAG,MAAM,CAW5D;AAED;;;;;;;;;;;;GAYG;AACH,wBAAgB,kBAAkB,CAAC,MAAM,EAAE,MAAM,GAAG,UAAU,CAY7D;AAED;;;;;;;;;;GAUG;AACH,wBAAgB,mBAAmB,CAAC,MAAM,EAAE,WAAW,GAAG,MAAM,CAG/D;AAED;;;;;;;;;;GAUG;AACH,wBAAgB,mBAAmB,CAAC,MAAM,EAAE,MAAM,GAAG,WAAW,CAI/D;AAMD;;;;;;;;;;GAUG;AACH,wBAAgB,uBAAuB,CAAC,KAAK,EAAE,UAAU,GAAG,WAAW,CAKtE;AAED;;;;;;;;;;GAUG;AACH,wBAAgB,uBAAuB,CAAC,MAAM,EAAE,WAAW,GAAG,UAAU,CAEvE;AAMD;;;;;;;;;;GAUG;AACH,wBAAgB,QAAQ,CAAC,GAAG,EAAE,UAAU,GAAG,MAAM,CAEhD;AAED;;;;;;;;;;;GAWG;AACH,wBAAgB,UAAU,CAAC,GAAG,EAAE,MAAM,GAAG,UAAU,CAElD"}
@@ -0,0 +1,165 @@
1
+ /**
2
+ * Encoding/Decoding Utilities
3
+ *
4
+ * Provides general-purpose encoding and decoding functions for converting
5
+ * between different binary formats (Uint8Array, ArrayBuffer, Base64, Base58)
6
+ */
7
+ import bs58 from 'bs58';
8
+ // ============================================================================
9
+ // Base64 Encoding/Decoding
10
+ // ============================================================================
11
+ /**
12
+ * Check if Buffer is available (Node.js environment)
13
+ */
14
+ function isBufferAvailable() {
15
+ return typeof Buffer !== 'undefined' && Buffer.from !== undefined;
16
+ }
17
+ /**
18
+ * Convert Uint8Array to Base64 string
19
+ *
20
+ * Uses Node.js Buffer when available (more efficient), falls back to btoa for browsers.
21
+ *
22
+ * @param bytes - Bytes to encode
23
+ * @returns Base64 encoded string
24
+ *
25
+ * @example
26
+ * ```ts
27
+ * const base64 = uint8ArrayToBase64(new Uint8Array([1, 2, 3]))
28
+ * ```
29
+ */
30
+ export function uint8ArrayToBase64(bytes) {
31
+ if (isBufferAvailable()) {
32
+ // Node.js: Use Buffer for better performance
33
+ return Buffer.from(bytes).toString('base64');
34
+ }
35
+ // Browser: Use btoa (available in Node.js 18+ and all modern browsers)
36
+ let binary = '';
37
+ for (let i = 0; i < bytes.byteLength; i++) {
38
+ binary += String.fromCharCode(bytes[i]);
39
+ }
40
+ return btoa(binary);
41
+ }
42
+ /**
43
+ * Convert Base64 string to Uint8Array
44
+ *
45
+ * Uses Node.js Buffer when available (more efficient), falls back to atob for browsers.
46
+ *
47
+ * @param base64 - Base64 encoded string
48
+ * @returns Decoded bytes
49
+ *
50
+ * @example
51
+ * ```ts
52
+ * const bytes = base64ToUint8Array('AQID')
53
+ * ```
54
+ */
55
+ export function base64ToUint8Array(base64) {
56
+ if (isBufferAvailable()) {
57
+ // Node.js: Use Buffer for better performance
58
+ return new Uint8Array(Buffer.from(base64, 'base64'));
59
+ }
60
+ // Browser: Use atob (available in Node.js 18+ and all modern browsers)
61
+ const binaryString = atob(base64);
62
+ const bytes = new Uint8Array(binaryString.length);
63
+ for (let i = 0; i < binaryString.length; i++) {
64
+ bytes[i] = binaryString.charCodeAt(i);
65
+ }
66
+ return bytes;
67
+ }
68
+ /**
69
+ * Convert ArrayBuffer to Base64 string
70
+ *
71
+ * @param buffer - Buffer to encode
72
+ * @returns Base64 encoded string
73
+ *
74
+ * @example
75
+ * ```ts
76
+ * const base64 = arrayBufferToBase64(new ArrayBuffer(8))
77
+ * ```
78
+ */
79
+ export function arrayBufferToBase64(buffer) {
80
+ const bytes = new Uint8Array(buffer);
81
+ return uint8ArrayToBase64(bytes);
82
+ }
83
+ /**
84
+ * Convert Base64 string to ArrayBuffer
85
+ *
86
+ * @param base64 - Base64 encoded string
87
+ * @returns Decoded buffer
88
+ *
89
+ * @example
90
+ * ```ts
91
+ * const buffer = base64ToArrayBuffer('AQID')
92
+ * ```
93
+ */
94
+ export function base64ToArrayBuffer(base64) {
95
+ const bytes = base64ToUint8Array(base64);
96
+ const buffer = bytes.buffer.slice(bytes.byteOffset, bytes.byteOffset + bytes.byteLength);
97
+ return buffer;
98
+ }
99
+ // ============================================================================
100
+ // ArrayBuffer/Uint8Array Conversions
101
+ // ============================================================================
102
+ /**
103
+ * Convert Uint8Array to ArrayBuffer
104
+ *
105
+ * @param bytes - The Uint8Array to convert
106
+ * @returns ArrayBuffer
107
+ *
108
+ * @example
109
+ * ```ts
110
+ * const buffer = uint8ArrayToArrayBuffer(new Uint8Array([1, 2, 3]))
111
+ * ```
112
+ */
113
+ export function uint8ArrayToArrayBuffer(bytes) {
114
+ if (bytes.byteOffset === 0 && bytes.byteLength === bytes.buffer.byteLength) {
115
+ return bytes.buffer;
116
+ }
117
+ return bytes.slice().buffer;
118
+ }
119
+ /**
120
+ * Convert ArrayBuffer to Uint8Array
121
+ *
122
+ * @param buffer - The ArrayBuffer to convert
123
+ * @returns Uint8Array view of the buffer
124
+ *
125
+ * @example
126
+ * ```ts
127
+ * const bytes = arrayBufferToUint8Array(new ArrayBuffer(8))
128
+ * ```
129
+ */
130
+ export function arrayBufferToUint8Array(buffer) {
131
+ return new Uint8Array(buffer);
132
+ }
133
+ // ============================================================================
134
+ // Base58 Encoding/Decoding
135
+ // ============================================================================
136
+ /**
137
+ * Encode a Uint8Array to Base58 string
138
+ *
139
+ * @param buf - The bytes to encode
140
+ * @returns Base58 encoded string
141
+ *
142
+ * @example
143
+ * ```ts
144
+ * const encoded = toBase58(new Uint8Array([1, 2, 3]))
145
+ * ```
146
+ */
147
+ export function toBase58(buf) {
148
+ return bs58.encode(buf);
149
+ }
150
+ /**
151
+ * Decode a Base58 string to Uint8Array
152
+ *
153
+ * @param str - The Base58 string to decode
154
+ * @returns Decoded bytes as Uint8Array
155
+ * @throws Error if the string is invalid Base58
156
+ *
157
+ * @example
158
+ * ```ts
159
+ * const decoded = fromBase58('5Kd3N...')
160
+ * ```
161
+ */
162
+ export function fromBase58(str) {
163
+ return bs58.decode(str);
164
+ }
165
+ //# sourceMappingURL=encoding.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"encoding.js","sourceRoot":"","sources":["../src/encoding.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,IAAI,MAAM,MAAM,CAAA;AAEvB,+EAA+E;AAC/E,2BAA2B;AAC3B,+EAA+E;AAE/E;;GAEG;AACH,SAAS,iBAAiB;IACzB,OAAO,OAAO,MAAM,KAAK,WAAW,IAAI,MAAM,CAAC,IAAI,KAAK,SAAS,CAAA;AAClE,CAAC;AAED;;;;;;;;;;;;GAYG;AACH,MAAM,UAAU,kBAAkB,CAAC,KAAiB;IACnD,IAAI,iBAAiB,EAAE,EAAE,CAAC;QACzB,6CAA6C;QAC7C,OAAO,MAAM,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,QAAQ,CAAC,QAAQ,CAAC,CAAA;IAC7C,CAAC;IACD,uEAAuE;IACvE,IAAI,MAAM,GAAG,EAAE,CAAA;IACf,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,KAAK,CAAC,UAAU,EAAE,CAAC,EAAE,EAAE,CAAC;QAC3C,MAAM,IAAI,MAAM,CAAC,YAAY,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,CAAA;IACxC,CAAC;IACD,OAAO,IAAI,CAAC,MAAM,CAAC,CAAA;AACpB,CAAC;AAED;;;;;;;;;;;;GAYG;AACH,MAAM,UAAU,kBAAkB,CAAC,MAAc;IAChD,IAAI,iBAAiB,EAAE,EAAE,CAAC;QACzB,6CAA6C;QAC7C,OAAO,IAAI,UAAU,CAAC,MAAM,CAAC,IAAI,CAAC,MAAM,EAAE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