-
Notifications
You must be signed in to change notification settings - Fork 370
/
Copy pathNearLib.ts
437 lines (360 loc) · 11.7 KB
/
NearLib.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
import {
InMemorySigner,
providers,
keyStores as nearKeyStores,
transactions as nearTransactions,
utils
} from 'near-api-js'
import { AccessKeyView } from 'near-api-js/lib/providers/provider'
import { web3wallet } from '@/utils/WalletConnectUtil'
import { NEAR_TEST_CHAINS, TNearChain } from '@/data/NEARData'
import { Schema, serialize } from 'borsh'
const MAX_ACCOUNTS = 2
interface Account {
accountId: string
publicKey: string
}
interface Transaction {
signerId: string
receiverId: string
actions: Array<nearTransactions.Action>
}
interface CreateTransactionsParams {
chainId: string
transactions: Array<Transaction>
}
interface GetAccountsParams {
topic: string
}
interface SignInParams {
chainId: string
topic: string
permission: nearTransactions.FunctionCallPermission
accounts: Array<Account>
}
interface SignOutParams {
chainId: string
topic: string
accounts: Array<Account>
}
interface SignTransactionsParams {
chainId: string
topic: string
transactions: Array<nearTransactions.Transaction>
}
interface SignAndSendTransactionParams {
chainId: string
topic: string
transaction: nearTransactions.Transaction
}
interface SignAndSendTransactionsParams {
chainId: string
topic: string
transactions: Array<nearTransactions.Transaction>
}
export interface SignMessageParamsNEP {
message: string
recipient: string
nonce: Buffer
callbackUrl?: string
state?: string
}
interface SignMessageParams {
chainId: string
messageParams: SignMessageParamsNEP & {
accountId?: string
}
}
interface SignedMessage {
accountId: string
publicKey: string
signature: string
state?: string
}
export class MessagePayload {
tag: number
message: string
nonce: Buffer
recipient: string
callbackUrl?: string
constructor(data: SignMessageParamsNEP) {
// The tag's value is a hardcoded value as per
// defined in the NEP [NEP413](https://github.com/near/NEPs/blob/master/neps/nep-0413.md)
this.tag = 2147484061
this.message = data.message
this.nonce = data.nonce
this.recipient = data.recipient
if (data.callbackUrl) {
this.callbackUrl = data.callbackUrl
}
}
}
export const payloadSchema: Schema = {
struct: {
tag: 'u32',
message: 'string',
nonce: { array: { type: 'u8', len: 32 } },
recipient: 'string',
callbackUrl: { option: 'string' }
}
}
export class NearWallet {
private networkId: string
private keyStore: nearKeyStores.KeyStore
static async init(networkId: string) {
const keyStore = new nearKeyStores.BrowserLocalStorageKeyStore()
const accounts = await keyStore.getAccounts(networkId)
for (let i = 0; i < Math.max(MAX_ACCOUNTS - accounts.length, 0); i += 1) {
const { accountId, keyPair } = await NearWallet.createDevAccount()
await keyStore.setKey(networkId, accountId, keyPair)
}
return new NearWallet(networkId, keyStore)
}
static async createDevAccount() {
const keyPair = utils.KeyPair.fromRandom('ed25519')
const randomNumber = Math.floor(
Math.random() * (99999999999999 - 10000000000000) + 10000000000000
)
const accountId = `dev-${Date.now()}-${randomNumber}.testnet`
const publicKey = keyPair.getPublicKey().toString()
fetch(`https://helper.testnet.near.org/account`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
newAccountId: accountId,
newAccountPublicKey: publicKey
})
}).catch(error => {
console.error('Failed to create NEAR dev account: ', error)
})
return {
accountId,
keyPair
}
}
private constructor(networkId: string, keyStore: nearKeyStores.KeyStore) {
this.networkId = networkId
this.keyStore = keyStore
}
getKeyStore() {
return this.keyStore
}
// Retrieve all imported accounts from wallet.
async getAllAccounts(): Promise<Array<Account>> {
const accountIds = await this.keyStore.getAccounts(this.networkId)
return Promise.all(
accountIds.map(async accountId => {
const keyPair = await this.keyStore.getKey(this.networkId, accountId)
return {
accountId,
publicKey: keyPair.getPublicKey().toString()
}
})
)
}
private isAccountsValid(topic: string, accounts: Array<{ accountId: string }>) {
const session = web3wallet.engine.signClient.session.get(topic)
const validAccountIds = session.namespaces.near.accounts.map(accountId => {
return accountId.split(':')[2]
})
return accounts.every(({ accountId }) => {
return validAccountIds.includes(accountId)
})
}
private isTransactionsValid(topic: string, transactions: Array<nearTransactions.Transaction>) {
const accounts = transactions.map(({ signerId }) => ({ accountId: signerId }))
return this.isAccountsValid(topic, accounts)
}
async createTransactions({
chainId,
transactions
}: CreateTransactionsParams): Promise<Array<nearTransactions.Transaction>> {
const provider = new providers.JsonRpcProvider({
url: NEAR_TEST_CHAINS[chainId as TNearChain].rpc
})
const txs: Array<nearTransactions.Transaction> = []
const [block, accounts] = await Promise.all([
provider.block({ finality: 'final' }),
this.getAllAccounts()
])
for (let i = 0; i < transactions.length; i += 1) {
const transaction = transactions[i]
const account = accounts.find(x => x.accountId === transaction.signerId)
if (!account) {
throw new Error('Invalid signer id')
}
const accessKey = await provider.query<AccessKeyView>({
request_type: 'view_access_key',
finality: 'final',
account_id: transaction.signerId,
public_key: account.publicKey
})
const nonce = BigInt(accessKey.nonce) + BigInt(i) + 1n
txs.push(
nearTransactions.createTransaction(
transaction.signerId,
utils.PublicKey.from(account.publicKey),
transaction.receiverId,
nonce,
transaction.actions,
utils.serialize.base_decode(block.header.hash)
)
)
}
return txs
}
async getAccounts({ topic }: GetAccountsParams): Promise<Array<Account>> {
const session = web3wallet.engine.signClient.session.get(topic)
return Promise.all(
session.namespaces.near.accounts.map(async account => {
const accountId = account.split(':')[2]
const keyPair = await this.keyStore.getKey(this.networkId, accountId)
return {
accountId,
publicKey: keyPair.getPublicKey().toString()
}
})
)
}
async signIn({ chainId, topic, permission, accounts }: SignInParams): Promise<Array<Account>> {
if (!this.isAccountsValid(topic, accounts)) {
throw new Error('Invalid accounts')
}
const result: Array<Account> = []
for (let i = 0; i < accounts.length; i += 1) {
const account = accounts[i]
try {
const [transaction] = await this.createTransactions({
chainId,
transactions: [
{
signerId: account.accountId,
receiverId: account.accountId,
actions: [
nearTransactions.addKey(
utils.PublicKey.from(account.publicKey),
nearTransactions.functionCallAccessKey(
permission.receiverId,
permission.methodNames,
permission.allowance
)
)
]
}
]
})
await this.signAndSendTransaction({ chainId, topic, transaction })
result.push(account)
} catch (err) {
console.log(`Failed to create FunctionCall access key for ${account.accountId}`)
console.error(err)
}
}
return result
}
async signOut({ chainId, topic, accounts }: SignOutParams): Promise<Array<Account>> {
if (!this.isAccountsValid(topic, accounts)) {
throw new Error('Invalid accounts')
}
const result: Array<Account> = []
for (let i = 0; i < accounts.length; i += 1) {
const account = accounts[i]
try {
const [transaction] = await this.createTransactions({
chainId,
transactions: [
{
signerId: account.accountId,
receiverId: account.accountId,
actions: [nearTransactions.deleteKey(utils.PublicKey.from(account.publicKey))]
}
]
})
await this.signAndSendTransaction({ chainId, topic, transaction })
} catch (err) {
console.log(`Failed to remove FunctionCall access key for ${account.accountId}`)
console.error(err)
result.push(account)
}
}
return result
}
async signTransactions({
chainId,
topic,
transactions
}: SignTransactionsParams): Promise<Array<nearTransactions.SignedTransaction>> {
const networkId = chainId.split(':')[1]
const signer = new InMemorySigner(this.keyStore)
const signedTxs: Array<nearTransactions.SignedTransaction> = []
if (!this.isTransactionsValid(topic, transactions)) {
throw new Error('Invalid transactions')
}
for (let i = 0; i < transactions.length; i += 1) {
const transaction = transactions[i]
const [, signedTx] = await nearTransactions.signTransaction(
transaction,
signer,
transaction.signerId,
networkId
)
signedTxs.push(signedTx)
}
return signedTxs
}
async signAndSendTransaction({
chainId,
topic,
transaction
}: SignAndSendTransactionParams): Promise<providers.FinalExecutionOutcome> {
const provider = new providers.JsonRpcProvider({
url: NEAR_TEST_CHAINS[chainId as TNearChain].rpc
})
const [signedTx] = await this.signTransactions({
chainId,
topic,
transactions: [transaction]
})
return provider.sendTransaction(signedTx)
}
async signAndSendTransactions({
chainId,
topic,
transactions
}: SignAndSendTransactionsParams): Promise<Array<providers.FinalExecutionOutcome>> {
const provider = new providers.JsonRpcProvider({
url: NEAR_TEST_CHAINS[chainId as TNearChain].rpc
})
const signedTxs = await this.signTransactions({ chainId, topic, transactions })
const results: Array<providers.FinalExecutionOutcome> = []
for (let i = 0; i < signedTxs.length; i += 1) {
const signedTx = signedTxs[i]
results.push(await provider.sendTransaction(signedTx))
}
return results
}
async signMessage({ chainId, messageParams }: SignMessageParams): Promise<SignedMessage> {
const { message, nonce, recipient, callbackUrl, state, accountId } = messageParams
const nonceArray = Buffer.from(nonce)
if (nonceArray.length !== 32) {
throw Error('Expected nonce to be a 32 bytes buffer')
}
const accounts = await this.getAllAccounts()
const account = accounts.find(acc => acc.accountId === accountId)
// If no accountId is provided in params default to the first accountId in accounts.
// in a real wallet it would default to the `active/selected` account
// this is because we should be able to use `signMessage` without `signIn`.
const accId = account ? account.accountId : accounts[0].accountId
const signer = new InMemorySigner(this.getKeyStore())
const networkId = chainId.split(':')[1]
// Create the message payload and sign it
const payload = new MessagePayload({ message, nonce: nonceArray, recipient, callbackUrl })
const encodedPayload = serialize(payloadSchema, payload)
const signed = await signer.signMessage(encodedPayload, accId, networkId)
return {
accountId: accId,
publicKey: signed.publicKey.toString(),
signature: Buffer.from(signed.signature).toString('base64')
}
}
}