@fastnear/api 0.4.0 → 0.5.1
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/index.cjs +40 -0
- package/dist/cjs/index.cjs.map +1 -0
- package/dist/cjs/index.d.cts +3339 -0
- package/dist/esm/index.d.ts +3339 -0
- package/dist/esm/index.js +9 -60
- package/dist/esm/index.js.map +1 -7
- package/dist/umd/{index.js → browser.global.js} +272 -120
- package/dist/umd/browser.global.js.map +1 -0
- package/package.json +9 -8
- package/dist/cjs/cryptoUtils.js +0 -89
- package/dist/cjs/cryptoUtils.js.map +0 -7
- package/dist/cjs/index.esm.js +0 -1219
- package/dist/cjs/index.esm.js.map +0 -7
- package/dist/cjs/index.js +0 -1274
- package/dist/cjs/index.js.map +0 -7
- package/dist/cjs/near.js +0 -1219
- package/dist/cjs/near.js.map +0 -7
- package/dist/cjs/transaction.js +0 -360
- package/dist/cjs/transaction.js.map +0 -7
- package/dist/cjs/utils.js +0 -105
- package/dist/cjs/utils.js.map +0 -7
- package/dist/esm/chunk-2SCAGR3F.js +0 -68
- package/dist/esm/chunk-2SCAGR3F.js.map +0 -7
- package/dist/esm/chunk-OR3WITSY.js +0 -842
- package/dist/esm/chunk-OR3WITSY.js.map +0 -7
- package/dist/esm/chunk-S5Q2EM2B.js +0 -48
- package/dist/esm/chunk-S5Q2EM2B.js.map +0 -7
- package/dist/esm/chunk-YKPILPMX.js +0 -301
- package/dist/esm/chunk-YKPILPMX.js.map +0 -7
- package/dist/esm/cryptoUtils.js +0 -21
- package/dist/esm/cryptoUtils.js.map +0 -7
- package/dist/esm/index.esm.js +0 -13
- package/dist/esm/index.esm.js.map +0 -7
- package/dist/esm/near.js +0 -15
- package/dist/esm/near.js.map +0 -7
- package/dist/esm/transaction.js +0 -16
- package/dist/esm/transaction.js.map +0 -7
- package/dist/esm/utils.js +0 -24
- package/dist/esm/utils.js.map +0 -7
- package/dist/umd/index.js.map +0 -7
package/dist/cjs/index.js
DELETED
|
@@ -1,1274 +0,0 @@
|
|
|
1
|
-
/* ⋈ 🏃🏻💨 FastNEAR API - https://github.com/fastnear */
|
|
2
|
-
var __create = Object.create;
|
|
3
|
-
var __defProp = Object.defineProperty;
|
|
4
|
-
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
-
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
-
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
-
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
-
var __export = (target, all) => {
|
|
9
|
-
for (var name in all)
|
|
10
|
-
__defProp(target, name, { get: all[name], enumerable: true });
|
|
11
|
-
};
|
|
12
|
-
var __copyProps = (to, from, except, desc) => {
|
|
13
|
-
if (from && typeof from === "object" || typeof from === "function") {
|
|
14
|
-
for (let key of __getOwnPropNames(from))
|
|
15
|
-
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
16
|
-
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
17
|
-
}
|
|
18
|
-
return to;
|
|
19
|
-
};
|
|
20
|
-
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
-
// If the importer is in node compatibility mode or this is not an ESM
|
|
22
|
-
// file that has been converted to a CommonJS file using a Babel-
|
|
23
|
-
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
24
|
-
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
25
|
-
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
26
|
-
mod
|
|
27
|
-
));
|
|
28
|
-
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
29
|
-
|
|
30
|
-
// src/index.ts
|
|
31
|
-
var src_exports = {};
|
|
32
|
-
__export(src_exports, {
|
|
33
|
-
SCHEMA: () => SCHEMA,
|
|
34
|
-
api: () => api,
|
|
35
|
-
canSignWithLAK: () => canSignWithLAK,
|
|
36
|
-
convertUnit: () => convertUnit,
|
|
37
|
-
deepCopy: () => deepCopy,
|
|
38
|
-
fromBase58: () => import_base58_js.base58_to_binary,
|
|
39
|
-
fromBase64: () => fromBase64,
|
|
40
|
-
keyFromString: () => keyFromString,
|
|
41
|
-
keyToString: () => keyToString,
|
|
42
|
-
lsGet: () => lsGet,
|
|
43
|
-
lsSet: () => lsSet,
|
|
44
|
-
mapAction: () => mapAction,
|
|
45
|
-
near: () => api,
|
|
46
|
-
parseJsonFromBytes: () => parseJsonFromBytes,
|
|
47
|
-
privateKeyFromRandom: () => privateKeyFromRandom,
|
|
48
|
-
publicKeyFromPrivate: () => publicKeyFromPrivate,
|
|
49
|
-
serializeSignedTransaction: () => serializeSignedTransaction,
|
|
50
|
-
serializeTransaction: () => serializeTransaction,
|
|
51
|
-
sha256: () => import_sha2.sha256,
|
|
52
|
-
signBytes: () => signBytes,
|
|
53
|
-
signHash: () => signHash,
|
|
54
|
-
toBase58: () => import_base58_js.binary_to_base58,
|
|
55
|
-
toBase64: () => toBase64,
|
|
56
|
-
tryParseJson: () => tryParseJson
|
|
57
|
-
});
|
|
58
|
-
module.exports = __toCommonJS(src_exports);
|
|
59
|
-
|
|
60
|
-
// src/near.ts
|
|
61
|
-
var import_big2 = __toESM(require("big.js"), 1);
|
|
62
|
-
|
|
63
|
-
// ../wallet-adapter/src/index.ts
|
|
64
|
-
var WalletAdapter = class _WalletAdapter {
|
|
65
|
-
/** @type {HTMLIFrameElement} */
|
|
66
|
-
#iframe = null;
|
|
67
|
-
/** @type {string} */
|
|
68
|
-
#targetOrigin;
|
|
69
|
-
/** @type {string} */
|
|
70
|
-
#widgetUrl;
|
|
71
|
-
/** @type {Map<string, Function>} */
|
|
72
|
-
#pending = /* @__PURE__ */ new Map();
|
|
73
|
-
/** @type {WalletState} */
|
|
74
|
-
#state;
|
|
75
|
-
/** @type {Function} */
|
|
76
|
-
#onStateUpdate;
|
|
77
|
-
/** @type {string} */
|
|
78
|
-
#callbackUrl;
|
|
79
|
-
/** @type {string} */
|
|
80
|
-
static defaultWidgetUrl = "https://wallet-adapter.fastnear.com";
|
|
81
|
-
/**
|
|
82
|
-
* @param {WalletAdapterConfig} [config]
|
|
83
|
-
*/
|
|
84
|
-
constructor({
|
|
85
|
-
widgetUrl = _WalletAdapter.defaultWidgetUrl,
|
|
86
|
-
targetOrigin = "*",
|
|
87
|
-
onStateUpdate,
|
|
88
|
-
lastState,
|
|
89
|
-
callbackUrl = window.location.href
|
|
90
|
-
} = {}) {
|
|
91
|
-
this.#targetOrigin = targetOrigin;
|
|
92
|
-
this.#widgetUrl = widgetUrl;
|
|
93
|
-
this.#onStateUpdate = onStateUpdate;
|
|
94
|
-
this.#callbackUrl = callbackUrl;
|
|
95
|
-
this.#state = lastState || {};
|
|
96
|
-
window.addEventListener("message", this.#handleMessage.bind(this));
|
|
97
|
-
}
|
|
98
|
-
/**
|
|
99
|
-
* Creates an iframe for wallet interaction
|
|
100
|
-
* @param {string} path - Path to load in iframe
|
|
101
|
-
* @returns {HTMLIFrameElement}
|
|
102
|
-
*/
|
|
103
|
-
#createIframe(path) {
|
|
104
|
-
if (this.#iframe) {
|
|
105
|
-
this.#iframe.remove();
|
|
106
|
-
}
|
|
107
|
-
const url = new URL(path, this.#widgetUrl);
|
|
108
|
-
const iframe = document.createElement("iframe");
|
|
109
|
-
iframe.src = url.toString();
|
|
110
|
-
iframe.allow = "usb";
|
|
111
|
-
iframe.style.border = "none";
|
|
112
|
-
iframe.style.zIndex = "10000";
|
|
113
|
-
iframe.style.position = "fixed";
|
|
114
|
-
iframe.style.display = "block";
|
|
115
|
-
iframe.style.top = "0";
|
|
116
|
-
iframe.style.left = "0";
|
|
117
|
-
iframe.style.width = "100%";
|
|
118
|
-
iframe.style.height = "100%";
|
|
119
|
-
document.body.appendChild(iframe);
|
|
120
|
-
this.#iframe = iframe;
|
|
121
|
-
return iframe;
|
|
122
|
-
}
|
|
123
|
-
/**
|
|
124
|
-
* Handles messages from the wallet widget
|
|
125
|
-
* @param {MessageEvent} event
|
|
126
|
-
*/
|
|
127
|
-
#handleMessage(event) {
|
|
128
|
-
if (this.#targetOrigin !== "*" && event.origin !== this.#targetOrigin) {
|
|
129
|
-
return;
|
|
130
|
-
}
|
|
131
|
-
const { id, type, action, payload } = event.data;
|
|
132
|
-
if (type !== "wallet-adapter") return;
|
|
133
|
-
if (action === "close") {
|
|
134
|
-
this.#iframe?.remove();
|
|
135
|
-
this.#iframe = null;
|
|
136
|
-
return;
|
|
137
|
-
}
|
|
138
|
-
if (payload?.state) {
|
|
139
|
-
this.#state = { ...this.#state, ...payload.state };
|
|
140
|
-
this.#onStateUpdate?.(this.#state);
|
|
141
|
-
}
|
|
142
|
-
const resolve = this.#pending.get(id);
|
|
143
|
-
if (resolve) {
|
|
144
|
-
this.#pending.delete(id);
|
|
145
|
-
this.#iframe?.remove();
|
|
146
|
-
this.#iframe = null;
|
|
147
|
-
resolve(payload);
|
|
148
|
-
}
|
|
149
|
-
}
|
|
150
|
-
/**
|
|
151
|
-
* Sends a message to the wallet widget
|
|
152
|
-
* @param {string} path - Path to load in iframe
|
|
153
|
-
* @param {string} method - Method to call
|
|
154
|
-
* @param {Object} params - Parameters to pass
|
|
155
|
-
* @returns {Promise<any>}
|
|
156
|
-
*/
|
|
157
|
-
async #sendMessage(path, method, params) {
|
|
158
|
-
return new Promise((resolve) => {
|
|
159
|
-
const id = Math.random().toString(36).slice(2);
|
|
160
|
-
this.#pending.set(id, resolve);
|
|
161
|
-
const iframe = this.#createIframe(path);
|
|
162
|
-
iframe.onload = () => {
|
|
163
|
-
iframe.contentWindow?.postMessage(
|
|
164
|
-
{
|
|
165
|
-
type: "wallet-adapter",
|
|
166
|
-
method,
|
|
167
|
-
params: {
|
|
168
|
-
id,
|
|
169
|
-
...params,
|
|
170
|
-
state: this.#state,
|
|
171
|
-
callbackUrl: params.callbackUrl || this.#callbackUrl
|
|
172
|
-
}
|
|
173
|
-
},
|
|
174
|
-
this.#targetOrigin
|
|
175
|
-
);
|
|
176
|
-
};
|
|
177
|
-
});
|
|
178
|
-
}
|
|
179
|
-
/**
|
|
180
|
-
* Get current wallet state
|
|
181
|
-
* @returns {WalletState}
|
|
182
|
-
*/
|
|
183
|
-
getState() {
|
|
184
|
-
return { ...this.#state };
|
|
185
|
-
}
|
|
186
|
-
/**
|
|
187
|
-
* Set current wallet state
|
|
188
|
-
* @param state
|
|
189
|
-
*/
|
|
190
|
-
setState(state) {
|
|
191
|
-
this.#state = state;
|
|
192
|
-
}
|
|
193
|
-
/**
|
|
194
|
-
* Sign in with a NEAR wallet
|
|
195
|
-
* @param {SignInConfig} config
|
|
196
|
-
* @returns {Promise<SignInResult>}
|
|
197
|
-
*/
|
|
198
|
-
async signIn(config) {
|
|
199
|
-
return this.#sendMessage("/login.html", "signIn", config);
|
|
200
|
-
}
|
|
201
|
-
/**
|
|
202
|
-
* Send a transaction using connected wallet
|
|
203
|
-
* @param {TransactionConfig} config
|
|
204
|
-
* @returns {Promise<TransactionResult>}
|
|
205
|
-
*/
|
|
206
|
-
async sendTransactions(config) {
|
|
207
|
-
return this.#sendMessage("/send.html", "sendTransactions", config);
|
|
208
|
-
}
|
|
209
|
-
/**
|
|
210
|
-
* Clean up adapter resources
|
|
211
|
-
*/
|
|
212
|
-
destroy() {
|
|
213
|
-
window.removeEventListener("message", this.#handleMessage);
|
|
214
|
-
this.#iframe?.remove();
|
|
215
|
-
this.#iframe = null;
|
|
216
|
-
}
|
|
217
|
-
};
|
|
218
|
-
|
|
219
|
-
// src/cryptoUtils.ts
|
|
220
|
-
var import_ed25519 = require("@noble/curves/ed25519");
|
|
221
|
-
var import_sha2 = require("@noble/hashes/sha2");
|
|
222
|
-
|
|
223
|
-
// src/utils.ts
|
|
224
|
-
var import_base58_js = require("base58-js");
|
|
225
|
-
var import_big = __toESM(require("big.js"), 1);
|
|
226
|
-
var import_js_base64 = require("js-base64");
|
|
227
|
-
var LsPrefix = "__fastnear_";
|
|
228
|
-
function toBase64(data) {
|
|
229
|
-
if (typeof data === "string") {
|
|
230
|
-
return (0, import_js_base64.encode)(data);
|
|
231
|
-
} else {
|
|
232
|
-
const bytes = data instanceof Uint8Array ? data : new Uint8Array(data);
|
|
233
|
-
const str = String.fromCharCode(...bytes);
|
|
234
|
-
return (0, import_js_base64.encode)(str);
|
|
235
|
-
}
|
|
236
|
-
}
|
|
237
|
-
function fromBase64(str) {
|
|
238
|
-
const binaryString = (0, import_js_base64.decode)(str);
|
|
239
|
-
const len = binaryString.length;
|
|
240
|
-
const bytes = new Uint8Array(len);
|
|
241
|
-
for (let i = 0; i < len; i++) {
|
|
242
|
-
bytes[i] = binaryString.charCodeAt(i);
|
|
243
|
-
}
|
|
244
|
-
return bytes;
|
|
245
|
-
}
|
|
246
|
-
function lsSet(key, value) {
|
|
247
|
-
if (value === null || value === void 0) {
|
|
248
|
-
localStorage.removeItem(LsPrefix + key);
|
|
249
|
-
} else {
|
|
250
|
-
localStorage.setItem(LsPrefix + key, JSON.stringify(value));
|
|
251
|
-
}
|
|
252
|
-
}
|
|
253
|
-
function lsGet(key) {
|
|
254
|
-
const value = localStorage.getItem(LsPrefix + key);
|
|
255
|
-
return tryParseJson(value, null);
|
|
256
|
-
}
|
|
257
|
-
function deepCopy(obj) {
|
|
258
|
-
return JSON.parse(JSON.stringify(obj));
|
|
259
|
-
}
|
|
260
|
-
function tryParseJson(...args) {
|
|
261
|
-
try {
|
|
262
|
-
return JSON.parse(args[0]);
|
|
263
|
-
} catch {
|
|
264
|
-
if (args.length > 1) {
|
|
265
|
-
return args[1];
|
|
266
|
-
}
|
|
267
|
-
return args[0];
|
|
268
|
-
}
|
|
269
|
-
}
|
|
270
|
-
function canSignWithLAK(actions) {
|
|
271
|
-
return actions.length === 1 && actions[0].type === "FunctionCall" && (0, import_big.default)(actions[0]?.deposit ?? "0").eq(0);
|
|
272
|
-
}
|
|
273
|
-
|
|
274
|
-
// src/cryptoUtils.ts
|
|
275
|
-
var keyFromString = (key) => (0, import_base58_js.base58_to_binary)(
|
|
276
|
-
key.includes(":") ? (() => {
|
|
277
|
-
const [curve, keyPart] = key.split(":");
|
|
278
|
-
if (curve !== "ed25519") {
|
|
279
|
-
throw new Error(`Unsupported curve: ${curve}`);
|
|
280
|
-
}
|
|
281
|
-
return keyPart;
|
|
282
|
-
})() : key
|
|
283
|
-
);
|
|
284
|
-
var keyToString = (key) => `ed25519:${(0, import_base58_js.binary_to_base58)(key)}`;
|
|
285
|
-
function publicKeyFromPrivate(privateKey) {
|
|
286
|
-
privateKey = keyFromString(privateKey).slice(0, 32);
|
|
287
|
-
const publicKey = import_ed25519.ed25519.getPublicKey(privateKey);
|
|
288
|
-
return keyToString(publicKey);
|
|
289
|
-
}
|
|
290
|
-
function privateKeyFromRandom() {
|
|
291
|
-
const privateKey = crypto.getRandomValues(new Uint8Array(64));
|
|
292
|
-
return keyToString(privateKey);
|
|
293
|
-
}
|
|
294
|
-
function signHash(hash, privateKey) {
|
|
295
|
-
privateKey = keyFromString(privateKey).slice(0, 32);
|
|
296
|
-
const signature = import_ed25519.ed25519.sign((0, import_base58_js.base58_to_binary)(hash), privateKey);
|
|
297
|
-
return (0, import_base58_js.binary_to_base58)(signature);
|
|
298
|
-
}
|
|
299
|
-
function signBytes(bytes, privateKey) {
|
|
300
|
-
const hash = (0, import_sha2.sha256)(bytes);
|
|
301
|
-
return signHash((0, import_base58_js.binary_to_base58)(hash), privateKey);
|
|
302
|
-
}
|
|
303
|
-
|
|
304
|
-
// src/transaction.ts
|
|
305
|
-
var import_borsh = require("borsh");
|
|
306
|
-
function mapTransaction(jsonTransaction) {
|
|
307
|
-
return {
|
|
308
|
-
signerId: jsonTransaction.signerId,
|
|
309
|
-
publicKey: {
|
|
310
|
-
ed25519Key: {
|
|
311
|
-
data: keyFromString(jsonTransaction.publicKey)
|
|
312
|
-
}
|
|
313
|
-
},
|
|
314
|
-
nonce: BigInt(jsonTransaction.nonce),
|
|
315
|
-
receiverId: jsonTransaction.receiverId,
|
|
316
|
-
blockHash: (0, import_base58_js.base58_to_binary)(jsonTransaction.blockHash),
|
|
317
|
-
actions: jsonTransaction.actions.map(mapAction)
|
|
318
|
-
};
|
|
319
|
-
}
|
|
320
|
-
function serializeTransaction(jsonTransaction) {
|
|
321
|
-
const transaction = mapTransaction(jsonTransaction);
|
|
322
|
-
return (0, import_borsh.serialize)(SCHEMA.Transaction, transaction);
|
|
323
|
-
}
|
|
324
|
-
function serializeSignedTransaction(jsonTransaction, signature) {
|
|
325
|
-
const signedTransaction = {
|
|
326
|
-
transaction: mapTransaction(jsonTransaction),
|
|
327
|
-
signature: {
|
|
328
|
-
ed25519Signature: {
|
|
329
|
-
data: (0, import_base58_js.base58_to_binary)(signature)
|
|
330
|
-
}
|
|
331
|
-
}
|
|
332
|
-
};
|
|
333
|
-
return (0, import_borsh.serialize)(SCHEMA.SignedTransaction, signedTransaction);
|
|
334
|
-
}
|
|
335
|
-
function mapAction(action) {
|
|
336
|
-
switch (action.type) {
|
|
337
|
-
case "CreateAccount": {
|
|
338
|
-
return {
|
|
339
|
-
createAccount: {}
|
|
340
|
-
};
|
|
341
|
-
}
|
|
342
|
-
case "DeployContract": {
|
|
343
|
-
return {
|
|
344
|
-
deployContract: {
|
|
345
|
-
code: fromBase64(action.codeBase64)
|
|
346
|
-
}
|
|
347
|
-
};
|
|
348
|
-
}
|
|
349
|
-
case "FunctionCall": {
|
|
350
|
-
return {
|
|
351
|
-
functionCall: {
|
|
352
|
-
methodName: action.methodName,
|
|
353
|
-
args: action.argsBase64 ? fromBase64(action.argsBase64) : new TextEncoder().encode(JSON.stringify(action.args)),
|
|
354
|
-
gas: BigInt(action.gas),
|
|
355
|
-
deposit: BigInt(action.deposit)
|
|
356
|
-
}
|
|
357
|
-
};
|
|
358
|
-
}
|
|
359
|
-
case "Transfer": {
|
|
360
|
-
return {
|
|
361
|
-
transfer: {
|
|
362
|
-
deposit: BigInt(action.deposit)
|
|
363
|
-
}
|
|
364
|
-
};
|
|
365
|
-
}
|
|
366
|
-
case "Stake": {
|
|
367
|
-
return {
|
|
368
|
-
stake: {
|
|
369
|
-
stake: BigInt(action.stake),
|
|
370
|
-
publicKey: {
|
|
371
|
-
ed25519Key: {
|
|
372
|
-
data: keyFromString(action.publicKey)
|
|
373
|
-
}
|
|
374
|
-
}
|
|
375
|
-
}
|
|
376
|
-
};
|
|
377
|
-
}
|
|
378
|
-
case "AddKey": {
|
|
379
|
-
return {
|
|
380
|
-
addKey: {
|
|
381
|
-
publicKey: {
|
|
382
|
-
ed25519Key: {
|
|
383
|
-
data: keyFromString(action.publicKey)
|
|
384
|
-
}
|
|
385
|
-
},
|
|
386
|
-
accessKey: {
|
|
387
|
-
nonce: BigInt(action.accessKey.nonce),
|
|
388
|
-
permission: action.accessKey.permission === "FullAccess" ? { fullAccess: {} } : {
|
|
389
|
-
functionCall: {
|
|
390
|
-
allowance: action.accessKey.allowance ? BigInt(action.accessKey.allowance) : null,
|
|
391
|
-
receiverId: action.accessKey.receiverId,
|
|
392
|
-
methodNames: action.accessKey.methodNames
|
|
393
|
-
}
|
|
394
|
-
}
|
|
395
|
-
}
|
|
396
|
-
}
|
|
397
|
-
};
|
|
398
|
-
}
|
|
399
|
-
case "DeleteKey": {
|
|
400
|
-
return {
|
|
401
|
-
deleteKey: {
|
|
402
|
-
publicKey: {
|
|
403
|
-
ed25519Key: {
|
|
404
|
-
data: keyFromString(action.publicKey)
|
|
405
|
-
}
|
|
406
|
-
}
|
|
407
|
-
}
|
|
408
|
-
};
|
|
409
|
-
}
|
|
410
|
-
case "DeleteAccount": {
|
|
411
|
-
return {
|
|
412
|
-
deleteAccount: {
|
|
413
|
-
beneficiaryId: action.beneficiaryId
|
|
414
|
-
}
|
|
415
|
-
};
|
|
416
|
-
}
|
|
417
|
-
case "SignedDelegate": {
|
|
418
|
-
return {
|
|
419
|
-
signedDelegate: {
|
|
420
|
-
delegateAction: mapAction(action.delegateAction),
|
|
421
|
-
signature: {
|
|
422
|
-
ed25519Signature: (0, import_base58_js.base58_to_binary)(action.signature)
|
|
423
|
-
}
|
|
424
|
-
}
|
|
425
|
-
};
|
|
426
|
-
}
|
|
427
|
-
default: {
|
|
428
|
-
throw new Error("Not implemented action: " + action.type);
|
|
429
|
-
}
|
|
430
|
-
}
|
|
431
|
-
}
|
|
432
|
-
var SCHEMA = new class BorshSchema {
|
|
433
|
-
Ed25519Signature = {
|
|
434
|
-
struct: {
|
|
435
|
-
data: { array: { type: "u8", len: 64 } }
|
|
436
|
-
}
|
|
437
|
-
};
|
|
438
|
-
Secp256k1Signature = {
|
|
439
|
-
struct: {
|
|
440
|
-
data: { array: { type: "u8", len: 65 } }
|
|
441
|
-
}
|
|
442
|
-
};
|
|
443
|
-
Signature = {
|
|
444
|
-
enum: [
|
|
445
|
-
{ struct: { ed25519Signature: this.Ed25519Signature } },
|
|
446
|
-
{ struct: { secp256k1Signature: this.Secp256k1Signature } }
|
|
447
|
-
]
|
|
448
|
-
};
|
|
449
|
-
Ed25519Data = {
|
|
450
|
-
struct: {
|
|
451
|
-
data: { array: { type: "u8", len: 32 } }
|
|
452
|
-
}
|
|
453
|
-
};
|
|
454
|
-
Secp256k1Data = {
|
|
455
|
-
struct: {
|
|
456
|
-
data: { array: { type: "u8", len: 64 } }
|
|
457
|
-
}
|
|
458
|
-
};
|
|
459
|
-
PublicKey = {
|
|
460
|
-
enum: [
|
|
461
|
-
{ struct: { ed25519Key: this.Ed25519Data } },
|
|
462
|
-
{ struct: { secp256k1Key: this.Secp256k1Data } }
|
|
463
|
-
]
|
|
464
|
-
};
|
|
465
|
-
FunctionCallPermission = {
|
|
466
|
-
struct: {
|
|
467
|
-
allowance: { option: "u128" },
|
|
468
|
-
receiverId: "string",
|
|
469
|
-
methodNames: { array: { type: "string" } }
|
|
470
|
-
}
|
|
471
|
-
};
|
|
472
|
-
FullAccessPermission = {
|
|
473
|
-
struct: {}
|
|
474
|
-
};
|
|
475
|
-
AccessKeyPermission = {
|
|
476
|
-
enum: [
|
|
477
|
-
{ struct: { functionCall: this.FunctionCallPermission } },
|
|
478
|
-
{ struct: { fullAccess: this.FullAccessPermission } }
|
|
479
|
-
]
|
|
480
|
-
};
|
|
481
|
-
AccessKey = {
|
|
482
|
-
struct: {
|
|
483
|
-
nonce: "u64",
|
|
484
|
-
permission: this.AccessKeyPermission
|
|
485
|
-
}
|
|
486
|
-
};
|
|
487
|
-
CreateAccount = {
|
|
488
|
-
struct: {}
|
|
489
|
-
};
|
|
490
|
-
DeployContract = {
|
|
491
|
-
struct: {
|
|
492
|
-
code: { array: { type: "u8" } }
|
|
493
|
-
}
|
|
494
|
-
};
|
|
495
|
-
FunctionCall = {
|
|
496
|
-
struct: {
|
|
497
|
-
methodName: "string",
|
|
498
|
-
args: { array: { type: "u8" } },
|
|
499
|
-
gas: "u64",
|
|
500
|
-
deposit: "u128"
|
|
501
|
-
}
|
|
502
|
-
};
|
|
503
|
-
Transfer = {
|
|
504
|
-
struct: {
|
|
505
|
-
deposit: "u128"
|
|
506
|
-
}
|
|
507
|
-
};
|
|
508
|
-
Stake = {
|
|
509
|
-
struct: {
|
|
510
|
-
stake: "u128",
|
|
511
|
-
publicKey: this.PublicKey
|
|
512
|
-
}
|
|
513
|
-
};
|
|
514
|
-
AddKey = {
|
|
515
|
-
struct: {
|
|
516
|
-
publicKey: this.PublicKey,
|
|
517
|
-
accessKey: this.AccessKey
|
|
518
|
-
}
|
|
519
|
-
};
|
|
520
|
-
DeleteKey = {
|
|
521
|
-
struct: {
|
|
522
|
-
publicKey: this.PublicKey
|
|
523
|
-
}
|
|
524
|
-
};
|
|
525
|
-
DeleteAccount = {
|
|
526
|
-
struct: {
|
|
527
|
-
beneficiaryId: "string"
|
|
528
|
-
}
|
|
529
|
-
};
|
|
530
|
-
ClassicAction = {
|
|
531
|
-
enum: [
|
|
532
|
-
{ struct: { createAccount: this.CreateAccount } },
|
|
533
|
-
{ struct: { deployContract: this.DeployContract } },
|
|
534
|
-
{ struct: { functionCall: this.FunctionCall } },
|
|
535
|
-
{ struct: { transfer: this.Transfer } },
|
|
536
|
-
{ struct: { stake: this.Stake } },
|
|
537
|
-
{ struct: { addKey: this.AddKey } },
|
|
538
|
-
{ struct: { deleteKey: this.DeleteKey } },
|
|
539
|
-
{ struct: { deleteAccount: this.DeleteAccount } }
|
|
540
|
-
]
|
|
541
|
-
};
|
|
542
|
-
DelegateAction = {
|
|
543
|
-
struct: {
|
|
544
|
-
senderId: "string",
|
|
545
|
-
receiverId: "string",
|
|
546
|
-
actions: { array: { type: this.ClassicAction } },
|
|
547
|
-
nonce: "u64",
|
|
548
|
-
maxBlockHeight: "u64",
|
|
549
|
-
publicKey: this.PublicKey
|
|
550
|
-
}
|
|
551
|
-
};
|
|
552
|
-
SignedDelegate = {
|
|
553
|
-
struct: {
|
|
554
|
-
delegateAction: this.DelegateAction,
|
|
555
|
-
signature: this.Signature
|
|
556
|
-
}
|
|
557
|
-
};
|
|
558
|
-
Action = {
|
|
559
|
-
enum: [
|
|
560
|
-
{ struct: { createAccount: this.CreateAccount } },
|
|
561
|
-
{ struct: { deployContract: this.DeployContract } },
|
|
562
|
-
{ struct: { functionCall: this.FunctionCall } },
|
|
563
|
-
{ struct: { transfer: this.Transfer } },
|
|
564
|
-
{ struct: { stake: this.Stake } },
|
|
565
|
-
{ struct: { addKey: this.AddKey } },
|
|
566
|
-
{ struct: { deleteKey: this.DeleteKey } },
|
|
567
|
-
{ struct: { deleteAccount: this.DeleteAccount } },
|
|
568
|
-
{ struct: { signedDelegate: this.SignedDelegate } }
|
|
569
|
-
]
|
|
570
|
-
};
|
|
571
|
-
Transaction = {
|
|
572
|
-
struct: {
|
|
573
|
-
signerId: "string",
|
|
574
|
-
publicKey: this.PublicKey,
|
|
575
|
-
nonce: "u64",
|
|
576
|
-
receiverId: "string",
|
|
577
|
-
blockHash: { array: { type: "u8", len: 32 } },
|
|
578
|
-
actions: { array: { type: this.Action } }
|
|
579
|
-
}
|
|
580
|
-
};
|
|
581
|
-
SignedTransaction = {
|
|
582
|
-
struct: {
|
|
583
|
-
transaction: this.Transaction,
|
|
584
|
-
signature: this.Signature
|
|
585
|
-
}
|
|
586
|
-
};
|
|
587
|
-
}();
|
|
588
|
-
|
|
589
|
-
// src/near.ts
|
|
590
|
-
import_big2.default.DP = 27;
|
|
591
|
-
var MaxBlockDelayMs = 1e3 * 60 * 60 * 6;
|
|
592
|
-
var WIDGET_URL = "http://localhost:3000/";
|
|
593
|
-
var DEFAULT_NETWORK_ID = "mainnet";
|
|
594
|
-
var NETWORKS = {
|
|
595
|
-
testnet: {
|
|
596
|
-
networkId: "testnet",
|
|
597
|
-
nodeUrl: "https://rpc.testnet.fastnear.com/"
|
|
598
|
-
},
|
|
599
|
-
mainnet: {
|
|
600
|
-
networkId: "mainnet",
|
|
601
|
-
nodeUrl: "https://rpc.mainnet.fastnear.com/"
|
|
602
|
-
}
|
|
603
|
-
};
|
|
604
|
-
var _config = lsGet("config") || { ...NETWORKS[DEFAULT_NETWORK_ID] };
|
|
605
|
-
var _state = lsGet("state") || {};
|
|
606
|
-
try {
|
|
607
|
-
_state.publicKey = _state.privateKey ? publicKeyFromPrivate(_state.privateKey) : null;
|
|
608
|
-
} catch (e) {
|
|
609
|
-
console.error("Error parsing private key:", e);
|
|
610
|
-
_state.privateKey = null;
|
|
611
|
-
lsSet("nonce", null);
|
|
612
|
-
}
|
|
613
|
-
var _txHistory = lsGet("txHistory") || {};
|
|
614
|
-
var _eventListeners = {
|
|
615
|
-
account: /* @__PURE__ */ new Set(),
|
|
616
|
-
tx: /* @__PURE__ */ new Set()
|
|
617
|
-
};
|
|
618
|
-
var _unbroadcastedEvents = {
|
|
619
|
-
account: [],
|
|
620
|
-
tx: []
|
|
621
|
-
};
|
|
622
|
-
function getWalletAdapterState() {
|
|
623
|
-
return {
|
|
624
|
-
publicKey: _state.publicKey,
|
|
625
|
-
accountId: _state.accountId,
|
|
626
|
-
lastWalletId: _state.lastWalletId,
|
|
627
|
-
networkId: DEFAULT_NETWORK_ID
|
|
628
|
-
};
|
|
629
|
-
}
|
|
630
|
-
var _adapter;
|
|
631
|
-
function updateState(newState) {
|
|
632
|
-
const oldState = _state;
|
|
633
|
-
_state = { ..._state, ...newState };
|
|
634
|
-
lsSet("state", {
|
|
635
|
-
accountId: _state.accountId,
|
|
636
|
-
privateKey: _state.privateKey,
|
|
637
|
-
lastWalletId: _state.lastWalletId,
|
|
638
|
-
accessKeyContractId: _state.accessKeyContractId
|
|
639
|
-
});
|
|
640
|
-
if (newState.hasOwnProperty("privateKey") && newState.privateKey !== oldState.privateKey) {
|
|
641
|
-
_state.publicKey = newState.privateKey ? publicKeyFromPrivate(newState.privateKey) : null;
|
|
642
|
-
lsSet("nonce", null);
|
|
643
|
-
}
|
|
644
|
-
if (newState.accountId !== oldState.accountId) {
|
|
645
|
-
notifyAccountListeners(newState.accountId);
|
|
646
|
-
}
|
|
647
|
-
if (newState.hasOwnProperty("lastWalletId") && newState.lastWalletId !== oldState.lastWalletId || newState.hasOwnProperty("accountId") && newState.accountId !== oldState.accountId || newState.hasOwnProperty("privateKey") && newState.privateKey !== oldState.privateKey) {
|
|
648
|
-
_adapter.setState(getWalletAdapterState());
|
|
649
|
-
}
|
|
650
|
-
}
|
|
651
|
-
function updateTxHistory(txStatus) {
|
|
652
|
-
const txId = txStatus.txId;
|
|
653
|
-
_txHistory[txId] = {
|
|
654
|
-
..._txHistory[txId] ?? {},
|
|
655
|
-
...txStatus,
|
|
656
|
-
updateTimestamp: Date.now()
|
|
657
|
-
};
|
|
658
|
-
lsSet("txHistory", _txHistory);
|
|
659
|
-
notifyTxListeners(_txHistory[txId]);
|
|
660
|
-
}
|
|
661
|
-
function onAdapterStateUpdate(state) {
|
|
662
|
-
console.log("Adapter state update:", state);
|
|
663
|
-
const { accountId, lastWalletId, privateKey } = state;
|
|
664
|
-
updateState({
|
|
665
|
-
accountId,
|
|
666
|
-
lastWalletId,
|
|
667
|
-
...privateKey && { privateKey }
|
|
668
|
-
});
|
|
669
|
-
}
|
|
670
|
-
_adapter = new WalletAdapter({
|
|
671
|
-
onStateUpdate: onAdapterStateUpdate,
|
|
672
|
-
lastState: getWalletAdapterState(),
|
|
673
|
-
widgetUrl: WIDGET_URL
|
|
674
|
-
});
|
|
675
|
-
function parseJsonFromBytes(bytes) {
|
|
676
|
-
try {
|
|
677
|
-
const decoder = new TextDecoder();
|
|
678
|
-
return JSON.parse(decoder.decode(bytes instanceof Uint8Array ? bytes : new Uint8Array(bytes)));
|
|
679
|
-
} catch (e) {
|
|
680
|
-
try {
|
|
681
|
-
return bytes instanceof Uint8Array ? bytes : new Uint8Array(bytes);
|
|
682
|
-
} catch (e2) {
|
|
683
|
-
return bytes;
|
|
684
|
-
}
|
|
685
|
-
}
|
|
686
|
-
}
|
|
687
|
-
function withBlockId(params, blockId) {
|
|
688
|
-
return blockId === "final" || blockId === "optimistic" ? { ...params, finality: blockId } : blockId ? { ...params, block_id: blockId } : { ...params, finality: "optimistic" };
|
|
689
|
-
}
|
|
690
|
-
async function queryRpc(method, params) {
|
|
691
|
-
const response = await fetch(_config.nodeUrl, {
|
|
692
|
-
method: "POST",
|
|
693
|
-
headers: { "Content-Type": "application/json" },
|
|
694
|
-
body: JSON.stringify({
|
|
695
|
-
jsonrpc: "2.0",
|
|
696
|
-
id: `fastnear-${Date.now()}`,
|
|
697
|
-
method,
|
|
698
|
-
params
|
|
699
|
-
})
|
|
700
|
-
});
|
|
701
|
-
const result = await response.json();
|
|
702
|
-
if (result.error) {
|
|
703
|
-
throw new Error(JSON.stringify(result.error));
|
|
704
|
-
}
|
|
705
|
-
return result.result;
|
|
706
|
-
}
|
|
707
|
-
function afterTxSent(txId) {
|
|
708
|
-
queryRpc("tx", {
|
|
709
|
-
tx_hash: _txHistory[txId].txHash,
|
|
710
|
-
sender_account_id: _txHistory[txId].tx.signerId,
|
|
711
|
-
wait_until: "EXECUTED_OPTIMISTIC"
|
|
712
|
-
}).then((result) => {
|
|
713
|
-
const successValue = result?.status?.SuccessValue;
|
|
714
|
-
updateTxHistory({
|
|
715
|
-
txId,
|
|
716
|
-
status: "Executed",
|
|
717
|
-
result,
|
|
718
|
-
successValue: successValue ? tryParseJson(fromBase64(successValue)) : void 0,
|
|
719
|
-
finalState: true
|
|
720
|
-
});
|
|
721
|
-
}).catch((error) => {
|
|
722
|
-
updateTxHistory({
|
|
723
|
-
txId,
|
|
724
|
-
status: "ErrorAfterIncluded",
|
|
725
|
-
error: tryParseJson(error.message),
|
|
726
|
-
finalState: true
|
|
727
|
-
});
|
|
728
|
-
});
|
|
729
|
-
}
|
|
730
|
-
function sendTxToRpc(signedTxBase64, waitUntil, txId) {
|
|
731
|
-
queryRpc("send_tx", {
|
|
732
|
-
signed_tx_base64: signedTxBase64,
|
|
733
|
-
wait_until: waitUntil ?? "INCLUDED"
|
|
734
|
-
}).then((result) => {
|
|
735
|
-
console.log("Transaction included:", result);
|
|
736
|
-
updateTxHistory({
|
|
737
|
-
txId,
|
|
738
|
-
status: "Included",
|
|
739
|
-
finalState: false
|
|
740
|
-
});
|
|
741
|
-
afterTxSent(txId);
|
|
742
|
-
}).catch((error) => {
|
|
743
|
-
updateTxHistory({
|
|
744
|
-
txId,
|
|
745
|
-
status: "Error",
|
|
746
|
-
error: tryParseJson(error.message),
|
|
747
|
-
finalState: false
|
|
748
|
-
});
|
|
749
|
-
});
|
|
750
|
-
}
|
|
751
|
-
function notifyAccountListeners(accountId) {
|
|
752
|
-
if (_eventListeners.account.size === 0) {
|
|
753
|
-
_unbroadcastedEvents.account.push(accountId);
|
|
754
|
-
return;
|
|
755
|
-
}
|
|
756
|
-
_eventListeners.account.forEach((callback) => {
|
|
757
|
-
try {
|
|
758
|
-
callback(accountId);
|
|
759
|
-
} catch (e) {
|
|
760
|
-
console.error(e);
|
|
761
|
-
}
|
|
762
|
-
});
|
|
763
|
-
}
|
|
764
|
-
function notifyTxListeners(tx) {
|
|
765
|
-
if (_eventListeners.tx.size === 0) {
|
|
766
|
-
_unbroadcastedEvents.tx.push(tx);
|
|
767
|
-
return;
|
|
768
|
-
}
|
|
769
|
-
_eventListeners.tx.forEach((callback) => {
|
|
770
|
-
try {
|
|
771
|
-
callback(tx);
|
|
772
|
-
} catch (e) {
|
|
773
|
-
console.error(e);
|
|
774
|
-
}
|
|
775
|
-
});
|
|
776
|
-
}
|
|
777
|
-
function convertUnit(s, ...args) {
|
|
778
|
-
if (Array.isArray(s)) {
|
|
779
|
-
s = s.reduce((acc, part, i) => {
|
|
780
|
-
return acc + (args[i - 1] ?? "") + part;
|
|
781
|
-
});
|
|
782
|
-
}
|
|
783
|
-
if (typeof s == "string") {
|
|
784
|
-
const match = s.match(/([0-9.,_]+)\s*([a-zA-Z]+)?/);
|
|
785
|
-
if (match) {
|
|
786
|
-
const amount = match[1].replace(/[_,]/g, "");
|
|
787
|
-
const unitPart = match[2];
|
|
788
|
-
if (unitPart) {
|
|
789
|
-
switch (unitPart.toLowerCase()) {
|
|
790
|
-
case "near":
|
|
791
|
-
return (0, import_big2.default)(amount).mul((0, import_big2.default)(10).pow(24)).toFixed(0);
|
|
792
|
-
case "tgas":
|
|
793
|
-
return (0, import_big2.default)(amount).mul((0, import_big2.default)(10).pow(12)).toFixed(0);
|
|
794
|
-
case "ggas":
|
|
795
|
-
return (0, import_big2.default)(amount).mul((0, import_big2.default)(10).pow(9)).toFixed(0);
|
|
796
|
-
case "gas":
|
|
797
|
-
case "yoctonear":
|
|
798
|
-
return (0, import_big2.default)(amount).toFixed(0);
|
|
799
|
-
default:
|
|
800
|
-
throw new Error(`Unknown unit: ${unitPart}`);
|
|
801
|
-
}
|
|
802
|
-
} else {
|
|
803
|
-
return (0, import_big2.default)(amount).toFixed(0);
|
|
804
|
-
}
|
|
805
|
-
}
|
|
806
|
-
}
|
|
807
|
-
return (0, import_big2.default)(s).toFixed(0);
|
|
808
|
-
}
|
|
809
|
-
var api = {
|
|
810
|
-
// Context
|
|
811
|
-
get accountId() {
|
|
812
|
-
return _state.accountId;
|
|
813
|
-
},
|
|
814
|
-
get publicKey() {
|
|
815
|
-
return _state.publicKey;
|
|
816
|
-
},
|
|
817
|
-
config(newConfig) {
|
|
818
|
-
if (newConfig) {
|
|
819
|
-
if (newConfig.networkId && _config.networkId !== newConfig.networkId) {
|
|
820
|
-
_config = { ...NETWORKS[newConfig.networkId] };
|
|
821
|
-
updateState({
|
|
822
|
-
accountId: null,
|
|
823
|
-
privateKey: null,
|
|
824
|
-
lastWalletId: null
|
|
825
|
-
});
|
|
826
|
-
lsSet("block", null);
|
|
827
|
-
_txHistory = {};
|
|
828
|
-
lsSet("txHistory", _txHistory);
|
|
829
|
-
}
|
|
830
|
-
_config = { ..._config, ...newConfig };
|
|
831
|
-
lsSet("config", _config);
|
|
832
|
-
}
|
|
833
|
-
return _config;
|
|
834
|
-
},
|
|
835
|
-
get authStatus() {
|
|
836
|
-
if (!_state.accountId) {
|
|
837
|
-
return "SignedOut";
|
|
838
|
-
}
|
|
839
|
-
const accessKey = _state.publicKey;
|
|
840
|
-
const contractId = _state.accessKeyContractId;
|
|
841
|
-
if (accessKey && contractId && _state.privateKey) {
|
|
842
|
-
return {
|
|
843
|
-
type: "SignedInWithLimitedAccessKey",
|
|
844
|
-
accessKey,
|
|
845
|
-
contractId
|
|
846
|
-
};
|
|
847
|
-
}
|
|
848
|
-
return "SignedIn";
|
|
849
|
-
},
|
|
850
|
-
// Query Methods
|
|
851
|
-
async view({
|
|
852
|
-
contractId,
|
|
853
|
-
methodName,
|
|
854
|
-
args,
|
|
855
|
-
argsBase64,
|
|
856
|
-
blockId
|
|
857
|
-
}) {
|
|
858
|
-
const encodedArgs = argsBase64 || (args ? toBase64(JSON.stringify(args)) : "");
|
|
859
|
-
const result = await queryRpc(
|
|
860
|
-
"query",
|
|
861
|
-
withBlockId(
|
|
862
|
-
{
|
|
863
|
-
request_type: "call_function",
|
|
864
|
-
account_id: contractId,
|
|
865
|
-
method_name: methodName,
|
|
866
|
-
args_base64: encodedArgs
|
|
867
|
-
},
|
|
868
|
-
blockId
|
|
869
|
-
)
|
|
870
|
-
);
|
|
871
|
-
return parseJsonFromBytes(result.result);
|
|
872
|
-
},
|
|
873
|
-
async account({
|
|
874
|
-
accountId,
|
|
875
|
-
blockId
|
|
876
|
-
}) {
|
|
877
|
-
return queryRpc(
|
|
878
|
-
"query",
|
|
879
|
-
withBlockId(
|
|
880
|
-
{
|
|
881
|
-
request_type: "view_account",
|
|
882
|
-
account_id: accountId
|
|
883
|
-
},
|
|
884
|
-
blockId
|
|
885
|
-
)
|
|
886
|
-
);
|
|
887
|
-
},
|
|
888
|
-
async block({ blockId }) {
|
|
889
|
-
return queryRpc("block", withBlockId({}, blockId));
|
|
890
|
-
},
|
|
891
|
-
async accessKey({
|
|
892
|
-
accountId,
|
|
893
|
-
publicKey,
|
|
894
|
-
blockId
|
|
895
|
-
}) {
|
|
896
|
-
return queryRpc(
|
|
897
|
-
"query",
|
|
898
|
-
withBlockId(
|
|
899
|
-
{
|
|
900
|
-
request_type: "view_access_key",
|
|
901
|
-
account_id: accountId,
|
|
902
|
-
public_key: publicKey
|
|
903
|
-
},
|
|
904
|
-
blockId
|
|
905
|
-
)
|
|
906
|
-
);
|
|
907
|
-
},
|
|
908
|
-
async tx({ txHash, accountId }) {
|
|
909
|
-
return queryRpc("tx", [txHash, accountId]);
|
|
910
|
-
},
|
|
911
|
-
localTxHistory() {
|
|
912
|
-
return [..._txHistory];
|
|
913
|
-
},
|
|
914
|
-
// Transaction Methods
|
|
915
|
-
async sendTx({
|
|
916
|
-
receiverId,
|
|
917
|
-
actions,
|
|
918
|
-
waitUntil
|
|
919
|
-
}) {
|
|
920
|
-
const signerId = _state.accountId;
|
|
921
|
-
if (!signerId) {
|
|
922
|
-
throw new Error("Not signed in");
|
|
923
|
-
}
|
|
924
|
-
const publicKey = _state.publicKey;
|
|
925
|
-
const privateKey = _state.privateKey;
|
|
926
|
-
const txId = `tx-${Date.now()}-${Math.random()}`;
|
|
927
|
-
if (!privateKey || receiverId !== _state.accessKeyContractId || !canSignWithLAK(actions)) {
|
|
928
|
-
const jsonTransaction2 = {
|
|
929
|
-
signerId,
|
|
930
|
-
receiverId,
|
|
931
|
-
actions
|
|
932
|
-
};
|
|
933
|
-
updateTxHistory({
|
|
934
|
-
status: "Pending",
|
|
935
|
-
txId,
|
|
936
|
-
tx: jsonTransaction2,
|
|
937
|
-
finalState: false
|
|
938
|
-
});
|
|
939
|
-
const url = new URL(typeof window !== "undefined" ? window.location.href : "");
|
|
940
|
-
url.searchParams.set("txIds", txId);
|
|
941
|
-
_adapter.sendTransactions({
|
|
942
|
-
transactions: [jsonTransaction2],
|
|
943
|
-
callbackUrl: url.toString()
|
|
944
|
-
}).then((result) => {
|
|
945
|
-
console.log("Transaction result:", result);
|
|
946
|
-
if (result.url) {
|
|
947
|
-
console.log("Redirecting to wallet:", result.url);
|
|
948
|
-
if (typeof window !== "undefined") {
|
|
949
|
-
setTimeout(() => {
|
|
950
|
-
window.location.href = result.url;
|
|
951
|
-
}, 100);
|
|
952
|
-
}
|
|
953
|
-
} else if (result.outcomes) {
|
|
954
|
-
result.outcomes.forEach((r) => {
|
|
955
|
-
updateTxHistory({
|
|
956
|
-
txId,
|
|
957
|
-
status: "Executed",
|
|
958
|
-
result: r,
|
|
959
|
-
txHash: r.transaction.hash,
|
|
960
|
-
finalState: true
|
|
961
|
-
});
|
|
962
|
-
});
|
|
963
|
-
} else if (result.rejected) {
|
|
964
|
-
updateTxHistory({
|
|
965
|
-
txId,
|
|
966
|
-
status: "RejectedByUser",
|
|
967
|
-
finalState: true
|
|
968
|
-
});
|
|
969
|
-
} else if (result.error) {
|
|
970
|
-
updateTxHistory({
|
|
971
|
-
txId,
|
|
972
|
-
status: "Error",
|
|
973
|
-
error: tryParseJson(result.error),
|
|
974
|
-
finalState: true
|
|
975
|
-
});
|
|
976
|
-
}
|
|
977
|
-
}).catch((error) => {
|
|
978
|
-
updateTxHistory({
|
|
979
|
-
txId,
|
|
980
|
-
status: "Error",
|
|
981
|
-
error: tryParseJson(error.message),
|
|
982
|
-
finalState: true
|
|
983
|
-
});
|
|
984
|
-
});
|
|
985
|
-
return txId;
|
|
986
|
-
}
|
|
987
|
-
let nonce = lsGet("nonce");
|
|
988
|
-
let block = lsGet("block");
|
|
989
|
-
const toDoPromises = {};
|
|
990
|
-
if (nonce === null || nonce === void 0) {
|
|
991
|
-
toDoPromises.nonce = api.accessKey({
|
|
992
|
-
accountId: signerId,
|
|
993
|
-
publicKey
|
|
994
|
-
}).then((accessKey) => {
|
|
995
|
-
if (accessKey.error) {
|
|
996
|
-
throw new Error(`Access key error: ${accessKey.error}`);
|
|
997
|
-
}
|
|
998
|
-
lsSet("nonce", accessKey.nonce);
|
|
999
|
-
return accessKey.nonce;
|
|
1000
|
-
});
|
|
1001
|
-
}
|
|
1002
|
-
if (!block || !block.header || parseFloat(block.header.timestamp_nanosec) / 1e6 + MaxBlockDelayMs < Date.now()) {
|
|
1003
|
-
toDoPromises.block = api.block({ blockId: "final" }).then((b) => {
|
|
1004
|
-
const newBlock = {
|
|
1005
|
-
header: {
|
|
1006
|
-
prev_hash: b.header.prev_hash,
|
|
1007
|
-
timestamp_nanosec: b.header.timestamp_nanosec
|
|
1008
|
-
}
|
|
1009
|
-
};
|
|
1010
|
-
lsSet("block", newBlock);
|
|
1011
|
-
return newBlock;
|
|
1012
|
-
});
|
|
1013
|
-
}
|
|
1014
|
-
if (Object.keys(toDoPromises).length > 0) {
|
|
1015
|
-
const results = await Promise.all(Object.values(toDoPromises));
|
|
1016
|
-
const keys = Object.keys(toDoPromises);
|
|
1017
|
-
results.forEach((res, i) => {
|
|
1018
|
-
if (keys[i] === "nonce") {
|
|
1019
|
-
nonce = res;
|
|
1020
|
-
} else if (keys[i] === "block") {
|
|
1021
|
-
block = res;
|
|
1022
|
-
}
|
|
1023
|
-
});
|
|
1024
|
-
}
|
|
1025
|
-
const newNonce = (nonce ?? 0) + 1;
|
|
1026
|
-
lsSet("nonce", newNonce);
|
|
1027
|
-
const blockHash = block.header.prev_hash;
|
|
1028
|
-
const jsonTransaction = {
|
|
1029
|
-
signerId,
|
|
1030
|
-
publicKey,
|
|
1031
|
-
nonce: newNonce,
|
|
1032
|
-
receiverId,
|
|
1033
|
-
blockHash,
|
|
1034
|
-
actions
|
|
1035
|
-
};
|
|
1036
|
-
console.log("Transaction:", jsonTransaction);
|
|
1037
|
-
const transaction = serializeTransaction(jsonTransaction);
|
|
1038
|
-
const txHash = (0, import_base58_js.binary_to_base58)(import_sha2.sha256(transaction));
|
|
1039
|
-
const signature = signHash(txHash, privateKey);
|
|
1040
|
-
const signedTransaction = serializeSignedTransaction(jsonTransaction, signature);
|
|
1041
|
-
const signedTxBase64 = toBase64(signedTransaction);
|
|
1042
|
-
updateTxHistory({
|
|
1043
|
-
status: "Pending",
|
|
1044
|
-
txId,
|
|
1045
|
-
tx: jsonTransaction,
|
|
1046
|
-
signature,
|
|
1047
|
-
signedTxBase64,
|
|
1048
|
-
txHash,
|
|
1049
|
-
finalState: false
|
|
1050
|
-
});
|
|
1051
|
-
sendTxToRpc(signedTxBase64, waitUntil, txId);
|
|
1052
|
-
return txId;
|
|
1053
|
-
},
|
|
1054
|
-
// Authentication Methods
|
|
1055
|
-
async requestSignIn({ contractId }) {
|
|
1056
|
-
const privateKey = privateKeyFromRandom();
|
|
1057
|
-
updateState({
|
|
1058
|
-
accessKeyContractId: contractId,
|
|
1059
|
-
accountId: null,
|
|
1060
|
-
privateKey
|
|
1061
|
-
});
|
|
1062
|
-
const publicKey = publicKeyFromPrivate(privateKey);
|
|
1063
|
-
const result = await _adapter.signIn({
|
|
1064
|
-
networkId: _config.networkId,
|
|
1065
|
-
contractId,
|
|
1066
|
-
publicKey
|
|
1067
|
-
});
|
|
1068
|
-
console.log("Sign in result:", result);
|
|
1069
|
-
if (result.error) {
|
|
1070
|
-
throw new Error(`Wallet error: ${result.error}`);
|
|
1071
|
-
}
|
|
1072
|
-
if (result.url) {
|
|
1073
|
-
console.log("Redirecting to wallet:", result.url);
|
|
1074
|
-
if (typeof window !== "undefined") {
|
|
1075
|
-
setTimeout(() => {
|
|
1076
|
-
window.location.href = result.url;
|
|
1077
|
-
}, 100);
|
|
1078
|
-
}
|
|
1079
|
-
} else if (result.accountId) {
|
|
1080
|
-
updateState({
|
|
1081
|
-
accountId: result.accountId
|
|
1082
|
-
});
|
|
1083
|
-
}
|
|
1084
|
-
},
|
|
1085
|
-
signOut() {
|
|
1086
|
-
updateState({
|
|
1087
|
-
accountId: null,
|
|
1088
|
-
privateKey: null,
|
|
1089
|
-
contractId: null
|
|
1090
|
-
});
|
|
1091
|
-
},
|
|
1092
|
-
// Event Handlers
|
|
1093
|
-
onAccount(callback) {
|
|
1094
|
-
_eventListeners.account.add(callback);
|
|
1095
|
-
if (_unbroadcastedEvents.account.length > 0) {
|
|
1096
|
-
const events = _unbroadcastedEvents.account;
|
|
1097
|
-
_unbroadcastedEvents.account = [];
|
|
1098
|
-
events.forEach(notifyAccountListeners);
|
|
1099
|
-
}
|
|
1100
|
-
},
|
|
1101
|
-
onTx(callback) {
|
|
1102
|
-
_eventListeners.tx.add(callback);
|
|
1103
|
-
if (_unbroadcastedEvents.tx.length > 0) {
|
|
1104
|
-
const events = _unbroadcastedEvents.tx;
|
|
1105
|
-
_unbroadcastedEvents.tx = [];
|
|
1106
|
-
events.forEach(notifyTxListeners);
|
|
1107
|
-
}
|
|
1108
|
-
},
|
|
1109
|
-
// Action Helpers
|
|
1110
|
-
actions: {
|
|
1111
|
-
functionCall: ({
|
|
1112
|
-
methodName,
|
|
1113
|
-
gas,
|
|
1114
|
-
deposit,
|
|
1115
|
-
args,
|
|
1116
|
-
argsBase64
|
|
1117
|
-
}) => ({
|
|
1118
|
-
type: "FunctionCall",
|
|
1119
|
-
methodName,
|
|
1120
|
-
args,
|
|
1121
|
-
argsBase64,
|
|
1122
|
-
gas,
|
|
1123
|
-
deposit
|
|
1124
|
-
}),
|
|
1125
|
-
transfer: (yoctoAmount) => ({
|
|
1126
|
-
type: "Transfer",
|
|
1127
|
-
deposit: yoctoAmount
|
|
1128
|
-
}),
|
|
1129
|
-
stakeNEAR: ({ amount, publicKey }) => ({
|
|
1130
|
-
type: "Stake",
|
|
1131
|
-
stake: amount,
|
|
1132
|
-
publicKey
|
|
1133
|
-
}),
|
|
1134
|
-
addFullAccessKey: ({ publicKey }) => ({
|
|
1135
|
-
type: "AddKey",
|
|
1136
|
-
publicKey,
|
|
1137
|
-
accessKey: { permission: "FullAccess" }
|
|
1138
|
-
}),
|
|
1139
|
-
addLimitedAccessKey: ({
|
|
1140
|
-
publicKey,
|
|
1141
|
-
allowance,
|
|
1142
|
-
accountId,
|
|
1143
|
-
methodNames
|
|
1144
|
-
}) => ({
|
|
1145
|
-
type: "AddKey",
|
|
1146
|
-
publicKey,
|
|
1147
|
-
accessKey: {
|
|
1148
|
-
permission: "FunctionCall",
|
|
1149
|
-
allowance,
|
|
1150
|
-
receiverId: accountId,
|
|
1151
|
-
methodNames
|
|
1152
|
-
}
|
|
1153
|
-
}),
|
|
1154
|
-
deleteKey: ({ publicKey }) => ({
|
|
1155
|
-
type: "DeleteKey",
|
|
1156
|
-
publicKey
|
|
1157
|
-
}),
|
|
1158
|
-
deleteAccount: ({ beneficiaryId }) => ({
|
|
1159
|
-
type: "DeleteAccount",
|
|
1160
|
-
beneficiaryId
|
|
1161
|
-
}),
|
|
1162
|
-
createAccount: () => ({
|
|
1163
|
-
type: "CreateAccount"
|
|
1164
|
-
}),
|
|
1165
|
-
deployContract: ({ codeBase64 }) => ({
|
|
1166
|
-
type: "DeployContract",
|
|
1167
|
-
codeBase64
|
|
1168
|
-
})
|
|
1169
|
-
},
|
|
1170
|
-
utils: {
|
|
1171
|
-
toBase64,
|
|
1172
|
-
fromBase64,
|
|
1173
|
-
toBase58: import_base58_js.binary_to_base58,
|
|
1174
|
-
fromBase58: import_base58_js.base58_to_binary
|
|
1175
|
-
}
|
|
1176
|
-
};
|
|
1177
|
-
try {
|
|
1178
|
-
if (typeof window !== "undefined") {
|
|
1179
|
-
const url = new URL(window.location.href);
|
|
1180
|
-
const accountId = url.searchParams.get("account_id");
|
|
1181
|
-
const publicKey = url.searchParams.get("public_key");
|
|
1182
|
-
const errorCode = url.searchParams.get("errorCode");
|
|
1183
|
-
const errorMessage = url.searchParams.get("errorMessage");
|
|
1184
|
-
const transactionHashes = url.searchParams.get("transactionHashes");
|
|
1185
|
-
const txIds = url.searchParams.get("txIds");
|
|
1186
|
-
if (errorCode || errorMessage) {
|
|
1187
|
-
console.warn(new Error(`Wallet error: ${errorCode} ${errorMessage}`));
|
|
1188
|
-
}
|
|
1189
|
-
if (accountId && publicKey) {
|
|
1190
|
-
if (publicKey === _state.publicKey) {
|
|
1191
|
-
updateState({
|
|
1192
|
-
accountId
|
|
1193
|
-
});
|
|
1194
|
-
} else {
|
|
1195
|
-
console.error(
|
|
1196
|
-
new Error("Public key mismatch from wallet redirect"),
|
|
1197
|
-
publicKey,
|
|
1198
|
-
_state.publicKey
|
|
1199
|
-
);
|
|
1200
|
-
}
|
|
1201
|
-
}
|
|
1202
|
-
if (transactionHashes || txIds) {
|
|
1203
|
-
const txHashes = transactionHashes ? transactionHashes.split(",") : [];
|
|
1204
|
-
const txIdsArray = txIds ? txIds.split(",") : [];
|
|
1205
|
-
if (txIdsArray.length > txHashes.length) {
|
|
1206
|
-
txIdsArray.forEach((txId, i) => {
|
|
1207
|
-
updateTxHistory({
|
|
1208
|
-
txId,
|
|
1209
|
-
status: "RejectedByUser",
|
|
1210
|
-
finalState: true
|
|
1211
|
-
});
|
|
1212
|
-
});
|
|
1213
|
-
} else if (txIdsArray.length === txHashes.length) {
|
|
1214
|
-
txIdsArray.forEach((txId, i) => {
|
|
1215
|
-
updateTxHistory({
|
|
1216
|
-
txId,
|
|
1217
|
-
status: "PendingGotTxHash",
|
|
1218
|
-
txHash: txHashes[i],
|
|
1219
|
-
finalState: false
|
|
1220
|
-
});
|
|
1221
|
-
afterTxSent(txId);
|
|
1222
|
-
});
|
|
1223
|
-
} else {
|
|
1224
|
-
console.error(
|
|
1225
|
-
new Error("Transaction hash mismatch from wallet redirect"),
|
|
1226
|
-
txIdsArray,
|
|
1227
|
-
txHashes
|
|
1228
|
-
);
|
|
1229
|
-
}
|
|
1230
|
-
}
|
|
1231
|
-
url.searchParams.delete("account_id");
|
|
1232
|
-
url.searchParams.delete("public_key");
|
|
1233
|
-
url.searchParams.delete("errorCode");
|
|
1234
|
-
url.searchParams.delete("errorMessage");
|
|
1235
|
-
url.searchParams.delete("all_keys");
|
|
1236
|
-
url.searchParams.delete("transactionHashes");
|
|
1237
|
-
url.searchParams.delete("txIds");
|
|
1238
|
-
window.history.replaceState({}, "", url.toString());
|
|
1239
|
-
}
|
|
1240
|
-
} catch (e) {
|
|
1241
|
-
console.error("Error handling wallet redirect:", e);
|
|
1242
|
-
}
|
|
1243
|
-
|
|
1244
|
-
// src/index.ts
|
|
1245
|
-
window.near = api;
|
|
1246
|
-
window.$$ = convertUnit;
|
|
1247
|
-
// Annotate the CommonJS export names for ESM import in node:
|
|
1248
|
-
0 && (module.exports = {
|
|
1249
|
-
SCHEMA,
|
|
1250
|
-
api,
|
|
1251
|
-
canSignWithLAK,
|
|
1252
|
-
convertUnit,
|
|
1253
|
-
deepCopy,
|
|
1254
|
-
fromBase58,
|
|
1255
|
-
fromBase64,
|
|
1256
|
-
keyFromString,
|
|
1257
|
-
keyToString,
|
|
1258
|
-
lsGet,
|
|
1259
|
-
lsSet,
|
|
1260
|
-
mapAction,
|
|
1261
|
-
near,
|
|
1262
|
-
parseJsonFromBytes,
|
|
1263
|
-
privateKeyFromRandom,
|
|
1264
|
-
publicKeyFromPrivate,
|
|
1265
|
-
serializeSignedTransaction,
|
|
1266
|
-
serializeTransaction,
|
|
1267
|
-
sha256,
|
|
1268
|
-
signBytes,
|
|
1269
|
-
signHash,
|
|
1270
|
-
toBase58,
|
|
1271
|
-
toBase64,
|
|
1272
|
-
tryParseJson
|
|
1273
|
-
});
|
|
1274
|
-
//# sourceMappingURL=index.js.map
|