@getpara/core-sdk 2.0.0-alpha.53 → 2.0.0-alpha.54
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cjs/ParaCore.js +213 -60
- package/dist/cjs/constants.js +7 -1
- package/dist/cjs/index.js +2 -0
- package/dist/cjs/shares/enclave.js +266 -0
- package/dist/cjs/shares/shareDistribution.js +16 -1
- package/dist/cjs/types/assets.js +15 -0
- package/dist/cjs/types/events.js +2 -0
- package/dist/cjs/utils/formatting.js +41 -0
- package/dist/esm/ParaCore.js +214 -61
- package/dist/esm/{chunk-7B52C2XE.js → chunk-W5CT3TVS.js} +2 -0
- package/dist/esm/constants.js +6 -2
- package/dist/esm/cryptography/utils.js +1 -1
- package/dist/esm/errors.js +1 -1
- package/dist/esm/external/mpcComputationClient.js +1 -1
- package/dist/esm/external/userManagementClient.js +1 -1
- package/dist/esm/index.js +3 -2
- package/dist/esm/shares/KeyContainer.js +1 -1
- package/dist/esm/shares/enclave.js +226 -0
- package/dist/esm/shares/recovery.js +1 -1
- package/dist/esm/shares/shareDistribution.js +17 -2
- package/dist/esm/transmission/transmissionUtils.js +1 -1
- package/dist/esm/types/assets.js +0 -0
- package/dist/esm/types/auth.js +1 -1
- package/dist/esm/types/config.js +1 -1
- package/dist/esm/types/coreApi.js +1 -1
- package/dist/esm/types/events.js +3 -1
- package/dist/esm/types/popup.js +1 -1
- package/dist/esm/types/wallet.js +1 -1
- package/dist/esm/utils/autobind.js +1 -1
- package/dist/esm/utils/events.js +1 -1
- package/dist/esm/utils/formatting.js +41 -1
- package/dist/esm/utils/json.js +1 -1
- package/dist/esm/utils/listeners.js +1 -1
- package/dist/esm/utils/onRamps.js +1 -1
- package/dist/esm/utils/phone.js +1 -1
- package/dist/esm/utils/polling.js +1 -1
- package/dist/esm/utils/types.js +1 -1
- package/dist/esm/utils/url.js +1 -1
- package/dist/esm/utils/wallet.js +1 -1
- package/dist/types/ParaCore.d.ts +15 -2
- package/dist/types/constants.d.ts +2 -0
- package/dist/types/index.d.ts +3 -2
- package/dist/types/shares/enclave.d.ts +81 -0
- package/dist/types/shares/shareDistribution.d.ts +4 -2
- package/dist/types/types/assets.d.ts +14 -0
- package/dist/types/types/config.d.ts +2 -0
- package/dist/types/types/coreApi.d.ts +1 -0
- package/dist/types/types/events.d.ts +7 -2
- package/dist/types/types/methods.d.ts +15 -7
- package/dist/types/utils/formatting.d.ts +10 -1
- package/package.json +3 -3
|
@@ -0,0 +1,266 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
3
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
4
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
5
|
+
var __export = (target, all) => {
|
|
6
|
+
for (var name in all)
|
|
7
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
8
|
+
};
|
|
9
|
+
var __copyProps = (to, from, except, desc) => {
|
|
10
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
11
|
+
for (let key of __getOwnPropNames(from))
|
|
12
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
13
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
14
|
+
}
|
|
15
|
+
return to;
|
|
16
|
+
};
|
|
17
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
18
|
+
var __async = (__this, __arguments, generator) => {
|
|
19
|
+
return new Promise((resolve, reject) => {
|
|
20
|
+
var fulfilled = (value) => {
|
|
21
|
+
try {
|
|
22
|
+
step(generator.next(value));
|
|
23
|
+
} catch (e) {
|
|
24
|
+
reject(e);
|
|
25
|
+
}
|
|
26
|
+
};
|
|
27
|
+
var rejected = (value) => {
|
|
28
|
+
try {
|
|
29
|
+
step(generator.throw(value));
|
|
30
|
+
} catch (e) {
|
|
31
|
+
reject(e);
|
|
32
|
+
}
|
|
33
|
+
};
|
|
34
|
+
var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
|
|
35
|
+
step((generator = generator.apply(__this, __arguments)).next());
|
|
36
|
+
});
|
|
37
|
+
};
|
|
38
|
+
var enclave_exports = {};
|
|
39
|
+
__export(enclave_exports, {
|
|
40
|
+
EnclaveClient: () => EnclaveClient
|
|
41
|
+
});
|
|
42
|
+
module.exports = __toCommonJS(enclave_exports);
|
|
43
|
+
class EnclaveClient {
|
|
44
|
+
constructor({
|
|
45
|
+
userManagementClient,
|
|
46
|
+
retrieveJwt,
|
|
47
|
+
persistJwt,
|
|
48
|
+
retrieveRefreshJwt,
|
|
49
|
+
persistRefreshJwt
|
|
50
|
+
}) {
|
|
51
|
+
this.enclavePublicKey = null;
|
|
52
|
+
this.frontendKeyPair = null;
|
|
53
|
+
this.userManagementClient = userManagementClient;
|
|
54
|
+
this.retrieveJwt = retrieveJwt;
|
|
55
|
+
this.persistJwt = persistJwt;
|
|
56
|
+
this.retrieveRefreshJwt = retrieveRefreshJwt;
|
|
57
|
+
this.persistRefreshJwt = persistRefreshJwt;
|
|
58
|
+
}
|
|
59
|
+
refreshJwt() {
|
|
60
|
+
return __async(this, null, function* () {
|
|
61
|
+
const frontendKeyPair = yield this.generateFrontendKeyPair();
|
|
62
|
+
const responsePublicKeyPEM = yield this.exportPublicKeyToPEM(frontendKeyPair.publicKey);
|
|
63
|
+
const payload = {
|
|
64
|
+
refreshJwt: this.retrieveRefreshJwt(),
|
|
65
|
+
responsePublicKey: responsePublicKeyPEM
|
|
66
|
+
};
|
|
67
|
+
const encryptedPayload = yield this.encryptForEnclave(JSON.stringify(payload));
|
|
68
|
+
const response = yield this.userManagementClient.refreshEnclaveJwt(JSON.stringify(encryptedPayload));
|
|
69
|
+
const decryptedResponse = yield this.decryptForFrontend(JSON.parse(response.payload));
|
|
70
|
+
this.persistJwt(decryptedResponse.jwt);
|
|
71
|
+
this.persistRefreshJwt(decryptedResponse.refreshJwt);
|
|
72
|
+
});
|
|
73
|
+
}
|
|
74
|
+
withJwtRefreshRetry(fn) {
|
|
75
|
+
return __async(this, null, function* () {
|
|
76
|
+
try {
|
|
77
|
+
return yield fn();
|
|
78
|
+
} catch (error) {
|
|
79
|
+
yield this.refreshJwt();
|
|
80
|
+
return yield fn();
|
|
81
|
+
}
|
|
82
|
+
});
|
|
83
|
+
}
|
|
84
|
+
issueEnclaveJwt() {
|
|
85
|
+
return __async(this, null, function* () {
|
|
86
|
+
const frontendKeyPair = yield this.generateFrontendKeyPair();
|
|
87
|
+
const responsePublicKeyPEM = yield this.exportPublicKeyToPEM(frontendKeyPair.publicKey);
|
|
88
|
+
const payload = {
|
|
89
|
+
responsePublicKey: responsePublicKeyPEM
|
|
90
|
+
};
|
|
91
|
+
const encryptedPayload = yield this.encryptForEnclave(JSON.stringify(payload));
|
|
92
|
+
const response = yield this.userManagementClient.issueEnclaveJwt(JSON.stringify(encryptedPayload));
|
|
93
|
+
const decryptedResponse = yield this.decryptForFrontend(JSON.parse(response));
|
|
94
|
+
this.persistJwt(decryptedResponse.jwt);
|
|
95
|
+
});
|
|
96
|
+
}
|
|
97
|
+
/**
|
|
98
|
+
* Generate a P-256 keypair for the frontend to receive encrypted responses
|
|
99
|
+
*/
|
|
100
|
+
generateFrontendKeyPair() {
|
|
101
|
+
return __async(this, null, function* () {
|
|
102
|
+
if (this.frontendKeyPair) {
|
|
103
|
+
return this.frontendKeyPair;
|
|
104
|
+
}
|
|
105
|
+
this.frontendKeyPair = yield crypto.subtle.generateKey({ name: "ECDH", namedCurve: "P-256" }, true, ["deriveBits"]);
|
|
106
|
+
return this.frontendKeyPair;
|
|
107
|
+
});
|
|
108
|
+
}
|
|
109
|
+
/**
|
|
110
|
+
* Get the enclave's public key from the user-management service
|
|
111
|
+
*/
|
|
112
|
+
getEnclavePublicKey() {
|
|
113
|
+
return __async(this, null, function* () {
|
|
114
|
+
if (this.enclavePublicKey) {
|
|
115
|
+
return this.enclavePublicKey;
|
|
116
|
+
}
|
|
117
|
+
const response = yield this.userManagementClient.getEnclavePublicKey();
|
|
118
|
+
this.enclavePublicKey = response.publicKey;
|
|
119
|
+
return this.enclavePublicKey;
|
|
120
|
+
});
|
|
121
|
+
}
|
|
122
|
+
/**
|
|
123
|
+
* Import a PEM-formatted public key for use with Web Crypto API
|
|
124
|
+
*/
|
|
125
|
+
importPublicKeyFromPEM(pemString) {
|
|
126
|
+
return __async(this, null, function* () {
|
|
127
|
+
const pemContents = pemString.replace("-----BEGIN PUBLIC KEY-----", "").replace("-----END PUBLIC KEY-----", "").replace(/\s/g, "");
|
|
128
|
+
const keyData = Uint8Array.from(atob(pemContents), (c) => c.charCodeAt(0));
|
|
129
|
+
return yield crypto.subtle.importKey("spki", keyData, { name: "ECDH", namedCurve: "P-256" }, false, []);
|
|
130
|
+
});
|
|
131
|
+
}
|
|
132
|
+
/**
|
|
133
|
+
* Export a public key to PEM format
|
|
134
|
+
*/
|
|
135
|
+
exportPublicKeyToPEM(publicKey) {
|
|
136
|
+
return __async(this, null, function* () {
|
|
137
|
+
const exported = yield crypto.subtle.exportKey("spki", publicKey);
|
|
138
|
+
const exportedAsBase64 = btoa(String.fromCharCode(...new Uint8Array(exported)));
|
|
139
|
+
return `-----BEGIN PUBLIC KEY-----
|
|
140
|
+
${exportedAsBase64}
|
|
141
|
+
-----END PUBLIC KEY-----`;
|
|
142
|
+
});
|
|
143
|
+
}
|
|
144
|
+
/**
|
|
145
|
+
* Encrypt data using P-256 ECIES for the enclave
|
|
146
|
+
*/
|
|
147
|
+
encryptForEnclave(plaintext) {
|
|
148
|
+
return __async(this, null, function* () {
|
|
149
|
+
const enclavePublicKeyPEM = yield this.getEnclavePublicKey();
|
|
150
|
+
const enclavePublicKey = yield this.importPublicKeyFromPEM(enclavePublicKeyPEM);
|
|
151
|
+
const ephemeralKeyPair = yield crypto.subtle.generateKey({ name: "ECDH", namedCurve: "P-256" }, true, ["deriveBits"]);
|
|
152
|
+
const sharedSecretBits = yield crypto.subtle.deriveBits(
|
|
153
|
+
{ name: "ECDH", public: enclavePublicKey },
|
|
154
|
+
ephemeralKeyPair.privateKey,
|
|
155
|
+
256
|
|
156
|
+
// 32 bytes = 256 bits
|
|
157
|
+
);
|
|
158
|
+
const encryptionKeyBuffer = yield crypto.subtle.digest("SHA-256", sharedSecretBits);
|
|
159
|
+
const encryptionKey = yield crypto.subtle.importKey("raw", encryptionKeyBuffer, { name: "AES-GCM" }, false, ["encrypt"]);
|
|
160
|
+
const iv = crypto.getRandomValues(new Uint8Array(12));
|
|
161
|
+
const encrypted = yield crypto.subtle.encrypt(
|
|
162
|
+
{ name: "AES-GCM", iv },
|
|
163
|
+
encryptionKey,
|
|
164
|
+
new TextEncoder().encode(plaintext)
|
|
165
|
+
);
|
|
166
|
+
const encryptedArray = new Uint8Array(encrypted);
|
|
167
|
+
const combined = new Uint8Array(iv.length + encryptedArray.length);
|
|
168
|
+
combined.set(iv);
|
|
169
|
+
combined.set(encryptedArray, iv.length);
|
|
170
|
+
const ephemeralPublicKeyBuffer = yield crypto.subtle.exportKey("spki", ephemeralKeyPair.publicKey);
|
|
171
|
+
return {
|
|
172
|
+
encryptedData: btoa(String.fromCharCode(...combined)),
|
|
173
|
+
keyId: "",
|
|
174
|
+
// Will be set by the enclave
|
|
175
|
+
algorithm: "ECIES-P256-AES256-SHA256",
|
|
176
|
+
ephemeral: btoa(String.fromCharCode(...new Uint8Array(ephemeralPublicKeyBuffer)))
|
|
177
|
+
};
|
|
178
|
+
});
|
|
179
|
+
}
|
|
180
|
+
/**
|
|
181
|
+
* Decrypt response encrypted for the frontend
|
|
182
|
+
*/
|
|
183
|
+
decryptForFrontend(encryptedPayload) {
|
|
184
|
+
return __async(this, null, function* () {
|
|
185
|
+
if (!this.frontendKeyPair) {
|
|
186
|
+
throw new Error("Frontend keypair not available");
|
|
187
|
+
}
|
|
188
|
+
const encryptedData = Uint8Array.from(atob(encryptedPayload.encryptedData), (c) => c.charCodeAt(0));
|
|
189
|
+
const ephemeralPublicKeyData = Uint8Array.from(atob(encryptedPayload.ephemeral), (c) => c.charCodeAt(0));
|
|
190
|
+
const ephemeralPublicKey = yield crypto.subtle.importKey(
|
|
191
|
+
"spki",
|
|
192
|
+
ephemeralPublicKeyData,
|
|
193
|
+
{ name: "ECDH", namedCurve: "P-256" },
|
|
194
|
+
false,
|
|
195
|
+
[]
|
|
196
|
+
);
|
|
197
|
+
const sharedSecretBits = yield crypto.subtle.deriveBits(
|
|
198
|
+
{ name: "ECDH", public: ephemeralPublicKey },
|
|
199
|
+
this.frontendKeyPair.privateKey,
|
|
200
|
+
256
|
|
201
|
+
);
|
|
202
|
+
const encryptionKeyBuffer = yield crypto.subtle.digest("SHA-256", sharedSecretBits);
|
|
203
|
+
const encryptionKey = yield crypto.subtle.importKey("raw", encryptionKeyBuffer, { name: "AES-GCM" }, false, ["decrypt"]);
|
|
204
|
+
const iv = encryptedData.slice(0, 12);
|
|
205
|
+
const ciphertext = encryptedData.slice(12);
|
|
206
|
+
const decrypted = yield crypto.subtle.decrypt({ name: "AES-GCM", iv }, encryptionKey, ciphertext);
|
|
207
|
+
return JSON.parse(new TextDecoder().decode(decrypted));
|
|
208
|
+
});
|
|
209
|
+
}
|
|
210
|
+
/**
|
|
211
|
+
* Persist key shares to the enclave
|
|
212
|
+
* @param shares Array of share data to persist
|
|
213
|
+
*/
|
|
214
|
+
persistShares(shares) {
|
|
215
|
+
return __async(this, null, function* () {
|
|
216
|
+
const payload = {
|
|
217
|
+
shares,
|
|
218
|
+
jwt: this.retrieveJwt()
|
|
219
|
+
};
|
|
220
|
+
const encryptedPayload = yield this.encryptForEnclave(JSON.stringify(payload));
|
|
221
|
+
const encryptedPayloadStr = JSON.stringify(encryptedPayload);
|
|
222
|
+
return yield this.userManagementClient.persistEnclaveShares(encryptedPayloadStr);
|
|
223
|
+
});
|
|
224
|
+
}
|
|
225
|
+
/**
|
|
226
|
+
* Retrieve key shares from the enclave
|
|
227
|
+
* @param query Query parameters for finding shares (single query or array of queries)
|
|
228
|
+
*/
|
|
229
|
+
retrieveShares(query) {
|
|
230
|
+
return __async(this, null, function* () {
|
|
231
|
+
yield this.issueEnclaveJwt();
|
|
232
|
+
const frontendKeyPair = yield this.generateFrontendKeyPair();
|
|
233
|
+
const responsePublicKeyPEM = yield this.exportPublicKeyToPEM(frontendKeyPair.publicKey);
|
|
234
|
+
const fullQuery = query.map((q) => ({
|
|
235
|
+
userId: q.userId
|
|
236
|
+
}));
|
|
237
|
+
const payload = {
|
|
238
|
+
query: fullQuery,
|
|
239
|
+
responsePublicKey: responsePublicKeyPEM,
|
|
240
|
+
jwt: this.retrieveJwt()
|
|
241
|
+
};
|
|
242
|
+
const encryptedPayload = yield this.encryptForEnclave(JSON.stringify(payload));
|
|
243
|
+
const encryptedPayloadStr = JSON.stringify(encryptedPayload);
|
|
244
|
+
const response = yield this.userManagementClient.retrieveEnclaveShares(encryptedPayloadStr);
|
|
245
|
+
const encryptedResponse = JSON.parse(response.payload);
|
|
246
|
+
const decryptedData = yield this.decryptForFrontend(encryptedResponse);
|
|
247
|
+
return decryptedData.shares;
|
|
248
|
+
});
|
|
249
|
+
}
|
|
250
|
+
retrieveSharesWithRetry(query) {
|
|
251
|
+
return __async(this, null, function* () {
|
|
252
|
+
return yield this.withJwtRefreshRetry(() => __async(this, null, function* () {
|
|
253
|
+
return this.retrieveShares(query);
|
|
254
|
+
}));
|
|
255
|
+
});
|
|
256
|
+
}
|
|
257
|
+
persistSharesWithRetry(shares) {
|
|
258
|
+
return __async(this, null, function* () {
|
|
259
|
+
return yield this.persistShares(shares);
|
|
260
|
+
});
|
|
261
|
+
}
|
|
262
|
+
}
|
|
263
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
264
|
+
0 && (module.exports = {
|
|
265
|
+
EnclaveClient
|
|
266
|
+
});
|
|
@@ -52,8 +52,23 @@ function distributeNewShare(_0) {
|
|
|
52
52
|
ignoreRedistributingBackupEncryptedShare = false,
|
|
53
53
|
emailProps = {},
|
|
54
54
|
partnerId,
|
|
55
|
-
protocolId
|
|
55
|
+
protocolId,
|
|
56
|
+
isEnclaveUser,
|
|
57
|
+
walletScheme
|
|
56
58
|
}) {
|
|
59
|
+
if (isEnclaveUser) {
|
|
60
|
+
yield ctx.enclaveClient.persistSharesWithRetry([
|
|
61
|
+
{
|
|
62
|
+
userId,
|
|
63
|
+
walletId,
|
|
64
|
+
walletScheme,
|
|
65
|
+
signer: userShare,
|
|
66
|
+
partnerId,
|
|
67
|
+
protocolId
|
|
68
|
+
}
|
|
69
|
+
]);
|
|
70
|
+
return "";
|
|
71
|
+
}
|
|
57
72
|
const publicKeysRes = yield ctx.client.getSessionPublicKeys(userId);
|
|
58
73
|
const biometricEncryptedShares = publicKeysRes.data.keys.map((key) => {
|
|
59
74
|
if (!key.publicKey) {
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
3
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
4
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
5
|
+
var __copyProps = (to, from, except, desc) => {
|
|
6
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
7
|
+
for (let key of __getOwnPropNames(from))
|
|
8
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
9
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
10
|
+
}
|
|
11
|
+
return to;
|
|
12
|
+
};
|
|
13
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
14
|
+
var assets_exports = {};
|
|
15
|
+
module.exports = __toCommonJS(assets_exports);
|
package/dist/cjs/types/events.js
CHANGED
|
@@ -33,6 +33,8 @@ var ParaEvent = ((ParaEvent2) => {
|
|
|
33
33
|
ParaEvent2["WALLET_CREATED"] = `${EVENT_PREFIX}WalletCreated`;
|
|
34
34
|
ParaEvent2["PREGEN_WALLET_CLAIMED"] = `${EVENT_PREFIX}PregenWalletClaimed`;
|
|
35
35
|
ParaEvent2["GUEST_WALLETS_CREATED"] = `${EVENT_PREFIX}GuestWalletsCreated`;
|
|
36
|
+
ParaEvent2["ASSET_TRANSFERRED"] = `${EVENT_PREFIX}AssetTransferred`;
|
|
37
|
+
ParaEvent2["ONRAMP_TRANSACTION_COMPLETE"] = `${EVENT_PREFIX}OnRampTransactionComplete`;
|
|
36
38
|
return ParaEvent2;
|
|
37
39
|
})(ParaEvent || {});
|
|
38
40
|
// Annotate the CommonJS export names for ESM import in node:
|
|
@@ -4,6 +4,7 @@ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
|
4
4
|
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
5
|
var __getProtoOf = Object.getPrototypeOf;
|
|
6
6
|
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
7
|
+
var __pow = Math.pow;
|
|
7
8
|
var __export = (target, all) => {
|
|
8
9
|
for (var name in all)
|
|
9
10
|
__defProp(target, name, { get: all[name], enumerable: true });
|
|
@@ -29,6 +30,8 @@ var formatting_exports = {};
|
|
|
29
30
|
__export(formatting_exports, {
|
|
30
31
|
compressPubkey: () => compressPubkey,
|
|
31
32
|
decimalToHex: () => decimalToHex,
|
|
33
|
+
formatAssetQuantity: () => formatAssetQuantity,
|
|
34
|
+
formatCurrency: () => formatCurrency,
|
|
32
35
|
getCosmosAddress: () => getCosmosAddress,
|
|
33
36
|
hexStringToBase64: () => hexStringToBase64,
|
|
34
37
|
hexToDecimal: () => hexToDecimal,
|
|
@@ -102,10 +105,48 @@ function truncateAddress(str, addressType, {
|
|
|
102
105
|
const margin = targetLength !== void 0 ? (targetLength - minimum) / 2 : 4;
|
|
103
106
|
return `${str.slice(0, minimum + margin)}...${str.slice(-1 * margin)}`;
|
|
104
107
|
}
|
|
108
|
+
function formatCurrency(value, { fallback = "" } = {}) {
|
|
109
|
+
if (!value) {
|
|
110
|
+
return fallback;
|
|
111
|
+
}
|
|
112
|
+
const formatter = new Intl.NumberFormat("en-US", {
|
|
113
|
+
style: "currency",
|
|
114
|
+
currency: value.currency
|
|
115
|
+
});
|
|
116
|
+
const zeroFormatter = new Intl.NumberFormat("en-US", {
|
|
117
|
+
style: "currency",
|
|
118
|
+
currency: value.currency,
|
|
119
|
+
maximumFractionDigits: 0
|
|
120
|
+
});
|
|
121
|
+
return Math.abs(value.value) < 0.01 ? zeroFormatter.format(0) : formatter.format(value.value);
|
|
122
|
+
}
|
|
123
|
+
const zeroAssetFormatter = new Intl.NumberFormat("en-US", {
|
|
124
|
+
style: "decimal",
|
|
125
|
+
maximumFractionDigits: 0,
|
|
126
|
+
minimumFractionDigits: 0
|
|
127
|
+
});
|
|
128
|
+
function formatAssetQuantity({
|
|
129
|
+
quantity,
|
|
130
|
+
symbol = "",
|
|
131
|
+
decimals,
|
|
132
|
+
fallback = ""
|
|
133
|
+
}) {
|
|
134
|
+
if (!quantity) {
|
|
135
|
+
return fallback;
|
|
136
|
+
}
|
|
137
|
+
const formatter = new Intl.NumberFormat("en-US", {
|
|
138
|
+
style: "decimal",
|
|
139
|
+
maximumFractionDigits: decimals != null ? decimals : Math.abs(quantity) < 1e-3 ? 6 : 3,
|
|
140
|
+
minimumFractionDigits: decimals != null ? decimals : 3
|
|
141
|
+
});
|
|
142
|
+
return `${Math.abs(quantity) < __pow(10, -1 * (decimals != null ? decimals : 6)) ? zeroAssetFormatter.format(0) : formatter.format(quantity)}${symbol && symbol.length > 0 ? ` ${symbol}` : ""}`;
|
|
143
|
+
}
|
|
105
144
|
// Annotate the CommonJS export names for ESM import in node:
|
|
106
145
|
0 && (module.exports = {
|
|
107
146
|
compressPubkey,
|
|
108
147
|
decimalToHex,
|
|
148
|
+
formatAssetQuantity,
|
|
149
|
+
formatCurrency,
|
|
109
150
|
getCosmosAddress,
|
|
110
151
|
hexStringToBase64,
|
|
111
152
|
hexToDecimal,
|