near-kit 0.0.0 → 0.2.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/LICENSE +21 -0
- package/README.md +473 -2
- package/dist/contracts/contract.d.ts +63 -0
- package/dist/contracts/contract.d.ts.map +1 -0
- package/dist/contracts/contract.js +42 -0
- package/dist/contracts/contract.js.map +1 -0
- package/dist/contracts/index.d.ts +5 -0
- package/dist/contracts/index.d.ts.map +1 -0
- package/dist/contracts/index.js +5 -0
- package/dist/contracts/index.js.map +1 -0
- package/dist/core/actions.d.ts +193 -0
- package/dist/core/actions.d.ts.map +1 -0
- package/dist/core/actions.js +195 -0
- package/dist/core/actions.js.map +1 -0
- package/dist/core/config-schemas.d.ts +179 -0
- package/dist/core/config-schemas.d.ts.map +1 -0
- package/dist/core/config-schemas.js +169 -0
- package/dist/core/config-schemas.js.map +1 -0
- package/dist/core/constants.d.ts +43 -0
- package/dist/core/constants.d.ts.map +1 -0
- package/dist/core/constants.js +49 -0
- package/dist/core/constants.js.map +1 -0
- package/dist/core/near.d.ts +301 -0
- package/dist/core/near.d.ts.map +1 -0
- package/dist/core/near.js +504 -0
- package/dist/core/near.js.map +1 -0
- package/dist/core/nonce-manager.d.ts +39 -0
- package/dist/core/nonce-manager.d.ts.map +1 -0
- package/dist/core/nonce-manager.js +73 -0
- package/dist/core/nonce-manager.js.map +1 -0
- package/dist/core/rpc/rpc-error-handler.d.ts +60 -0
- package/dist/core/rpc/rpc-error-handler.d.ts.map +1 -0
- package/dist/core/rpc/rpc-error-handler.js +324 -0
- package/dist/core/rpc/rpc-error-handler.js.map +1 -0
- package/dist/core/rpc/rpc-schemas.d.ts +1812 -0
- package/dist/core/rpc/rpc-schemas.d.ts.map +1 -0
- package/dist/core/rpc/rpc-schemas.js +424 -0
- package/dist/core/rpc/rpc-schemas.js.map +1 -0
- package/dist/core/rpc/rpc.d.ts +117 -0
- package/dist/core/rpc/rpc.d.ts.map +1 -0
- package/dist/core/rpc/rpc.js +325 -0
- package/dist/core/rpc/rpc.js.map +1 -0
- package/dist/core/schema.d.ts +1188 -0
- package/dist/core/schema.d.ts.map +1 -0
- package/dist/core/schema.js +396 -0
- package/dist/core/schema.js.map +1 -0
- package/dist/core/transaction.d.ts +390 -0
- package/dist/core/transaction.d.ts.map +1 -0
- package/dist/core/transaction.js +653 -0
- package/dist/core/transaction.js.map +1 -0
- package/dist/core/types.d.ts +271 -0
- package/dist/core/types.d.ts.map +1 -0
- package/dist/core/types.js +9 -0
- package/dist/core/types.js.map +1 -0
- package/dist/errors/index.d.ts +226 -0
- package/dist/errors/index.d.ts.map +1 -0
- package/dist/errors/index.js +366 -0
- package/dist/errors/index.js.map +1 -0
- package/dist/index.d.ts +16 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +23 -0
- package/dist/index.js.map +1 -0
- package/dist/keys/credential-schemas.d.ts +98 -0
- package/dist/keys/credential-schemas.d.ts.map +1 -0
- package/dist/keys/credential-schemas.js +128 -0
- package/dist/keys/credential-schemas.js.map +1 -0
- package/dist/keys/file-keystore.d.ts +130 -0
- package/dist/keys/file-keystore.d.ts.map +1 -0
- package/dist/keys/file-keystore.js +266 -0
- package/dist/keys/file-keystore.js.map +1 -0
- package/dist/keys/in-memory-keystore.d.ts +71 -0
- package/dist/keys/in-memory-keystore.d.ts.map +1 -0
- package/dist/keys/in-memory-keystore.js +85 -0
- package/dist/keys/in-memory-keystore.js.map +1 -0
- package/dist/keys/index.d.ts +14 -0
- package/dist/keys/index.d.ts.map +1 -0
- package/dist/keys/index.js +20 -0
- package/dist/keys/index.js.map +1 -0
- package/dist/keys/native-keystore.d.ts +111 -0
- package/dist/keys/native-keystore.d.ts.map +1 -0
- package/dist/keys/native-keystore.js +167 -0
- package/dist/keys/native-keystore.js.map +1 -0
- package/dist/keys/rotating-keystore.d.ts +207 -0
- package/dist/keys/rotating-keystore.d.ts.map +1 -0
- package/dist/keys/rotating-keystore.js +240 -0
- package/dist/keys/rotating-keystore.js.map +1 -0
- package/dist/sandbox/index.d.ts +6 -0
- package/dist/sandbox/index.d.ts.map +1 -0
- package/dist/sandbox/index.js +5 -0
- package/dist/sandbox/index.js.map +1 -0
- package/dist/sandbox/sandbox.d.ts +55 -0
- package/dist/sandbox/sandbox.d.ts.map +1 -0
- package/dist/sandbox/sandbox.js +341 -0
- package/dist/sandbox/sandbox.js.map +1 -0
- package/dist/utils/amount.d.ts +76 -0
- package/dist/utils/amount.d.ts.map +1 -0
- package/dist/utils/amount.js +137 -0
- package/dist/utils/amount.js.map +1 -0
- package/dist/utils/gas.d.ts +69 -0
- package/dist/utils/gas.d.ts.map +1 -0
- package/dist/utils/gas.js +92 -0
- package/dist/utils/gas.js.map +1 -0
- package/dist/utils/index.d.ts +14 -0
- package/dist/utils/index.d.ts.map +1 -0
- package/dist/utils/index.js +14 -0
- package/dist/utils/index.js.map +1 -0
- package/dist/utils/key.d.ts +117 -0
- package/dist/utils/key.d.ts.map +1 -0
- package/dist/utils/key.js +270 -0
- package/dist/utils/key.js.map +1 -0
- package/dist/utils/nep413.d.ts +97 -0
- package/dist/utils/nep413.d.ts.map +1 -0
- package/dist/utils/nep413.js +154 -0
- package/dist/utils/nep413.js.map +1 -0
- package/dist/utils/validation.d.ts +114 -0
- package/dist/utils/validation.d.ts.map +1 -0
- package/dist/utils/validation.js +150 -0
- package/dist/utils/validation.js.map +1 -0
- package/dist/wallets/adapters.d.ts +119 -0
- package/dist/wallets/adapters.d.ts.map +1 -0
- package/dist/wallets/adapters.js +267 -0
- package/dist/wallets/adapters.js.map +1 -0
- package/dist/wallets/index.d.ts +11 -0
- package/dist/wallets/index.d.ts.map +1 -0
- package/dist/wallets/index.js +2 -0
- package/dist/wallets/index.js.map +1 -0
- package/dist/wallets/types.d.ts +99 -0
- package/dist/wallets/types.d.ts.map +1 -0
- package/dist/wallets/types.js +10 -0
- package/dist/wallets/types.js.map +1 -0
- package/package.json +78 -7
- package/index.js +0 -1
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Amount utilities for NEAR tokens.
|
|
3
|
+
*
|
|
4
|
+
* All amounts must specify units explicitly:
|
|
5
|
+
* - `Amount.NEAR(10)` → `"10 NEAR"`
|
|
6
|
+
* - `Amount.yocto(1000n)` → `"1000 yocto"`
|
|
7
|
+
* - `"10 NEAR"` (string literal)
|
|
8
|
+
* - `"1000 yocto"` (string literal)
|
|
9
|
+
* - `1000n` (bigint literal, treated as yoctoNEAR)
|
|
10
|
+
*
|
|
11
|
+
* No bare numbers are accepted in order to prevent accidental unit confusion.
|
|
12
|
+
*/
|
|
13
|
+
/**
|
|
14
|
+
* Amount input type - must be a string with unit specification or bigint.
|
|
15
|
+
*
|
|
16
|
+
* Accepts:
|
|
17
|
+
* - Literal strings: "10 NEAR", "10.5 NEAR", "1000 yocto"
|
|
18
|
+
* - Constructor output: Amount.NEAR(10), Amount.yocto(1000n)
|
|
19
|
+
* - Raw bigint: 1000000n (interpreted as yoctoNEAR)
|
|
20
|
+
*
|
|
21
|
+
* For variables, use 'as const' to preserve literal type:
|
|
22
|
+
* @example
|
|
23
|
+
* const amount = "10 NEAR" as const
|
|
24
|
+
* const yocto = 1000000n // Direct bigint is treated as yoctoNEAR
|
|
25
|
+
*/
|
|
26
|
+
export type AmountInput = `${number} NEAR` | `${number}.${number} NEAR` | `${number} yocto` | ReturnType<typeof Amount.NEAR> | ReturnType<typeof Amount.yocto> | bigint;
|
|
27
|
+
/**
|
|
28
|
+
* Amount namespace - explicit constructors for NEAR values.
|
|
29
|
+
*
|
|
30
|
+
* @example
|
|
31
|
+
* Amount.NEAR(10) // "10 NEAR"
|
|
32
|
+
* Amount.NEAR(10.5) // "10.5 NEAR"
|
|
33
|
+
* Amount.NEAR("10.5") // "10.5 NEAR"
|
|
34
|
+
* Amount.yocto(1000000n) // "1000000 yocto"
|
|
35
|
+
* Amount.yocto("1000000") // "1000000 yocto"
|
|
36
|
+
*/
|
|
37
|
+
export declare const Amount: {
|
|
38
|
+
/**
|
|
39
|
+
* Create a NEAR amount.
|
|
40
|
+
* @param value - Amount in NEAR.
|
|
41
|
+
* @returns Formatted string `"X NEAR"`.
|
|
42
|
+
*/
|
|
43
|
+
readonly NEAR: (value: number | string) => string;
|
|
44
|
+
/**
|
|
45
|
+
* Create a yoctoNEAR amount (10^-24 NEAR).
|
|
46
|
+
* @param value - Amount in yoctoNEAR.
|
|
47
|
+
* @returns Formatted string `"X yocto"`.
|
|
48
|
+
*/
|
|
49
|
+
readonly yocto: (value: bigint | string) => string;
|
|
50
|
+
/**
|
|
51
|
+
* Common amount constants.
|
|
52
|
+
*/
|
|
53
|
+
readonly ZERO: "0 yocto";
|
|
54
|
+
readonly ONE_NEAR: "1 NEAR";
|
|
55
|
+
readonly ONE_YOCTO: "1 yocto";
|
|
56
|
+
};
|
|
57
|
+
/**
|
|
58
|
+
* Parse amount to yoctoNEAR.
|
|
59
|
+
*
|
|
60
|
+
* @param amount - Amount with unit (e.g., `"10 NEAR"`, `"1000 yocto"`) or bigint (treated as yoctoNEAR).
|
|
61
|
+
* @returns Amount in yoctoNEAR as a string.
|
|
62
|
+
* @throws Error If the format is invalid or ambiguous (e.g. bare numbers).
|
|
63
|
+
*/
|
|
64
|
+
export declare function parseAmount(amount: AmountInput): string;
|
|
65
|
+
/**
|
|
66
|
+
* Format yoctoNEAR to human-readable NEAR
|
|
67
|
+
* @param yocto - Amount in yoctoNEAR
|
|
68
|
+
* @param options - Formatting options
|
|
69
|
+
* @returns Formatted string (e.g., "10.50 NEAR")
|
|
70
|
+
*/
|
|
71
|
+
export declare function formatAmount(yocto: string | bigint, options?: {
|
|
72
|
+
precision?: number;
|
|
73
|
+
includeSuffix?: boolean;
|
|
74
|
+
trimZeros?: boolean;
|
|
75
|
+
}): string;
|
|
76
|
+
//# sourceMappingURL=amount.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"amount.d.ts","sourceRoot":"","sources":["../../src/utils/amount.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;GAWG;AAIH;;;;;;;;;;;;GAYG;AACH,MAAM,MAAM,WAAW,GACnB,GAAG,MAAM,OAAO,GAChB,GAAG,MAAM,IAAI,MAAM,OAAO,GAC1B,GAAG,MAAM,QAAQ,GACjB,UAAU,CAAC,OAAO,MAAM,CAAC,IAAI,CAAC,GAC9B,UAAU,CAAC,OAAO,MAAM,CAAC,KAAK,CAAC,GAC/B,MAAM,CAAA;AAEV;;;;;;;;;GASG;AACH,eAAO,MAAM,MAAM;IACjB;;;;OAIG;2BACS,MAAM,GAAG,MAAM,KAAG,MAAM;IAIpC;;;;OAIG;4BACU,MAAM,GAAG,MAAM,KAAG,MAAM;IAIrC;;OAEG;;;;CAIK,CAAA;AAEV;;;;;;GAMG;AACH,wBAAgB,WAAW,CAAC,MAAM,EAAE,WAAW,GAAG,MAAM,CA+CvD;AAED;;;;;GAKG;AACH,wBAAgB,YAAY,CAC1B,KAAK,EAAE,MAAM,GAAG,MAAM,EACtB,OAAO,CAAC,EAAE;IACR,SAAS,CAAC,EAAE,MAAM,CAAA;IAClB,aAAa,CAAC,EAAE,OAAO,CAAA;IACvB,SAAS,CAAC,EAAE,OAAO,CAAA;CACpB,GACA,MAAM,CA4BR"}
|
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Amount utilities for NEAR tokens.
|
|
3
|
+
*
|
|
4
|
+
* All amounts must specify units explicitly:
|
|
5
|
+
* - `Amount.NEAR(10)` → `"10 NEAR"`
|
|
6
|
+
* - `Amount.yocto(1000n)` → `"1000 yocto"`
|
|
7
|
+
* - `"10 NEAR"` (string literal)
|
|
8
|
+
* - `"1000 yocto"` (string literal)
|
|
9
|
+
* - `1000n` (bigint literal, treated as yoctoNEAR)
|
|
10
|
+
*
|
|
11
|
+
* No bare numbers are accepted in order to prevent accidental unit confusion.
|
|
12
|
+
*/
|
|
13
|
+
import { YOCTO_PER_NEAR } from "../core/constants.js";
|
|
14
|
+
/**
|
|
15
|
+
* Amount namespace - explicit constructors for NEAR values.
|
|
16
|
+
*
|
|
17
|
+
* @example
|
|
18
|
+
* Amount.NEAR(10) // "10 NEAR"
|
|
19
|
+
* Amount.NEAR(10.5) // "10.5 NEAR"
|
|
20
|
+
* Amount.NEAR("10.5") // "10.5 NEAR"
|
|
21
|
+
* Amount.yocto(1000000n) // "1000000 yocto"
|
|
22
|
+
* Amount.yocto("1000000") // "1000000 yocto"
|
|
23
|
+
*/
|
|
24
|
+
export const Amount = {
|
|
25
|
+
/**
|
|
26
|
+
* Create a NEAR amount.
|
|
27
|
+
* @param value - Amount in NEAR.
|
|
28
|
+
* @returns Formatted string `"X NEAR"`.
|
|
29
|
+
*/
|
|
30
|
+
NEAR(value) {
|
|
31
|
+
return `${value} NEAR`;
|
|
32
|
+
},
|
|
33
|
+
/**
|
|
34
|
+
* Create a yoctoNEAR amount (10^-24 NEAR).
|
|
35
|
+
* @param value - Amount in yoctoNEAR.
|
|
36
|
+
* @returns Formatted string `"X yocto"`.
|
|
37
|
+
*/
|
|
38
|
+
yocto(value) {
|
|
39
|
+
return `${value} yocto`;
|
|
40
|
+
},
|
|
41
|
+
/**
|
|
42
|
+
* Common amount constants.
|
|
43
|
+
*/
|
|
44
|
+
ZERO: "0 yocto",
|
|
45
|
+
ONE_NEAR: "1 NEAR",
|
|
46
|
+
ONE_YOCTO: "1 yocto",
|
|
47
|
+
};
|
|
48
|
+
/**
|
|
49
|
+
* Parse amount to yoctoNEAR.
|
|
50
|
+
*
|
|
51
|
+
* @param amount - Amount with unit (e.g., `"10 NEAR"`, `"1000 yocto"`) or bigint (treated as yoctoNEAR).
|
|
52
|
+
* @returns Amount in yoctoNEAR as a string.
|
|
53
|
+
* @throws Error If the format is invalid or ambiguous (e.g. bare numbers).
|
|
54
|
+
*/
|
|
55
|
+
export function parseAmount(amount) {
|
|
56
|
+
// Handle bigint directly (treated as yoctoNEAR)
|
|
57
|
+
if (typeof amount === "bigint") {
|
|
58
|
+
return amount.toString();
|
|
59
|
+
}
|
|
60
|
+
const trimmed = amount.trim();
|
|
61
|
+
// Parse "X NEAR" format (case insensitive)
|
|
62
|
+
const nearMatch = trimmed.match(/^([\d.]+)\s+NEAR$/i);
|
|
63
|
+
if (nearMatch) {
|
|
64
|
+
// Safe to use non-null assertion after match check
|
|
65
|
+
// biome-ignore lint/style/noNonNullAssertion: regex capture group guaranteed to exist when match succeeds
|
|
66
|
+
const value = nearMatch[1];
|
|
67
|
+
return parseNearToYocto(value);
|
|
68
|
+
}
|
|
69
|
+
// Parse "X yocto" format
|
|
70
|
+
const yoctoMatch = trimmed.match(/^(\d+)\s+yocto$/i);
|
|
71
|
+
if (yoctoMatch) {
|
|
72
|
+
// Safe to use non-null assertion after match check
|
|
73
|
+
// biome-ignore lint/style/noNonNullAssertion: regex capture group guaranteed to exist when match succeeds
|
|
74
|
+
return yoctoMatch[1];
|
|
75
|
+
}
|
|
76
|
+
// Common mistake: bare number
|
|
77
|
+
if (/^[\d.]+$/.test(trimmed)) {
|
|
78
|
+
throw new Error(`Ambiguous amount: "${amount}". Did you mean "${amount} NEAR"?\n` +
|
|
79
|
+
` - Use Amount.NEAR(${amount}) for NEAR\n` +
|
|
80
|
+
` - Use Amount.yocto(${amount}n) for yoctoNEAR\n` +
|
|
81
|
+
` - Or write "${amount} NEAR" or "${amount} yocto"\n` +
|
|
82
|
+
` - Or pass ${amount}n as bigint for yoctoNEAR\n` +
|
|
83
|
+
`\n` +
|
|
84
|
+
`💡 TypeScript tip: Use 'as const' for variables:\n` +
|
|
85
|
+
` const amount = "10 NEAR" as const`);
|
|
86
|
+
}
|
|
87
|
+
// Invalid format
|
|
88
|
+
throw new Error(`Invalid amount format: "${amount}"\n` +
|
|
89
|
+
`Expected formats:\n` +
|
|
90
|
+
` - "10 NEAR" or Amount.NEAR(10)\n` +
|
|
91
|
+
` - "1000000 yocto" or Amount.yocto(1000000n)\n` +
|
|
92
|
+
` - 1000000n (bigint, treated as yoctoNEAR)`);
|
|
93
|
+
}
|
|
94
|
+
/**
|
|
95
|
+
* Format yoctoNEAR to human-readable NEAR
|
|
96
|
+
* @param yocto - Amount in yoctoNEAR
|
|
97
|
+
* @param options - Formatting options
|
|
98
|
+
* @returns Formatted string (e.g., "10.50 NEAR")
|
|
99
|
+
*/
|
|
100
|
+
export function formatAmount(yocto, options) {
|
|
101
|
+
const { precision = 2, includeSuffix = true, trimZeros = false, } = options || {};
|
|
102
|
+
const amount = typeof yocto === "string" ? BigInt(yocto) : yocto;
|
|
103
|
+
const wholePart = amount / YOCTO_PER_NEAR;
|
|
104
|
+
const fracPart = amount % YOCTO_PER_NEAR;
|
|
105
|
+
let result;
|
|
106
|
+
if (fracPart === BigInt(0)) {
|
|
107
|
+
result = wholePart.toString();
|
|
108
|
+
}
|
|
109
|
+
else {
|
|
110
|
+
const fracStr = fracPart.toString().padStart(24, "0");
|
|
111
|
+
let decimals = fracStr.substring(0, precision);
|
|
112
|
+
if (trimZeros) {
|
|
113
|
+
decimals = decimals.replace(/0+$/, "");
|
|
114
|
+
}
|
|
115
|
+
result = decimals ? `${wholePart}.${decimals}` : wholePart.toString();
|
|
116
|
+
}
|
|
117
|
+
return includeSuffix ? `${result} NEAR` : result;
|
|
118
|
+
}
|
|
119
|
+
/**
|
|
120
|
+
* Internal: Parse NEAR value to yoctoNEAR
|
|
121
|
+
* Note: The caller (parseAmount) already validates the format with the same regex,
|
|
122
|
+
* so the value is guaranteed to match [\d.]+ pattern when it reaches this function.
|
|
123
|
+
*/
|
|
124
|
+
function parseNearToYocto(value) {
|
|
125
|
+
// Split into whole and fractional parts
|
|
126
|
+
const parts = value.split(".");
|
|
127
|
+
const wholePart = parts[0] || "0";
|
|
128
|
+
const fracPart = (parts[1] || "").padEnd(24, "0").substring(0, 24);
|
|
129
|
+
// Check for negative values
|
|
130
|
+
if (wholePart.startsWith("-")) {
|
|
131
|
+
throw new Error("NEAR amount must be non-negative");
|
|
132
|
+
}
|
|
133
|
+
// Convert to yoctoNEAR
|
|
134
|
+
const yocto = BigInt(wholePart) * YOCTO_PER_NEAR + BigInt(fracPart);
|
|
135
|
+
return yocto.toString();
|
|
136
|
+
}
|
|
137
|
+
//# sourceMappingURL=amount.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"amount.js","sourceRoot":"","sources":["../../src/utils/amount.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;GAWG;AAEH,OAAO,EAAE,cAAc,EAAE,MAAM,sBAAsB,CAAA;AAuBrD;;;;;;;;;GASG;AACH,MAAM,CAAC,MAAM,MAAM,GAAG;IACpB;;;;OAIG;IACH,IAAI,CAAC,KAAsB;QACzB,OAAO,GAAG,KAAK,OAAO,CAAA;IACxB,CAAC;IAED;;;;OAIG;IACH,KAAK,CAAC,KAAsB;QAC1B,OAAO,GAAG,KAAK,QAAQ,CAAA;IACzB,CAAC;IAED;;OAEG;IACH,IAAI,EAAE,SAAS;IACf,QAAQ,EAAE,QAAQ;IAClB,SAAS,EAAE,SAAS;CACZ,CAAA;AAEV;;;;;;GAMG;AACH,MAAM,UAAU,WAAW,CAAC,MAAmB;IAC7C,gDAAgD;IAChD,IAAI,OAAO,MAAM,KAAK,QAAQ,EAAE,CAAC;QAC/B,OAAO,MAAM,CAAC,QAAQ,EAAE,CAAA;IAC1B,CAAC;IAED,MAAM,OAAO,GAAG,MAAM,CAAC,IAAI,EAAE,CAAA;IAE7B,2CAA2C;IAC3C,MAAM,SAAS,GAAG,OAAO,CAAC,KAAK,CAAC,oBAAoB,CAAC,CAAA;IACrD,IAAI,SAAS,EAAE,CAAC;QACd,mDAAmD;QACnD,0GAA0G;QAC1G,MAAM,KAAK,GAAG,SAAS,CAAC,CAAC,CAAE,CAAA;QAC3B,OAAO,gBAAgB,CAAC,KAAK,CAAC,CAAA;IAChC,CAAC;IAED,yBAAyB;IACzB,MAAM,UAAU,GAAG,OAAO,CAAC,KAAK,CAAC,kBAAkB,CAAC,CAAA;IACpD,IAAI,UAAU,EAAE,CAAC;QACf,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gas"`
|
|
6
|
+
* - `"30 Tgas"` (string literal)
|
|
7
|
+
*
|
|
8
|
+
* Raw gas units (e.g. `"30000000000000"`) are also supported for advanced use cases.
|
|
9
|
+
*/
|
|
10
|
+
/**
|
|
11
|
+
* Gas input type - string with unit specification.
|
|
12
|
+
*
|
|
13
|
+
* Accepts:
|
|
14
|
+
* - Literal strings: "30 Tgas", "30.5 Tgas"
|
|
15
|
+
* - Raw gas strings: "30000000000000"
|
|
16
|
+
* - Constructor output: Gas.Tgas(30)
|
|
17
|
+
*
|
|
18
|
+
* For variables, use 'as const' to preserve literal type:
|
|
19
|
+
* @example
|
|
20
|
+
* const gas = "30 Tgas" as const
|
|
21
|
+
*/
|
|
22
|
+
export type GasInput = `${number} Tgas` | `${number}.${number} Tgas` | `${number}` | ReturnType<typeof Gas.Tgas>;
|
|
23
|
+
/**
|
|
24
|
+
* Gas namespace - explicit constructors.
|
|
25
|
+
*
|
|
26
|
+
* @example
|
|
27
|
+
* Gas.Tgas(30) // "30 Tgas"
|
|
28
|
+
* Gas.Tgas(300) // "300 Tgas"
|
|
29
|
+
*/
|
|
30
|
+
export declare const Gas: {
|
|
31
|
+
/**
|
|
32
|
+
* Create gas amount in TGas (teragas).
|
|
33
|
+
* @param value - Amount in TGas.
|
|
34
|
+
*/
|
|
35
|
+
readonly Tgas: (value: number | string) => string;
|
|
36
|
+
/**
|
|
37
|
+
* Common gas amounts.
|
|
38
|
+
*/
|
|
39
|
+
readonly DEFAULT: "30 Tgas";
|
|
40
|
+
readonly MAX: "300 Tgas";
|
|
41
|
+
};
|
|
42
|
+
/**
|
|
43
|
+
* Parse gas string to raw gas units.
|
|
44
|
+
*
|
|
45
|
+
* @param gas - Gas with unit (e.g., `"30 Tgas"`) or raw gas number.
|
|
46
|
+
* @returns Gas in raw units as a string.
|
|
47
|
+
*/
|
|
48
|
+
export declare function parseGas(gas: GasInput | number): string;
|
|
49
|
+
/**
|
|
50
|
+
* Format gas to TGas.
|
|
51
|
+
*
|
|
52
|
+
* @param gas - Gas in raw units.
|
|
53
|
+
* @param precision - Decimal places (default: 2).
|
|
54
|
+
* @returns Formatted gas with `' Tgas'` suffix.
|
|
55
|
+
*/
|
|
56
|
+
export declare function formatGas(gas: string | bigint, precision?: number): string;
|
|
57
|
+
/**
|
|
58
|
+
* Convert TGas to raw gas units.
|
|
59
|
+
* @param tgas - Amount in TGas.
|
|
60
|
+
* @returns Gas amount as string.
|
|
61
|
+
*/
|
|
62
|
+
export declare function toGas(tgas: number): string;
|
|
63
|
+
/**
|
|
64
|
+
* Convert raw gas to TGas.
|
|
65
|
+
* @param gas - Gas amount in raw units.
|
|
66
|
+
* @returns Amount in TGas.
|
|
67
|
+
*/
|
|
68
|
+
export declare function toTGas(gas: string | bigint): number;
|
|
69
|
+
//# sourceMappingURL=gas.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"gas.d.ts","sourceRoot":"","sources":["../../src/utils/gas.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAIH;;;;;;;;;;;GAWG;AACH,MAAM,MAAM,QAAQ,GAChB,GAAG,MAAM,OAAO,GAChB,GAAG,MAAM,IAAI,MAAM,OAAO,GAC1B,GAAG,MAAM,EAAE,GACX,UAAU,CAAC,OAAO,GAAG,CAAC,IAAI,CAAC,CAAA;AAE/B;;;;;;GAMG;AACH,eAAO,MAAM,GAAG;IACd;;;OAGG;2BACS,MAAM,GAAG,MAAM,KAAG,MAAM;IAIpC;;OAEG;;;CAGK,CAAA;AAEV;;;;;GAKG;AACH,wBAAgB,QAAQ,CAAC,GAAG,EAAE,QAAQ,GAAG,MAAM,GAAG,MAAM,CA6BvD;AAED;;;;;;GAMG;AACH,wBAAgB,SAAS,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,EAAE,SAAS,SAAI,GAAG,MAAM,CAIrE;AAED;;;;GAIG;AACH,wBAAgB,KAAK,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,CAE1C;AAED;;;;GAIG;AACH,wBAAgB,MAAM,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,GAAG,MAAM,CAGnD"}
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Gas utilities for NEAR transactions.
|
|
3
|
+
*
|
|
4
|
+
* Gas amounts should specify units explicitly:
|
|
5
|
+
* - `Gas.Tgas(30)` → `"30 Tgas"`
|
|
6
|
+
* - `"30 Tgas"` (string literal)
|
|
7
|
+
*
|
|
8
|
+
* Raw gas units (e.g. `"30000000000000"`) are also supported for advanced use cases.
|
|
9
|
+
*/
|
|
10
|
+
import { GAS_PER_TGAS } from "../core/constants.js";
|
|
11
|
+
/**
|
|
12
|
+
* Gas namespace - explicit constructors.
|
|
13
|
+
*
|
|
14
|
+
* @example
|
|
15
|
+
* Gas.Tgas(30) // "30 Tgas"
|
|
16
|
+
* Gas.Tgas(300) // "300 Tgas"
|
|
17
|
+
*/
|
|
18
|
+
export const Gas = {
|
|
19
|
+
/**
|
|
20
|
+
* Create gas amount in TGas (teragas).
|
|
21
|
+
* @param value - Amount in TGas.
|
|
22
|
+
*/
|
|
23
|
+
Tgas(value) {
|
|
24
|
+
return `${value} Tgas`;
|
|
25
|
+
},
|
|
26
|
+
/**
|
|
27
|
+
* Common gas amounts.
|
|
28
|
+
*/
|
|
29
|
+
DEFAULT: "30 Tgas",
|
|
30
|
+
MAX: "300 Tgas",
|
|
31
|
+
};
|
|
32
|
+
/**
|
|
33
|
+
* Parse gas string to raw gas units.
|
|
34
|
+
*
|
|
35
|
+
* @param gas - Gas with unit (e.g., `"30 Tgas"`) or raw gas number.
|
|
36
|
+
* @returns Gas in raw units as a string.
|
|
37
|
+
*/
|
|
38
|
+
export function parseGas(gas) {
|
|
39
|
+
const gasStr = typeof gas === "number" ? gas.toString() : gas;
|
|
40
|
+
const trimmed = gasStr.trim();
|
|
41
|
+
// Parse "X Tgas" format (case insensitive)
|
|
42
|
+
const tgasMatch = trimmed.match(/^([\d.]+)\s+Tgas$/i);
|
|
43
|
+
if (tgasMatch) {
|
|
44
|
+
// Safe to use non-null assertion after match check
|
|
45
|
+
// biome-ignore lint/style/noNonNullAssertion: regex capture group guaranteed to exist when match succeeds
|
|
46
|
+
const tgas = parseFloat(tgasMatch[1]);
|
|
47
|
+
if (Number.isNaN(tgas) || tgas < 0) {
|
|
48
|
+
// biome-ignore lint/style/noNonNullAssertion: same capture group as above
|
|
49
|
+
throw new Error(`Invalid Tgas value: ${tgasMatch[1]}`);
|
|
50
|
+
}
|
|
51
|
+
return BigInt(Math.floor(tgas * 1e12)).toString();
|
|
52
|
+
}
|
|
53
|
+
// Raw number (no unit) - assume it's already in gas units
|
|
54
|
+
// This allows power users to specify exact gas amounts
|
|
55
|
+
if (/^\d+$/.test(trimmed)) {
|
|
56
|
+
return trimmed;
|
|
57
|
+
}
|
|
58
|
+
// Invalid format
|
|
59
|
+
throw new Error(`Invalid gas format: "${gas}"\n` +
|
|
60
|
+
`Expected: "30 Tgas" or Gas.Tgas(30)\n` +
|
|
61
|
+
`Or provide raw gas units as a number string`);
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* Format gas to TGas.
|
|
65
|
+
*
|
|
66
|
+
* @param gas - Gas in raw units.
|
|
67
|
+
* @param precision - Decimal places (default: 2).
|
|
68
|
+
* @returns Formatted gas with `' Tgas'` suffix.
|
|
69
|
+
*/
|
|
70
|
+
export function formatGas(gas, precision = 2) {
|
|
71
|
+
const amount = typeof gas === "string" ? BigInt(gas) : gas;
|
|
72
|
+
const tgas = Number(amount) / Number(GAS_PER_TGAS);
|
|
73
|
+
return `${tgas.toFixed(precision)} Tgas`;
|
|
74
|
+
}
|
|
75
|
+
/**
|
|
76
|
+
* Convert TGas to raw gas units.
|
|
77
|
+
* @param tgas - Amount in TGas.
|
|
78
|
+
* @returns Gas amount as string.
|
|
79
|
+
*/
|
|
80
|
+
export function toGas(tgas) {
|
|
81
|
+
return (BigInt(Math.floor(tgas * 1e12)) * BigInt(1)).toString();
|
|
82
|
+
}
|
|
83
|
+
/**
|
|
84
|
+
* Convert raw gas to TGas.
|
|
85
|
+
* @param gas - Gas amount in raw units.
|
|
86
|
+
* @returns Amount in TGas.
|
|
87
|
+
*/
|
|
88
|
+
export function toTGas(gas) {
|
|
89
|
+
const amount = typeof gas === "string" ? BigInt(gas) : gas;
|
|
90
|
+
return Number(amount) / Number(GAS_PER_TGAS);
|
|
91
|
+
}
|
|
92
|
+
//# sourceMappingURL=gas.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"gas.js","sourceRoot":"","sources":["../../src/utils/gas.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAEH,OAAO,EAAE,YAAY,EAAE,MAAM,sBAAsB,CAAA;AAoBnD;;;;;;GAMG;AACH,MAAM,CAAC,MAAM,GAAG,GAAG;IACjB;;;OAGG;IACH,IAAI,CAAC,KAAsB;QACzB,OAAO,GAAG,KAAK,OAAO,CAAA;IACxB,CAAC;IAED;;OAEG;IACH,OAAO,EAAE,SAAS;IAClB,GAAG,EAAE,UAAU;CACP,CAAA;AAEV;;;;;GAKG;AACH,MAAM,UAAU,QAAQ,CAAC,GAAsB;IAC7C,MAAM,MAAM,GAAG,OAAO,GAAG,KAAK,QAAQ,CAAC,CAAC,CAAC,GAAG,CAAC,QAAQ,EAAE,CAAC,CAAC,CAAC,GAAG,CAAA;IAC7D,MAAM,OAAO,GAAG,MAAM,CAAC,IAAI,EAAE,CAAA;IAE7B,2CAA2C;IAC3C,MAAM,SAAS,GAAG,OAAO,CAAC,KAAK,CAAC,oBAAoB,CAAC,CAAA;IACrD,IAAI,SAAS,EAAE,CAAC;QACd,mDAAmD;QACnD,0GAA0G;QAC1G,MAAM,IAAI,GAAG,UAAU,CAAC,SAAS,CAAC,CAAC,CAAE,CAAC,CAAA;QACtC,IAAI,MAAM,CAAC,KAAK,CAAC,IAAI,CAAC,IAAI,IAAI,GAAG,CAAC,EAAE,CAAC;YACnC,0EAA0E;YAC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,MAAM,CAAC,YAAY,CAAC,CAAA;AAC9C,CAAC"}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Utility functions for the NEAR client library.
|
|
3
|
+
*
|
|
4
|
+
* @remarks
|
|
5
|
+
* This module exposes helpers for working with human-readable amounts and gas,
|
|
6
|
+
* key generation and parsing, NEP-413 message signing, and validation of
|
|
7
|
+
* common NEAR types (account IDs, public/private keys, amounts, gas).
|
|
8
|
+
*/
|
|
9
|
+
export { Amount, type AmountInput, formatAmount, parseAmount, } from "./amount.js";
|
|
10
|
+
export { formatGas, Gas, type GasInput, parseGas } from "./gas.js";
|
|
11
|
+
export * from "./key.js";
|
|
12
|
+
export { generateNep413Nonce, NEP413_TAG, serializeNep413Message, verifyNep413Signature, } from "./nep413.js";
|
|
13
|
+
export { isPrivateKey, isValidAccountId, isValidPublicKey, type PrivateKey, validateAccountId, validatePrivateKey, validatePublicKey, } from "./validation.js";
|
|
14
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/utils/index.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AACH,OAAO,EACL,MAAM,EACN,KAAK,WAAW,EAChB,YAAY,EACZ,WAAW,GACZ,MAAM,aAAa,CAAA;AACpB,OAAO,EAAE,SAAS,EAAE,GAAG,EAAE,KAAK,QAAQ,EAAE,QAAQ,EAAE,MAAM,UAAU,CAAA;AAClE,cAAc,UAAU,CAAA;AACxB,OAAO,EACL,mBAAmB,EACnB,UAAU,EACV,sBAAsB,EACtB,qBAAqB,GACtB,MAAM,aAAa,CAAA;AACpB,OAAO,EACL,YAAY,EACZ,gBAAgB,EAChB,gBAAgB,EAChB,KAAK,UAAU,EACf,iBAAiB,EACjB,kBAAkB,EAClB,iBAAiB,GAClB,MAAM,iBAAiB,CAAA"}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Utility functions for the NEAR client library.
|
|
3
|
+
*
|
|
4
|
+
* @remarks
|
|
5
|
+
* This module exposes helpers for working with human-readable amounts and gas,
|
|
6
|
+
* key generation and parsing, NEP-413 message signing, and validation of
|
|
7
|
+
* common NEAR types (account IDs, public/private keys, amounts, gas).
|
|
8
|
+
*/
|
|
9
|
+
export { Amount, formatAmount, parseAmount, } from "./amount.js";
|
|
10
|
+
export { formatGas, Gas, parseGas } from "./gas.js";
|
|
11
|
+
export * from "./key.js";
|
|
12
|
+
export { generateNep413Nonce, NEP413_TAG, serializeNep413Message, verifyNep413Signature, } from "./nep413.js";
|
|
13
|
+
export { isPrivateKey, isValidAccountId, isValidPublicKey, validateAccountId, validatePrivateKey, validatePublicKey, } from "./validation.js";
|
|
14
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/utils/index.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AACH,OAAO,EACL,MAAM,EAEN,YAAY,EACZ,WAAW,GACZ,MAAM,aAAa,CAAA;AACpB,OAAO,EAAE,SAAS,EAAE,GAAG,EAAiB,QAAQ,EAAE,MAAM,UAAU,CAAA;AAClE,cAAc,UAAU,CAAA;AACxB,OAAO,EACL,mBAAmB,EACnB,UAAU,EACV,sBAAsB,EACtB,qBAAqB,GACtB,MAAM,aAAa,CAAA;AACpB,OAAO,EACL,YAAY,EACZ,gBAAgB,EAChB,gBAAgB,EAEhB,iBAAiB,EACjB,kBAAkB,EAClB,iBAAiB,GAClB,MAAM,iBAAiB,CAAA"}
|
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
import { type KeyPair, type PublicKey, type Signature, type SignedMessage, type SignMessageParams } from "../core/types.js";
|
|
2
|
+
/**
|
|
3
|
+
* Ed25519 key pair implementation.
|
|
4
|
+
*
|
|
5
|
+
* @remarks
|
|
6
|
+
* Implements the {@link KeyPair} interface used throughout the library and
|
|
7
|
+
* provides NEP-413 message signing via {@link Ed25519KeyPair.signNep413Message}.
|
|
8
|
+
*/
|
|
9
|
+
export declare class Ed25519KeyPair implements KeyPair {
|
|
10
|
+
publicKey: PublicKey;
|
|
11
|
+
secretKey: string;
|
|
12
|
+
private privateKey;
|
|
13
|
+
constructor(secretKey: Uint8Array);
|
|
14
|
+
sign(message: Uint8Array): Signature;
|
|
15
|
+
/**
|
|
16
|
+
* Sign a message according to NEP-413 specification
|
|
17
|
+
*
|
|
18
|
+
* NEP-413 enables off-chain message signing for authentication and ownership verification.
|
|
19
|
+
* The message is signed with a full-access key but does not require gas or blockchain state.
|
|
20
|
+
*
|
|
21
|
+
* @param accountId - The NEAR account ID that owns this key
|
|
22
|
+
* @param params - Message signing parameters (message, recipient, nonce)
|
|
23
|
+
* @returns Signed message with account ID, public key, and base64-encoded signature
|
|
24
|
+
*
|
|
25
|
+
* @see https://github.com/near/NEPs/blob/master/neps/nep-0413.md
|
|
26
|
+
*
|
|
27
|
+
* @example
|
|
28
|
+
* ```typescript
|
|
29
|
+
* const nonce = crypto.getRandomValues(new Uint8Array(32))
|
|
30
|
+
* const signedMessage = keyPair.signNep413Message("alice.near", {
|
|
31
|
+
* message: "Login to MyApp",
|
|
32
|
+
* recipient: "myapp.near",
|
|
33
|
+
* nonce,
|
|
34
|
+
* })
|
|
35
|
+
* console.log(signedMessage.signature) // Base64-encoded signature
|
|
36
|
+
* ```
|
|
37
|
+
*/
|
|
38
|
+
signNep413Message(accountId: string, params: SignMessageParams): SignedMessage;
|
|
39
|
+
static fromRandom(): Ed25519KeyPair;
|
|
40
|
+
static fromString(keyString: string): Ed25519KeyPair;
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* Secp256k1 key pair implementation.
|
|
44
|
+
*
|
|
45
|
+
* NEAR expects secp256k1 public keys to be 64 bytes (uncompressed without 0x04 header).
|
|
46
|
+
* The secp256k1 library returns 65-byte uncompressed keys (with 0x04 header), so we
|
|
47
|
+
* manually remove/add that byte as needed.
|
|
48
|
+
*
|
|
49
|
+
* Signatures are 65 bytes: 64-byte signature + 1-byte recovery ID.
|
|
50
|
+
*/
|
|
51
|
+
export declare class Secp256k1KeyPair implements KeyPair {
|
|
52
|
+
publicKey: PublicKey;
|
|
53
|
+
secretKey: string;
|
|
54
|
+
private privateKey;
|
|
55
|
+
constructor(secretKey: Uint8Array);
|
|
56
|
+
sign(message: Uint8Array): Signature;
|
|
57
|
+
/**
|
|
58
|
+
* Sign a message according to NEP-413 specification
|
|
59
|
+
*
|
|
60
|
+
* NEP-413 enables off-chain message signing for authentication and ownership verification.
|
|
61
|
+
* The message is signed with a full-access key but does not require gas or blockchain state.
|
|
62
|
+
*
|
|
63
|
+
* @param accountId - The NEAR account ID that owns this key
|
|
64
|
+
* @param params - Message signing parameters (message, recipient, nonce)
|
|
65
|
+
* @returns Signed message with account ID, public key, and base64-encoded signature
|
|
66
|
+
*
|
|
67
|
+
* @see https://github.com/near/NEPs/blob/master/neps/nep-0413.md
|
|
68
|
+
*
|
|
69
|
+
* @example
|
|
70
|
+
* ```typescript
|
|
71
|
+
* const nonce = crypto.getRandomValues(new Uint8Array(32))
|
|
72
|
+
* const signedMessage = keyPair.signNep413Message("alice.near", {
|
|
73
|
+
* message: "Login to MyApp",
|
|
74
|
+
* recipient: "myapp.near",
|
|
75
|
+
* nonce,
|
|
76
|
+
* })
|
|
77
|
+
* console.log(signedMessage.signature) // Base64-encoded signature
|
|
78
|
+
* ```
|
|
79
|
+
*/
|
|
80
|
+
signNep413Message(accountId: string, params: SignMessageParams): SignedMessage;
|
|
81
|
+
static fromRandom(): Secp256k1KeyPair;
|
|
82
|
+
static fromString(keyString: string): Secp256k1KeyPair;
|
|
83
|
+
}
|
|
84
|
+
/**
|
|
85
|
+
* Generate a new random Ed25519 key pair.
|
|
86
|
+
* @returns A new {@link KeyPair} instance.
|
|
87
|
+
*/
|
|
88
|
+
export declare function generateKey(): KeyPair;
|
|
89
|
+
/**
|
|
90
|
+
* Parse a key string to a {@link KeyPair}.
|
|
91
|
+
*
|
|
92
|
+
* @param keyString - Key string (e.g. `"ed25519:..."` or `"secp256k1:..."`).
|
|
93
|
+
* @returns A concrete {@link Ed25519KeyPair} or {@link Secp256k1KeyPair}.
|
|
94
|
+
*/
|
|
95
|
+
export declare function parseKey(keyString: string): KeyPair;
|
|
96
|
+
/**
|
|
97
|
+
* Parse a public key string to a {@link PublicKey} object.
|
|
98
|
+
*
|
|
99
|
+
* @param publicKeyString - Public key string (e.g. `"ed25519:..."` or `"secp256k1:..."`).
|
|
100
|
+
* @returns {@link PublicKey} instance.
|
|
101
|
+
*/
|
|
102
|
+
export declare function parsePublicKey(publicKeyString: string): PublicKey;
|
|
103
|
+
/**
|
|
104
|
+
* Generate a BIP39 seed phrase (12 words by default)
|
|
105
|
+
* Uses proper BIP39 implementation with cryptographically secure randomness
|
|
106
|
+
* @param wordCount - Number of words (12, 15, 18, 21, or 24). Defaults to 12
|
|
107
|
+
* @returns A BIP39 seed phrase string
|
|
108
|
+
*/
|
|
109
|
+
export declare function generateSeedPhrase(wordCount?: 12 | 15 | 18 | 21 | 24): string;
|
|
110
|
+
/**
|
|
111
|
+
* Parse a BIP39 seed phrase to derive a key pair using proper BIP32/SLIP10 derivation
|
|
112
|
+
* @param phrase - BIP39 seed phrase (12-24 words)
|
|
113
|
+
* @param path - BIP32 derivation path (defaults to "m/44'/397'/0'" for NEAR)
|
|
114
|
+
* @returns KeyPair instance
|
|
115
|
+
*/
|
|
116
|
+
export declare function parseSeedPhrase(phrase: string, path?: string): KeyPair;
|
|
117
|
+
//# sourceMappingURL=key.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"key.d.ts","sourceRoot":"","sources":["../../src/utils/key.ts"],"names":[],"mappings":"AAOA,OAAO,EACL,KAAK,OAAO,EAEZ,KAAK,SAAS,EACd,KAAK,SAAS,EACd,KAAK,aAAa,EAClB,KAAK,iBAAiB,EACvB,MAAM,kBAAkB,CAAA;AAIzB;;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