@fastnear/api 0.0.1-rc.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/README.md +76 -0
- package/dist/fastnear.cjs +35 -0
- package/dist/fastnear.ejs +35 -0
- package/dist/fastnear.js +35 -0
- package/package.json +33 -0
- package/src/crypto.js +37 -0
- package/src/index.js +8 -0
- package/src/near.js +567 -0
- package/src/transaction.js +287 -0
- package/src/utils.js +48 -0
|
@@ -0,0 +1,287 @@
|
|
|
1
|
+
import { serialize as borshSerialize } from "borsh";
|
|
2
|
+
import { keyFromString } from "./crypto";
|
|
3
|
+
import { fromBase58, fromBase64 } from "./utils";
|
|
4
|
+
|
|
5
|
+
function mapTransaction(jsonTransaction) {
|
|
6
|
+
return {
|
|
7
|
+
signerId: jsonTransaction.signerId,
|
|
8
|
+
publicKey: {
|
|
9
|
+
ed25519Key: {
|
|
10
|
+
data: keyFromString(jsonTransaction.publicKey),
|
|
11
|
+
},
|
|
12
|
+
},
|
|
13
|
+
nonce: BigInt(jsonTransaction.nonce),
|
|
14
|
+
receiverId: jsonTransaction.receiverId,
|
|
15
|
+
blockHash: fromBase58(jsonTransaction.blockHash),
|
|
16
|
+
actions: jsonTransaction.actions.map(mapAction),
|
|
17
|
+
};
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export function serializeTransaction(jsonTransaction) {
|
|
21
|
+
const transaction = mapTransaction(jsonTransaction);
|
|
22
|
+
return borshSerialize(SCHEMA.Transaction, transaction);
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export function serializeSignedTransaction(jsonTransaction, signature) {
|
|
26
|
+
const signedTransaction = {
|
|
27
|
+
transaction: mapTransaction(jsonTransaction),
|
|
28
|
+
signature: {
|
|
29
|
+
ed25519Signature: {
|
|
30
|
+
data: fromBase58(signature),
|
|
31
|
+
},
|
|
32
|
+
},
|
|
33
|
+
};
|
|
34
|
+
return borshSerialize(SCHEMA.SignedTransaction, signedTransaction);
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export function mapAction(action) {
|
|
38
|
+
switch (action.type) {
|
|
39
|
+
case "CreateAccount": {
|
|
40
|
+
return {
|
|
41
|
+
createAccount: {},
|
|
42
|
+
};
|
|
43
|
+
}
|
|
44
|
+
case "DeployContract": {
|
|
45
|
+
return {
|
|
46
|
+
deployContract: {
|
|
47
|
+
code: fromBase64(action.codeBase64),
|
|
48
|
+
},
|
|
49
|
+
};
|
|
50
|
+
}
|
|
51
|
+
case "FunctionCall": {
|
|
52
|
+
return {
|
|
53
|
+
functionCall: {
|
|
54
|
+
methodName: action.methodName,
|
|
55
|
+
args: action.argsBase64
|
|
56
|
+
? fromBase64(action.argsBase64)
|
|
57
|
+
: Buffer.from(JSON.stringify(action.args)),
|
|
58
|
+
gas: BigInt(action.gas),
|
|
59
|
+
deposit: BigInt(action.deposit),
|
|
60
|
+
},
|
|
61
|
+
};
|
|
62
|
+
}
|
|
63
|
+
case "Transfer": {
|
|
64
|
+
return {
|
|
65
|
+
transfer: {
|
|
66
|
+
deposit: BigInt(action.deposit),
|
|
67
|
+
},
|
|
68
|
+
};
|
|
69
|
+
}
|
|
70
|
+
case "Stake": {
|
|
71
|
+
return {
|
|
72
|
+
stake: {
|
|
73
|
+
stake: BigInt(action.stake),
|
|
74
|
+
publicKey: {
|
|
75
|
+
ed25519Key: keyFromString(action.publicKey),
|
|
76
|
+
},
|
|
77
|
+
},
|
|
78
|
+
};
|
|
79
|
+
}
|
|
80
|
+
case "AddKey": {
|
|
81
|
+
return {
|
|
82
|
+
addKey: {
|
|
83
|
+
publicKey: keyFromString(action.publicKey),
|
|
84
|
+
accessKey: {
|
|
85
|
+
nonce: BigInt(action.accessKey.nonce),
|
|
86
|
+
permission:
|
|
87
|
+
action.accessKey.permission === "FullAccess"
|
|
88
|
+
? { fullAccess: {} }
|
|
89
|
+
: {
|
|
90
|
+
functionCall: {
|
|
91
|
+
allowance: action.accessKey.allowance
|
|
92
|
+
? BigInt(action.accessKey.allowance)
|
|
93
|
+
: null,
|
|
94
|
+
receiverId: action.accessKey.receiverId,
|
|
95
|
+
methodNames: action.accessKey.methodNames,
|
|
96
|
+
},
|
|
97
|
+
},
|
|
98
|
+
},
|
|
99
|
+
},
|
|
100
|
+
};
|
|
101
|
+
}
|
|
102
|
+
case "DeleteKey": {
|
|
103
|
+
return {
|
|
104
|
+
deleteKey: {
|
|
105
|
+
publicKey: keyFromString(action.publicKey),
|
|
106
|
+
},
|
|
107
|
+
};
|
|
108
|
+
}
|
|
109
|
+
case "DeleteAccount": {
|
|
110
|
+
return {
|
|
111
|
+
deleteAccount: {
|
|
112
|
+
beneficiaryId: action.beneficiaryId,
|
|
113
|
+
},
|
|
114
|
+
};
|
|
115
|
+
}
|
|
116
|
+
case "SignedDelegate": {
|
|
117
|
+
return {
|
|
118
|
+
signedDelegate: {
|
|
119
|
+
delegateAction: mapAction(action.delegateAction),
|
|
120
|
+
signature: {
|
|
121
|
+
ed25519Signature: fromBase58(action.signature),
|
|
122
|
+
},
|
|
123
|
+
},
|
|
124
|
+
};
|
|
125
|
+
}
|
|
126
|
+
default: {
|
|
127
|
+
throw new Error("Not implemented action: " + action.type);
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
export const SCHEMA = new (class BorshSchema {
|
|
133
|
+
Ed25519Signature = {
|
|
134
|
+
struct: {
|
|
135
|
+
data: { array: { type: "u8", len: 64 } },
|
|
136
|
+
},
|
|
137
|
+
};
|
|
138
|
+
Secp256k1Signature = {
|
|
139
|
+
struct: {
|
|
140
|
+
data: { array: { type: "u8", len: 65 } },
|
|
141
|
+
},
|
|
142
|
+
};
|
|
143
|
+
Signature = {
|
|
144
|
+
enum: [
|
|
145
|
+
{ struct: { ed25519Signature: this.Ed25519Signature } },
|
|
146
|
+
{ struct: { secp256k1Signature: this.Secp256k1Signature } },
|
|
147
|
+
],
|
|
148
|
+
};
|
|
149
|
+
Ed25519Data = {
|
|
150
|
+
struct: {
|
|
151
|
+
data: { array: { type: "u8", len: 32 } },
|
|
152
|
+
},
|
|
153
|
+
};
|
|
154
|
+
Secp256k1Data = {
|
|
155
|
+
struct: {
|
|
156
|
+
data: { array: { type: "u8", len: 64 } },
|
|
157
|
+
},
|
|
158
|
+
};
|
|
159
|
+
PublicKey = {
|
|
160
|
+
enum: [
|
|
161
|
+
{ struct: { ed25519Key: this.Ed25519Data } },
|
|
162
|
+
{ struct: { secp256k1Key: this.Secp256k1Data } },
|
|
163
|
+
],
|
|
164
|
+
};
|
|
165
|
+
FunctionCallPermission = {
|
|
166
|
+
struct: {
|
|
167
|
+
allowance: { option: "u128" },
|
|
168
|
+
receiverId: "string",
|
|
169
|
+
methodNames: { array: { type: "string" } },
|
|
170
|
+
},
|
|
171
|
+
};
|
|
172
|
+
FullAccessPermission = {
|
|
173
|
+
struct: {},
|
|
174
|
+
};
|
|
175
|
+
AccessKeyPermission = {
|
|
176
|
+
enum: [
|
|
177
|
+
{ struct: { functionCall: this.FunctionCallPermission } },
|
|
178
|
+
{ struct: { fullAccess: this.FullAccessPermission } },
|
|
179
|
+
],
|
|
180
|
+
};
|
|
181
|
+
AccessKey = {
|
|
182
|
+
struct: {
|
|
183
|
+
nonce: "u64",
|
|
184
|
+
permission: this.AccessKeyPermission,
|
|
185
|
+
},
|
|
186
|
+
};
|
|
187
|
+
CreateAccount = {
|
|
188
|
+
struct: {},
|
|
189
|
+
};
|
|
190
|
+
DeployContract = {
|
|
191
|
+
struct: {
|
|
192
|
+
code: { array: { type: "u8" } },
|
|
193
|
+
},
|
|
194
|
+
};
|
|
195
|
+
FunctionCall = {
|
|
196
|
+
struct: {
|
|
197
|
+
methodName: "string",
|
|
198
|
+
args: { array: { type: "u8" } },
|
|
199
|
+
gas: "u64",
|
|
200
|
+
deposit: "u128",
|
|
201
|
+
},
|
|
202
|
+
};
|
|
203
|
+
Transfer = {
|
|
204
|
+
struct: {
|
|
205
|
+
deposit: "u128",
|
|
206
|
+
},
|
|
207
|
+
};
|
|
208
|
+
Stake = {
|
|
209
|
+
struct: {
|
|
210
|
+
stake: "u128",
|
|
211
|
+
publicKey: this.PublicKey,
|
|
212
|
+
},
|
|
213
|
+
};
|
|
214
|
+
AddKey = {
|
|
215
|
+
struct: {
|
|
216
|
+
publicKey: this.PublicKey,
|
|
217
|
+
accessKey: this.AccessKey,
|
|
218
|
+
},
|
|
219
|
+
};
|
|
220
|
+
DeleteKey = {
|
|
221
|
+
struct: {
|
|
222
|
+
publicKey: this.PublicKey,
|
|
223
|
+
},
|
|
224
|
+
};
|
|
225
|
+
DeleteAccount = {
|
|
226
|
+
struct: {
|
|
227
|
+
beneficiaryId: "string",
|
|
228
|
+
},
|
|
229
|
+
};
|
|
230
|
+
ClassicAction = {
|
|
231
|
+
enum: [
|
|
232
|
+
{ struct: { createAccount: this.CreateAccount } },
|
|
233
|
+
{ struct: { deployContract: this.DeployContract } },
|
|
234
|
+
{ struct: { functionCall: this.FunctionCall } },
|
|
235
|
+
{ struct: { transfer: this.Transfer } },
|
|
236
|
+
{ struct: { stake: this.Stake } },
|
|
237
|
+
{ struct: { addKey: this.AddKey } },
|
|
238
|
+
{ struct: { deleteKey: this.DeleteKey } },
|
|
239
|
+
{ struct: { deleteAccount: this.DeleteAccount } },
|
|
240
|
+
],
|
|
241
|
+
};
|
|
242
|
+
DelegateAction = {
|
|
243
|
+
struct: {
|
|
244
|
+
senderId: "string",
|
|
245
|
+
receiverId: "string",
|
|
246
|
+
actions: { array: { type: this.ClassicAction } },
|
|
247
|
+
nonce: "u64",
|
|
248
|
+
maxBlockHeight: "u64",
|
|
249
|
+
publicKey: this.PublicKey,
|
|
250
|
+
},
|
|
251
|
+
};
|
|
252
|
+
SignedDelegate = {
|
|
253
|
+
struct: {
|
|
254
|
+
delegateAction: this.DelegateAction,
|
|
255
|
+
signature: this.Signature,
|
|
256
|
+
},
|
|
257
|
+
};
|
|
258
|
+
Action = {
|
|
259
|
+
enum: [
|
|
260
|
+
{ struct: { createAccount: this.CreateAccount } },
|
|
261
|
+
{ struct: { deployContract: this.DeployContract } },
|
|
262
|
+
{ struct: { functionCall: this.FunctionCall } },
|
|
263
|
+
{ struct: { transfer: this.Transfer } },
|
|
264
|
+
{ struct: { stake: this.Stake } },
|
|
265
|
+
{ struct: { addKey: this.AddKey } },
|
|
266
|
+
{ struct: { deleteKey: this.DeleteKey } },
|
|
267
|
+
{ struct: { deleteAccount: this.DeleteAccount } },
|
|
268
|
+
{ struct: { signedDelegate: this.SignedDelegate } },
|
|
269
|
+
],
|
|
270
|
+
};
|
|
271
|
+
Transaction = {
|
|
272
|
+
struct: {
|
|
273
|
+
signerId: "string",
|
|
274
|
+
publicKey: this.PublicKey,
|
|
275
|
+
nonce: "u64",
|
|
276
|
+
receiverId: "string",
|
|
277
|
+
blockHash: { array: { type: "u8", len: 32 } },
|
|
278
|
+
actions: { array: { type: this.Action } },
|
|
279
|
+
},
|
|
280
|
+
};
|
|
281
|
+
SignedTransaction = {
|
|
282
|
+
struct: {
|
|
283
|
+
transaction: this.Transaction,
|
|
284
|
+
signature: this.Signature,
|
|
285
|
+
},
|
|
286
|
+
};
|
|
287
|
+
})();
|
package/src/utils.js
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import {
|
|
2
|
+
binary_to_base58 as toBase58,
|
|
3
|
+
base58_to_binary as fromBase58,
|
|
4
|
+
} from "base58-js";
|
|
5
|
+
|
|
6
|
+
export { toBase58, fromBase58 };
|
|
7
|
+
|
|
8
|
+
const LsPrefix = "__fastnear_";
|
|
9
|
+
|
|
10
|
+
export function toBase64(data) {
|
|
11
|
+
return Buffer.from(data).toString("base64");
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export function fromBase64(data) {
|
|
15
|
+
return Buffer.from(data, "base64");
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export function lsSet(key, value) {
|
|
19
|
+
if (value === null || value === undefined) {
|
|
20
|
+
localStorage.removeItem(LsPrefix + key);
|
|
21
|
+
} else {
|
|
22
|
+
localStorage.setItem(LsPrefix + key, JSON.stringify(value));
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export function lsGet(key) {
|
|
27
|
+
const value = localStorage.getItem(LsPrefix + key);
|
|
28
|
+
try {
|
|
29
|
+
return JSON.parse(value);
|
|
30
|
+
} catch (e) {
|
|
31
|
+
return null;
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export function deepCopy(obj) {
|
|
36
|
+
return JSON.parse(JSON.stringify(obj));
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export function tryParseJson(...args) {
|
|
40
|
+
try {
|
|
41
|
+
return JSON.parse(args[0]);
|
|
42
|
+
} catch {
|
|
43
|
+
if (args.length > 1) {
|
|
44
|
+
return args[1];
|
|
45
|
+
}
|
|
46
|
+
return value;
|
|
47
|
+
}
|
|
48
|
+
}
|