@amadeus-protocol/sdk 1.0.8 → 1.1.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.
- package/dist/api/chain.d.ts +144 -117
- package/dist/api/chain.d.ts.map +1 -1
- package/dist/api/chain.js +175 -146
- package/dist/api/chain.js.map +1 -1
- package/dist/api/contract.d.ts +78 -60
- package/dist/api/contract.d.ts.map +1 -1
- package/dist/api/contract.js +102 -75
- package/dist/api/contract.js.map +1 -1
- package/dist/api/epoch.d.ts +65 -66
- package/dist/api/epoch.d.ts.map +1 -1
- package/dist/api/epoch.js +90 -98
- package/dist/api/epoch.js.map +1 -1
- package/dist/api/peer.d.ts +77 -78
- package/dist/api/peer.d.ts.map +1 -1
- package/dist/api/peer.js +93 -94
- package/dist/api/peer.js.map +1 -1
- package/dist/api/proof.d.ts +44 -23
- package/dist/api/proof.d.ts.map +1 -1
- package/dist/api/proof.js +56 -29
- package/dist/api/proof.js.map +1 -1
- package/dist/api/transaction.d.ts +68 -69
- package/dist/api/transaction.d.ts.map +1 -1
- package/dist/api/transaction.js +84 -84
- package/dist/api/transaction.js.map +1 -1
- package/dist/api/wallet.d.ts +36 -37
- package/dist/api/wallet.d.ts.map +1 -1
- package/dist/api/wallet.js +47 -50
- package/dist/api/wallet.js.map +1 -1
- package/dist/client.d.ts +66 -68
- package/dist/client.d.ts.map +1 -1
- package/dist/client.js +231 -276
- package/dist/client.js.map +1 -1
- package/dist/constants.d.ts +13 -10
- package/dist/constants.d.ts.map +1 -1
- package/dist/constants.js +49 -45
- package/dist/constants.js.map +1 -1
- package/dist/contract-state.d.ts +6 -3
- package/dist/contract-state.d.ts.map +1 -1
- package/dist/contract-state.js +88 -106
- package/dist/contract-state.js.map +1 -1
- package/dist/contracts/abi-types.d.ts +32 -32
- package/dist/contracts/abi-types.d.ts.map +1 -1
- package/dist/contracts/coin.d.ts +13 -20
- package/dist/contracts/coin.d.ts.map +1 -1
- package/dist/contracts/coin.js +42 -38
- package/dist/contracts/coin.js.map +1 -1
- package/dist/contracts/contract-call.d.ts +14 -16
- package/dist/contracts/contract-call.d.ts.map +1 -1
- package/dist/contracts/contract-call.js +45 -54
- package/dist/contracts/contract-call.js.map +1 -1
- package/dist/contracts/contract.d.ts +24 -55
- package/dist/contracts/contract.d.ts.map +1 -1
- package/dist/contracts/contract.js +63 -101
- package/dist/contracts/contract.js.map +1 -1
- package/dist/contracts/lockup/abi.d.ts +54 -51
- package/dist/contracts/lockup/abi.d.ts.map +1 -1
- package/dist/contracts/lockup/abi.js +65 -74
- package/dist/contracts/lockup/abi.js.map +1 -1
- package/dist/contracts/lockup/helpers.d.ts +22 -18
- package/dist/contracts/lockup/helpers.d.ts.map +1 -1
- package/dist/contracts/lockup/helpers.js +44 -47
- package/dist/contracts/lockup/helpers.js.map +1 -1
- package/dist/contracts/lockup/parsers.d.ts +7 -3
- package/dist/contracts/lockup/parsers.d.ts.map +1 -1
- package/dist/contracts/lockup/parsers.js +51 -55
- package/dist/contracts/lockup/parsers.js.map +1 -1
- package/dist/contracts/lockup/storage-keys.d.ts +5 -2
- package/dist/contracts/lockup/storage-keys.d.ts.map +1 -1
- package/dist/contracts/lockup/storage-keys.js +54 -70
- package/dist/contracts/lockup/storage-keys.js.map +1 -1
- package/dist/contracts/lockup/types.d.ts +73 -70
- package/dist/contracts/lockup/types.d.ts.map +1 -1
- package/dist/contracts/lockup-prime/abi.d.ts +328 -325
- package/dist/contracts/lockup-prime/abi.d.ts.map +1 -1
- package/dist/contracts/lockup-prime/abi.js +397 -417
- package/dist/contracts/lockup-prime/abi.js.map +1 -1
- package/dist/contracts/lockup-prime/helpers.d.ts +35 -31
- package/dist/contracts/lockup-prime/helpers.d.ts.map +1 -1
- package/dist/contracts/lockup-prime/helpers.js +131 -137
- package/dist/contracts/lockup-prime/helpers.js.map +1 -1
- package/dist/contracts/lockup-prime/parsers.d.ts +9 -5
- package/dist/contracts/lockup-prime/parsers.d.ts.map +1 -1
- package/dist/contracts/lockup-prime/parsers.js +61 -61
- package/dist/contracts/lockup-prime/parsers.js.map +1 -1
- package/dist/contracts/lockup-prime/storage-keys.d.ts +7 -4
- package/dist/contracts/lockup-prime/storage-keys.d.ts.map +1 -1
- package/dist/contracts/lockup-prime/storage-keys.js +64 -80
- package/dist/contracts/lockup-prime/storage-keys.js.map +1 -1
- package/dist/contracts/lockup-prime/types.d.ts +92 -89
- package/dist/contracts/lockup-prime/types.d.ts.map +1 -1
- package/dist/contracts/lockup-prime/types.js +12 -13
- package/dist/contracts/lockup-prime/types.js.map +1 -1
- package/dist/contracts/nft/abi.d.ts +104 -0
- package/dist/contracts/nft/abi.d.ts.map +1 -0
- package/dist/contracts/nft/abi.js +135 -0
- package/dist/contracts/nft/abi.js.map +1 -0
- package/dist/contracts/nft/helpers.d.ts +21 -0
- package/dist/contracts/nft/helpers.d.ts.map +1 -0
- package/dist/contracts/nft/helpers.js +61 -0
- package/dist/contracts/nft/helpers.js.map +1 -0
- package/dist/contracts/nft/types.d.ts +47 -0
- package/dist/contracts/nft/types.d.ts.map +1 -0
- package/dist/conversion.d.ts +5 -2
- package/dist/conversion.d.ts.map +1 -1
- package/dist/conversion.js +45 -50
- package/dist/conversion.js.map +1 -1
- package/dist/crypto.d.ts +14 -16
- package/dist/crypto.d.ts.map +1 -1
- package/dist/crypto.js +121 -140
- package/dist/crypto.js.map +1 -1
- package/dist/encoding.d.ts +11 -8
- package/dist/encoding.d.ts.map +1 -1
- package/dist/encoding.js +123 -144
- package/dist/encoding.js.map +1 -1
- package/dist/encryption.d.ts +15 -12
- package/dist/encryption.d.ts.map +1 -1
- package/dist/encryption.js +119 -117
- package/dist/encryption.js.map +1 -1
- package/dist/explorer.d.ts +10 -7
- package/dist/explorer.d.ts.map +1 -1
- package/dist/explorer.js +51 -48
- package/dist/explorer.js.map +1 -1
- package/dist/formatters.d.ts +8 -5
- package/dist/formatters.d.ts.map +1 -1
- package/dist/formatters.js +67 -68
- package/dist/formatters.js.map +1 -1
- package/dist/index.d.ts +42 -55
- package/dist/index.js +39 -55
- package/dist/mnemonic.d.ts +12 -9
- package/dist/mnemonic.d.ts.map +1 -1
- package/dist/mnemonic.js +105 -114
- package/dist/mnemonic.js.map +1 -1
- package/dist/networks.d.ts +14 -17
- package/dist/networks.d.ts.map +1 -1
- package/dist/networks.js +40 -36
- package/dist/networks.js.map +1 -1
- package/dist/schemas.js +75 -79
- package/dist/schemas.js.map +1 -1
- package/dist/sdk.d.ts +69 -65
- package/dist/sdk.d.ts.map +1 -1
- package/dist/sdk.js +126 -117
- package/dist/sdk.js.map +1 -1
- package/dist/serialization.d.ts +8 -18
- package/dist/serialization.d.ts.map +1 -1
- package/dist/serialization.js +207 -260
- package/dist/serialization.js.map +1 -1
- package/dist/signing.js +67 -67
- package/dist/signing.js.map +1 -1
- package/dist/transaction-builder.d.ts +175 -184
- package/dist/transaction-builder.d.ts.map +1 -1
- package/dist/transaction-builder.js +295 -344
- package/dist/transaction-builder.js.map +1 -1
- package/dist/transaction-errors.d.ts +10 -12
- package/dist/transaction-errors.d.ts.map +1 -1
- package/dist/transaction-errors.js +145 -166
- package/dist/transaction-errors.js.map +1 -1
- package/dist/types.d.ts +530 -450
- package/dist/types.d.ts.map +1 -1
- package/dist/types.js +169 -183
- package/dist/types.js.map +1 -1
- package/dist/validation.d.ts +24 -26
- package/dist/validation.d.ts.map +1 -1
- package/dist/validation.js +194 -192
- package/dist/validation.js.map +1 -1
- package/package.json +4 -3
- package/dist/api/index.d.ts +0 -8
- package/dist/api/index.d.ts.map +0 -1
- package/dist/api/index.js +0 -8
- package/dist/api/index.js.map +0 -1
- package/dist/contracts/abi-types.js +0 -8
- package/dist/contracts/abi-types.js.map +0 -1
- package/dist/contracts/index.d.ts +0 -7
- package/dist/contracts/index.d.ts.map +0 -1
- package/dist/contracts/index.js +0 -7
- package/dist/contracts/index.js.map +0 -1
- package/dist/contracts/lockup/calls.d.ts +0 -23
- package/dist/contracts/lockup/calls.d.ts.map +0 -1
- package/dist/contracts/lockup/calls.js +0 -25
- package/dist/contracts/lockup/calls.js.map +0 -1
- package/dist/contracts/lockup/index.d.ts +0 -6
- package/dist/contracts/lockup/index.d.ts.map +0 -1
- package/dist/contracts/lockup/index.js +0 -6
- package/dist/contracts/lockup/index.js.map +0 -1
- package/dist/contracts/lockup/types.js +0 -7
- package/dist/contracts/lockup/types.js.map +0 -1
- package/dist/contracts/lockup-prime/calls.d.ts +0 -53
- package/dist/contracts/lockup-prime/calls.d.ts.map +0 -1
- package/dist/contracts/lockup-prime/calls.js +0 -60
- package/dist/contracts/lockup-prime/calls.js.map +0 -1
- package/dist/contracts/lockup-prime/index.d.ts +0 -6
- package/dist/contracts/lockup-prime/index.d.ts.map +0 -1
- package/dist/contracts/lockup-prime/index.js +0 -6
- package/dist/contracts/lockup-prime/index.js.map +0 -1
- package/dist/index.d.ts.map +0 -1
- package/dist/index.js.map +0 -1
- package/dist/schemas.d.ts +0 -67
- package/dist/schemas.d.ts.map +0 -1
- package/dist/signing.d.ts +0 -39
- package/dist/signing.d.ts.map +0 -1
package/dist/encoding.js
CHANGED
|
@@ -1,165 +1,144 @@
|
|
|
1
|
+
import bs58 from "bs58";
|
|
2
|
+
//#region src/encoding.ts
|
|
1
3
|
/**
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
import bs58 from 'bs58';
|
|
8
|
-
// ============================================================================
|
|
9
|
-
// Base64 Encoding/Decoding
|
|
10
|
-
// ============================================================================
|
|
4
|
+
* Encoding/Decoding Utilities
|
|
5
|
+
*
|
|
6
|
+
* Provides general-purpose encoding and decoding functions for converting
|
|
7
|
+
* between different binary formats (Uint8Array, ArrayBuffer, Base64, Base58)
|
|
8
|
+
*/
|
|
11
9
|
/**
|
|
12
|
-
|
|
13
|
-
|
|
10
|
+
* Check if Buffer is available (Node.js environment)
|
|
11
|
+
*/
|
|
14
12
|
function isBufferAvailable() {
|
|
15
|
-
|
|
13
|
+
return typeof Buffer !== "undefined" && Buffer.from !== void 0;
|
|
16
14
|
}
|
|
17
15
|
/**
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
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);
|
|
16
|
+
* Convert Uint8Array to Base64 string
|
|
17
|
+
*
|
|
18
|
+
* Uses Node.js Buffer when available (more efficient), falls back to btoa for browsers.
|
|
19
|
+
*
|
|
20
|
+
* @param bytes - Bytes to encode
|
|
21
|
+
* @returns Base64 encoded string
|
|
22
|
+
*
|
|
23
|
+
* @example
|
|
24
|
+
* ```ts
|
|
25
|
+
* const base64 = uint8ArrayToBase64(new Uint8Array([1, 2, 3]))
|
|
26
|
+
* ```
|
|
27
|
+
*/
|
|
28
|
+
function uint8ArrayToBase64(bytes) {
|
|
29
|
+
if (isBufferAvailable()) return Buffer.from(bytes).toString("base64");
|
|
30
|
+
let binary = "";
|
|
31
|
+
for (let i = 0; i < bytes.byteLength; i++) binary += String.fromCharCode(bytes[i]);
|
|
32
|
+
return btoa(binary);
|
|
41
33
|
}
|
|
42
34
|
/**
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
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;
|
|
35
|
+
* Convert Base64 string to Uint8Array
|
|
36
|
+
*
|
|
37
|
+
* Uses Node.js Buffer when available (more efficient), falls back to atob for browsers.
|
|
38
|
+
*
|
|
39
|
+
* @param base64 - Base64 encoded string
|
|
40
|
+
* @returns Decoded bytes
|
|
41
|
+
*
|
|
42
|
+
* @example
|
|
43
|
+
* ```ts
|
|
44
|
+
* const bytes = base64ToUint8Array('AQID')
|
|
45
|
+
* ```
|
|
46
|
+
*/
|
|
47
|
+
function base64ToUint8Array(base64) {
|
|
48
|
+
if (isBufferAvailable()) return new Uint8Array(Buffer.from(base64, "base64"));
|
|
49
|
+
const binaryString = atob(base64);
|
|
50
|
+
const bytes = new Uint8Array(binaryString.length);
|
|
51
|
+
for (let i = 0; i < binaryString.length; i++) bytes[i] = binaryString.charCodeAt(i);
|
|
52
|
+
return bytes;
|
|
67
53
|
}
|
|
68
54
|
/**
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
return uint8ArrayToBase64(bytes);
|
|
55
|
+
* Convert ArrayBuffer to Base64 string
|
|
56
|
+
*
|
|
57
|
+
* @param buffer - Buffer to encode
|
|
58
|
+
* @returns Base64 encoded string
|
|
59
|
+
*
|
|
60
|
+
* @example
|
|
61
|
+
* ```ts
|
|
62
|
+
* const base64 = arrayBufferToBase64(new ArrayBuffer(8))
|
|
63
|
+
* ```
|
|
64
|
+
*/
|
|
65
|
+
function arrayBufferToBase64(buffer) {
|
|
66
|
+
return uint8ArrayToBase64(new Uint8Array(buffer));
|
|
82
67
|
}
|
|
83
68
|
/**
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
return buffer;
|
|
69
|
+
* Convert Base64 string to ArrayBuffer
|
|
70
|
+
*
|
|
71
|
+
* @param base64 - Base64 encoded string
|
|
72
|
+
* @returns Decoded buffer
|
|
73
|
+
*
|
|
74
|
+
* @example
|
|
75
|
+
* ```ts
|
|
76
|
+
* const buffer = base64ToArrayBuffer('AQID')
|
|
77
|
+
* ```
|
|
78
|
+
*/
|
|
79
|
+
function base64ToArrayBuffer(base64) {
|
|
80
|
+
const bytes = base64ToUint8Array(base64);
|
|
81
|
+
return bytes.buffer.slice(bytes.byteOffset, bytes.byteOffset + bytes.byteLength);
|
|
98
82
|
}
|
|
99
|
-
// ============================================================================
|
|
100
|
-
// ArrayBuffer/Uint8Array Conversions
|
|
101
|
-
// ============================================================================
|
|
102
83
|
/**
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
}
|
|
117
|
-
return bytes.slice().buffer;
|
|
84
|
+
* Convert Uint8Array to ArrayBuffer
|
|
85
|
+
*
|
|
86
|
+
* @param bytes - The Uint8Array to convert
|
|
87
|
+
* @returns ArrayBuffer
|
|
88
|
+
*
|
|
89
|
+
* @example
|
|
90
|
+
* ```ts
|
|
91
|
+
* const buffer = uint8ArrayToArrayBuffer(new Uint8Array([1, 2, 3]))
|
|
92
|
+
* ```
|
|
93
|
+
*/
|
|
94
|
+
function uint8ArrayToArrayBuffer(bytes) {
|
|
95
|
+
if (bytes.byteOffset === 0 && bytes.byteLength === bytes.buffer.byteLength) return bytes.buffer;
|
|
96
|
+
return bytes.slice().buffer;
|
|
118
97
|
}
|
|
119
98
|
/**
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
99
|
+
* Convert ArrayBuffer to Uint8Array
|
|
100
|
+
*
|
|
101
|
+
* @param buffer - The ArrayBuffer to convert
|
|
102
|
+
* @returns Uint8Array view of the buffer
|
|
103
|
+
*
|
|
104
|
+
* @example
|
|
105
|
+
* ```ts
|
|
106
|
+
* const bytes = arrayBufferToUint8Array(new ArrayBuffer(8))
|
|
107
|
+
* ```
|
|
108
|
+
*/
|
|
109
|
+
function arrayBufferToUint8Array(buffer) {
|
|
110
|
+
return new Uint8Array(buffer);
|
|
132
111
|
}
|
|
133
|
-
// ============================================================================
|
|
134
|
-
// Base58 Encoding/Decoding
|
|
135
|
-
// ============================================================================
|
|
136
112
|
/**
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
113
|
+
* Encode a Uint8Array to Base58 string
|
|
114
|
+
*
|
|
115
|
+
* @param buf - The bytes to encode
|
|
116
|
+
* @returns Base58 encoded string
|
|
117
|
+
*
|
|
118
|
+
* @example
|
|
119
|
+
* ```ts
|
|
120
|
+
* const encoded = toBase58(new Uint8Array([1, 2, 3]))
|
|
121
|
+
* ```
|
|
122
|
+
*/
|
|
123
|
+
function toBase58(buf) {
|
|
124
|
+
return bs58.encode(buf);
|
|
149
125
|
}
|
|
150
126
|
/**
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
127
|
+
* Decode a Base58 string to Uint8Array
|
|
128
|
+
*
|
|
129
|
+
* @param str - The Base58 string to decode
|
|
130
|
+
* @returns Decoded bytes as Uint8Array
|
|
131
|
+
* @throws Error if the string is invalid Base58
|
|
132
|
+
*
|
|
133
|
+
* @example
|
|
134
|
+
* ```ts
|
|
135
|
+
* const decoded = fromBase58('5Kd3N...')
|
|
136
|
+
* ```
|
|
137
|
+
*/
|
|
138
|
+
function fromBase58(str) {
|
|
139
|
+
return bs58.decode(str);
|
|
164
140
|
}
|
|
141
|
+
//#endregion
|
|
142
|
+
export { arrayBufferToBase64, arrayBufferToUint8Array, base64ToArrayBuffer, base64ToUint8Array, fromBase58, toBase58, uint8ArrayToArrayBuffer, uint8ArrayToBase64 };
|
|
143
|
+
|
|
165
144
|
//# sourceMappingURL=encoding.js.map
|
package/dist/encoding.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"encoding.js","
|
|
1
|
+
{"version":3,"file":"encoding.js","names":[],"sources":["../src/encoding.ts"],"sourcesContent":["/**\n * Encoding/Decoding Utilities\n *\n * Provides general-purpose encoding and decoding functions for converting\n * between different binary formats (Uint8Array, ArrayBuffer, Base64, Base58)\n */\n\nimport bs58 from 'bs58'\n\n// ============================================================================\n// Base64 Encoding/Decoding\n// ============================================================================\n\n/**\n * Check if Buffer is available (Node.js environment)\n */\nfunction isBufferAvailable(): boolean {\n\treturn typeof Buffer !== 'undefined' && Buffer.from !== undefined\n}\n\n/**\n * Convert Uint8Array to Base64 string\n *\n * Uses Node.js Buffer when available (more efficient), falls back to btoa for browsers.\n *\n * @param bytes - Bytes to encode\n * @returns Base64 encoded string\n *\n * @example\n * ```ts\n * const base64 = uint8ArrayToBase64(new Uint8Array([1, 2, 3]))\n * ```\n */\nexport function uint8ArrayToBase64(bytes: Uint8Array): string {\n\tif (isBufferAvailable()) {\n\t\t// Node.js: Use Buffer for better performance\n\t\treturn Buffer.from(bytes).toString('base64')\n\t}\n\t// Browser: Use btoa (available in Node.js 18+ and all modern browsers)\n\tlet binary = ''\n\tfor (let i = 0; i < bytes.byteLength; i++) {\n\t\tbinary += String.fromCharCode(bytes[i])\n\t}\n\treturn btoa(binary)\n}\n\n/**\n * Convert Base64 string to Uint8Array\n *\n * Uses Node.js Buffer when available (more efficient), falls back to atob for browsers.\n *\n * @param base64 - Base64 encoded string\n * @returns Decoded bytes\n *\n * @example\n * ```ts\n * const bytes = base64ToUint8Array('AQID')\n * ```\n */\nexport function base64ToUint8Array(base64: string): Uint8Array {\n\tif (isBufferAvailable()) {\n\t\t// Node.js: Use Buffer for better performance\n\t\treturn new Uint8Array(Buffer.from(base64, 'base64'))\n\t}\n\t// Browser: Use atob (available in Node.js 18+ and all modern browsers)\n\tconst binaryString = atob(base64)\n\tconst bytes = new Uint8Array(binaryString.length)\n\tfor (let i = 0; i < binaryString.length; i++) {\n\t\tbytes[i] = binaryString.charCodeAt(i)\n\t}\n\treturn bytes\n}\n\n/**\n * Convert ArrayBuffer to Base64 string\n *\n * @param buffer - Buffer to encode\n * @returns Base64 encoded string\n *\n * @example\n * ```ts\n * const base64 = arrayBufferToBase64(new ArrayBuffer(8))\n * ```\n */\nexport function arrayBufferToBase64(buffer: ArrayBuffer): string {\n\tconst bytes = new Uint8Array(buffer)\n\treturn uint8ArrayToBase64(bytes)\n}\n\n/**\n * Convert Base64 string to ArrayBuffer\n *\n * @param base64 - Base64 encoded string\n * @returns Decoded buffer\n *\n * @example\n * ```ts\n * const buffer = base64ToArrayBuffer('AQID')\n * ```\n */\nexport function base64ToArrayBuffer(base64: string): ArrayBuffer {\n\tconst bytes = base64ToUint8Array(base64)\n\tconst buffer = bytes.buffer.slice(bytes.byteOffset, bytes.byteOffset + bytes.byteLength)\n\treturn buffer as ArrayBuffer\n}\n\n// ============================================================================\n// ArrayBuffer/Uint8Array Conversions\n// ============================================================================\n\n/**\n * Convert Uint8Array to ArrayBuffer\n *\n * @param bytes - The Uint8Array to convert\n * @returns ArrayBuffer\n *\n * @example\n * ```ts\n * const buffer = uint8ArrayToArrayBuffer(new Uint8Array([1, 2, 3]))\n * ```\n */\nexport function uint8ArrayToArrayBuffer(bytes: Uint8Array): ArrayBuffer {\n\tif (bytes.byteOffset === 0 && bytes.byteLength === bytes.buffer.byteLength) {\n\t\treturn bytes.buffer as ArrayBuffer\n\t}\n\treturn bytes.slice().buffer\n}\n\n/**\n * Convert ArrayBuffer to Uint8Array\n *\n * @param buffer - The ArrayBuffer to convert\n * @returns Uint8Array view of the buffer\n *\n * @example\n * ```ts\n * const bytes = arrayBufferToUint8Array(new ArrayBuffer(8))\n * ```\n */\nexport function arrayBufferToUint8Array(buffer: ArrayBuffer): Uint8Array {\n\treturn new Uint8Array(buffer)\n}\n\n// ============================================================================\n// Base58 Encoding/Decoding\n// ============================================================================\n\n/**\n * Encode a Uint8Array to Base58 string\n *\n * @param buf - The bytes to encode\n * @returns Base58 encoded string\n *\n * @example\n * ```ts\n * const encoded = toBase58(new Uint8Array([1, 2, 3]))\n * ```\n */\nexport function toBase58(buf: Uint8Array): string {\n\treturn bs58.encode(buf)\n}\n\n/**\n * Decode a Base58 string to Uint8Array\n *\n * @param str - The Base58 string to decode\n * @returns Decoded bytes as Uint8Array\n * @throws Error if the string is invalid Base58\n *\n * @example\n * ```ts\n * const decoded = fromBase58('5Kd3N...')\n * ```\n */\nexport function fromBase58(str: string): Uint8Array {\n\treturn bs58.decode(str)\n}\n"],"mappings":";;;;;;;;;;;AAgBA,SAAS,oBAA6B;CACrC,OAAO,OAAO,WAAW,eAAe,OAAO,SAAS,KAAA;;;;;;;;;;;;;;;AAgBzD,SAAgB,mBAAmB,OAA2B;CAC7D,IAAI,mBAAmB,EAEtB,OAAO,OAAO,KAAK,MAAM,CAAC,SAAS,SAAS;CAG7C,IAAI,SAAS;CACb,KAAK,IAAI,IAAI,GAAG,IAAI,MAAM,YAAY,KACrC,UAAU,OAAO,aAAa,MAAM,GAAG;CAExC,OAAO,KAAK,OAAO;;;;;;;;;;;;;;;AAgBpB,SAAgB,mBAAmB,QAA4B;CAC9D,IAAI,mBAAmB,EAEtB,OAAO,IAAI,WAAW,OAAO,KAAK,QAAQ,SAAS,CAAC;CAGrD,MAAM,eAAe,KAAK,OAAO;CACjC,MAAM,QAAQ,IAAI,WAAW,aAAa,OAAO;CACjD,KAAK,IAAI,IAAI,GAAG,IAAI,aAAa,QAAQ,KACxC,MAAM,KAAK,aAAa,WAAW,EAAE;CAEtC,OAAO;;;;;;;;;;;;;AAcR,SAAgB,oBAAoB,QAA6B;CAEhE,OAAO,mBAAmB,IADR,WAAW,OACE,CAAC;;;;;;;;;;;;;AAcjC,SAAgB,oBAAoB,QAA6B;CAChE,MAAM,QAAQ,mBAAmB,OAAO;CAExC,OADe,MAAM,OAAO,MAAM,MAAM,YAAY,MAAM,aAAa,MAAM,WAChE;;;;;;;;;;;;;AAkBd,SAAgB,wBAAwB,OAAgC;CACvE,IAAI,MAAM,eAAe,KAAK,MAAM,eAAe,MAAM,OAAO,YAC/D,OAAO,MAAM;CAEd,OAAO,MAAM,OAAO,CAAC;;;;;;;;;;;;;AActB,SAAgB,wBAAwB,QAAiC;CACxE,OAAO,IAAI,WAAW,OAAO;;;;;;;;;;;;;AAkB9B,SAAgB,SAAS,KAAyB;CACjD,OAAO,KAAK,OAAO,IAAI;;;;;;;;;;;;;;AAexB,SAAgB,WAAW,KAAyB;CACnD,OAAO,KAAK,OAAO,IAAI"}
|
package/dist/encryption.d.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
//#region src/encryption.d.ts
|
|
1
2
|
/**
|
|
2
3
|
* Password-Based Encryption Utilities
|
|
3
4
|
*
|
|
@@ -12,32 +13,32 @@
|
|
|
12
13
|
* @param iterations - Number of PBKDF2 iterations (default: 100,000)
|
|
13
14
|
* @returns Derived AES-GCM key
|
|
14
15
|
*/
|
|
15
|
-
|
|
16
|
+
declare function deriveKey(password: string, salt: Uint8Array | ArrayBuffer, iterations?: number): Promise<CryptoKey>;
|
|
16
17
|
/**
|
|
17
18
|
* Generate a cryptographically secure random salt
|
|
18
19
|
*
|
|
19
20
|
* @returns Random salt (16 bytes)
|
|
20
21
|
*/
|
|
21
|
-
|
|
22
|
+
declare function generateSalt(): Uint8Array;
|
|
22
23
|
/**
|
|
23
24
|
* Generate a cryptographically secure random initialization vector
|
|
24
25
|
*
|
|
25
26
|
* @returns Random IV (12 bytes)
|
|
26
27
|
*/
|
|
27
|
-
|
|
28
|
+
declare function generateIV(): Uint8Array;
|
|
28
29
|
/**
|
|
29
30
|
* Encrypted data payload (Base64 encoded)
|
|
30
31
|
*
|
|
31
32
|
* Uses Base64 encoding for binary payloads like vaults (more efficient than Base58).
|
|
32
33
|
* Base58 is reserved for addresses, keys, and hashes.
|
|
33
34
|
*/
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
35
|
+
interface EncryptedPayload {
|
|
36
|
+
/** Encrypted data (Base64 encoded) */
|
|
37
|
+
encryptedData: string;
|
|
38
|
+
/** Initialization vector (Base64 encoded) */
|
|
39
|
+
iv: string;
|
|
40
|
+
/** Salt used for key derivation (Base64 encoded) */
|
|
41
|
+
salt: string;
|
|
41
42
|
}
|
|
42
43
|
/**
|
|
43
44
|
* Encrypt plaintext with a password using AES-GCM
|
|
@@ -58,7 +59,7 @@ export interface EncryptedPayload {
|
|
|
58
59
|
* // Store encrypted.encryptedData, encrypted.iv, encrypted.salt
|
|
59
60
|
* ```
|
|
60
61
|
*/
|
|
61
|
-
|
|
62
|
+
declare function encryptWithPassword(plaintext: string, password: string): Promise<EncryptedPayload>;
|
|
62
63
|
/**
|
|
63
64
|
* Decrypt encrypted data with a password
|
|
64
65
|
*
|
|
@@ -72,5 +73,7 @@ export declare function encryptWithPassword(plaintext: string, password: string)
|
|
|
72
73
|
* const decrypted = await decryptWithPassword(encrypted, 'my-password')
|
|
73
74
|
* ```
|
|
74
75
|
*/
|
|
75
|
-
|
|
76
|
+
declare function decryptWithPassword(payload: EncryptedPayload, password: string): Promise<string>;
|
|
77
|
+
//#endregion
|
|
78
|
+
export { EncryptedPayload, decryptWithPassword, deriveKey, encryptWithPassword, generateIV, generateSalt };
|
|
76
79
|
//# sourceMappingURL=encryption.d.ts.map
|
package/dist/encryption.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"encryption.d.ts","
|
|
1
|
+
{"version":3,"file":"encryption.d.ts","names":[],"sources":["../src/encryption.ts"],"mappings":";;AAkDA;;;;;;;;;;;;;iBAAsB,SAAA,CACrB,QAAA,UACA,IAAA,EAAM,UAAA,GAAa,WAAA,EACnB,UAAA,YACE,OAAA,CAAQ,SAAA;;;;;AA2BX;iBAAgB,YAAA,CAAA,GAAgB,UAAA;;;;AAWhC;;iBAAgB,UAAA,CAAA,GAAc,UAAA;;;AAgB9B;;;;UAAiB,gBAAA;EAIhB;EAFA,aAAA;EAII;EAFJ,EAAA;EAwBqB;EAtBrB,IAAA;AAAA;;;;;;;;AA0DD;;;;;;;;;;;;iBApCsB,mBAAA,CACrB,SAAA,UACA,QAAA,WACE,OAAA,CAAQ,gBAAA;;;;;;;;;;;;;;iBAiCW,mBAAA,CACrB,OAAA,EAAS,gBAAA,EACT,QAAA,WACE,OAAA"}
|
package/dist/encryption.js
CHANGED
|
@@ -1,137 +1,139 @@
|
|
|
1
|
+
import { base64ToUint8Array, uint8ArrayToArrayBuffer, uint8ArrayToBase64 } from "./encoding.js";
|
|
2
|
+
//#region src/encryption.ts
|
|
1
3
|
/**
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
import { uint8ArrayToBase64, base64ToUint8Array } from './encoding';
|
|
8
|
-
import { uint8ArrayToArrayBuffer } from './encoding';
|
|
9
|
-
// ============================================================================
|
|
10
|
-
// Constants
|
|
11
|
-
// ============================================================================
|
|
4
|
+
* Password-Based Encryption Utilities
|
|
5
|
+
*
|
|
6
|
+
* Provides secure password-based encryption using PBKDF2 key derivation
|
|
7
|
+
* and AES-GCM encryption. Suitable for encrypting sensitive wallet data.
|
|
8
|
+
*/
|
|
12
9
|
/**
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
const PBKDF2_ITERATIONS =
|
|
10
|
+
* PBKDF2 iterations for key derivation (100,000 iterations for security)
|
|
11
|
+
* Matches web wallet implementation for consistency
|
|
12
|
+
*/
|
|
13
|
+
const PBKDF2_ITERATIONS = 1e5;
|
|
17
14
|
/**
|
|
18
|
-
|
|
19
|
-
|
|
15
|
+
* Salt length in bytes (128 bits)
|
|
16
|
+
*/
|
|
20
17
|
const SALT_LENGTH = 16;
|
|
21
18
|
/**
|
|
22
|
-
|
|
23
|
-
|
|
19
|
+
* IV length in bytes for AES-GCM (96 bits)
|
|
20
|
+
*/
|
|
24
21
|
const IV_LENGTH = 12;
|
|
25
|
-
// ============================================================================
|
|
26
|
-
// Helper Functions
|
|
27
|
-
// ============================================================================
|
|
28
22
|
/**
|
|
29
|
-
|
|
30
|
-
|
|
23
|
+
* Import password as a PBKDF2 key
|
|
24
|
+
*/
|
|
31
25
|
async function importPbkdf2Key(password) {
|
|
32
|
-
|
|
33
|
-
|
|
26
|
+
const enc = new TextEncoder();
|
|
27
|
+
return crypto.subtle.importKey("raw", enc.encode(password), "PBKDF2", false, ["deriveKey"]);
|
|
34
28
|
}
|
|
35
29
|
/**
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
30
|
+
* Derive a cryptographic key from a password using PBKDF2
|
|
31
|
+
*
|
|
32
|
+
* @param password - Password string
|
|
33
|
+
* @param salt - Salt as Uint8Array or ArrayBuffer
|
|
34
|
+
* @param iterations - Number of PBKDF2 iterations (default: 100,000)
|
|
35
|
+
* @returns Derived AES-GCM key
|
|
36
|
+
*/
|
|
37
|
+
async function deriveKey(password, salt, iterations = PBKDF2_ITERATIONS) {
|
|
38
|
+
const baseKey = await importPbkdf2Key(password);
|
|
39
|
+
const saltBuffer = salt instanceof Uint8Array ? uint8ArrayToArrayBuffer(salt) : salt;
|
|
40
|
+
return crypto.subtle.deriveKey({
|
|
41
|
+
name: "PBKDF2",
|
|
42
|
+
salt: saltBuffer,
|
|
43
|
+
iterations,
|
|
44
|
+
hash: "SHA-256"
|
|
45
|
+
}, baseKey, {
|
|
46
|
+
name: "AES-GCM",
|
|
47
|
+
length: 256
|
|
48
|
+
}, false, ["encrypt", "decrypt"]);
|
|
52
49
|
}
|
|
53
|
-
// ============================================================================
|
|
54
|
-
// Random Generation
|
|
55
|
-
// ============================================================================
|
|
56
50
|
/**
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
51
|
+
* Generate a cryptographically secure random salt
|
|
52
|
+
*
|
|
53
|
+
* @returns Random salt (16 bytes)
|
|
54
|
+
*/
|
|
55
|
+
function generateSalt() {
|
|
56
|
+
const salt = new Uint8Array(SALT_LENGTH);
|
|
57
|
+
crypto.getRandomValues(salt);
|
|
58
|
+
return salt;
|
|
65
59
|
}
|
|
66
60
|
/**
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
61
|
+
* Generate a cryptographically secure random initialization vector
|
|
62
|
+
*
|
|
63
|
+
* @returns Random IV (12 bytes)
|
|
64
|
+
*/
|
|
65
|
+
function generateIV() {
|
|
66
|
+
const iv = new Uint8Array(IV_LENGTH);
|
|
67
|
+
crypto.getRandomValues(iv);
|
|
68
|
+
return iv;
|
|
75
69
|
}
|
|
76
70
|
/**
|
|
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
|
-
|
|
71
|
+
* Encrypt plaintext with a password using AES-GCM
|
|
72
|
+
*
|
|
73
|
+
* Uses PBKDF2 for key derivation and Base64 encoding for output.
|
|
74
|
+
* Suitable for encrypting sensitive wallet data like private keys.
|
|
75
|
+
*
|
|
76
|
+
* **Encoding Standard**: Uses Base64 encoding for binary payloads (RFC 4648).
|
|
77
|
+
* Base58 is used for addresses, keys, and hashes elsewhere in the SDK.
|
|
78
|
+
*
|
|
79
|
+
* @param plaintext - Plaintext string to encrypt
|
|
80
|
+
* @param password - Password for encryption
|
|
81
|
+
* @returns Encrypted payload with encryptedData, IV, and salt (all Base64 encoded)
|
|
82
|
+
*
|
|
83
|
+
* @example
|
|
84
|
+
* ```ts
|
|
85
|
+
* const encrypted = await encryptWithPassword('sensitive data', 'my-password')
|
|
86
|
+
* // Store encrypted.encryptedData, encrypted.iv, encrypted.salt
|
|
87
|
+
* ```
|
|
88
|
+
*/
|
|
89
|
+
async function encryptWithPassword(plaintext, password) {
|
|
90
|
+
const enc = new TextEncoder();
|
|
91
|
+
const iv = generateIV();
|
|
92
|
+
const salt = generateSalt();
|
|
93
|
+
const key = await deriveKey(password, salt);
|
|
94
|
+
const ivBuffer = uint8ArrayToArrayBuffer(iv);
|
|
95
|
+
const encryptedBuf = await crypto.subtle.encrypt({
|
|
96
|
+
name: "AES-GCM",
|
|
97
|
+
iv: ivBuffer
|
|
98
|
+
}, key, enc.encode(plaintext));
|
|
99
|
+
return {
|
|
100
|
+
encryptedData: uint8ArrayToBase64(new Uint8Array(encryptedBuf)),
|
|
101
|
+
iv: uint8ArrayToBase64(iv),
|
|
102
|
+
salt: uint8ArrayToBase64(salt)
|
|
103
|
+
};
|
|
107
104
|
}
|
|
108
105
|
/**
|
|
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
|
-
|
|
106
|
+
* Decrypt encrypted data with a password
|
|
107
|
+
*
|
|
108
|
+
* @param payload - Encrypted payload (Base64 encoded)
|
|
109
|
+
* @param password - Password used for encryption
|
|
110
|
+
* @returns Decrypted plaintext string
|
|
111
|
+
* @throws {Error} If decryption fails (wrong password or corrupted data)
|
|
112
|
+
*
|
|
113
|
+
* @example
|
|
114
|
+
* ```ts
|
|
115
|
+
* const decrypted = await decryptWithPassword(encrypted, 'my-password')
|
|
116
|
+
* ```
|
|
117
|
+
*/
|
|
118
|
+
async function decryptWithPassword(payload, password) {
|
|
119
|
+
const dec = new TextDecoder();
|
|
120
|
+
const ivBytes = base64ToUint8Array(payload.iv);
|
|
121
|
+
const saltBytes = base64ToUint8Array(payload.salt);
|
|
122
|
+
const encryptedBytes = base64ToUint8Array(payload.encryptedData);
|
|
123
|
+
const key = await deriveKey(password, saltBytes);
|
|
124
|
+
const iv = uint8ArrayToArrayBuffer(ivBytes);
|
|
125
|
+
const encrypted = uint8ArrayToArrayBuffer(encryptedBytes);
|
|
126
|
+
try {
|
|
127
|
+
const plaintextBuf = await crypto.subtle.decrypt({
|
|
128
|
+
name: "AES-GCM",
|
|
129
|
+
iv
|
|
130
|
+
}, key, encrypted);
|
|
131
|
+
return dec.decode(plaintextBuf);
|
|
132
|
+
} catch {
|
|
133
|
+
throw new Error("Decryption failed. Incorrect password or corrupted data.");
|
|
134
|
+
}
|
|
136
135
|
}
|
|
136
|
+
//#endregion
|
|
137
|
+
export { decryptWithPassword, deriveKey, encryptWithPassword, generateIV, generateSalt };
|
|
138
|
+
|
|
137
139
|
//# sourceMappingURL=encryption.js.map
|