/
flow.ts
494 lines (466 loc) Β· 20.6 KB
/
flow.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
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
// @ts-ignore
import * as fcl from '@onflow/fcl';
// @ts-ignore
import * as types from '@onflow/types';
// @ts-ignore
import {ECDSA_secp256k1, encodeKey, SHA3_256,} from '@onflow/util-encode-key';
import * as elliptic from 'elliptic';
import {SHA3} from 'sha3';
import {flowBroadcastTx, flowGetSignKey, flowSignWithKey} from '../blockchain';
import {validateBody} from '../connector/tatum';
import {FLOW_MAINNET_ADDRESSES, FLOW_TESTNET_ADDRESSES} from '../constants';
import {Currency, FlowArgs, FlowBurnNft, FlowMintMultipleNft, FlowMintNft, FlowTransferNft, TransactionKMS, TransferFlow, TransferFlowCustomTx} from '../model';
import {generatePrivateKeyFromMnemonic} from '../wallet';
import {
burnFlowNftTokenTxTemplate,
metadataFlowNftTokenScript,
mintFlowMultipleNftTokenTxTemplate,
mintFlowNftTokenTxTemplate,
prepareAddPublicKeyToAccountTxTemplate,
prepareCreateAccountWithFUSDFromPublicKeyTxTemplate,
prepareTransferFlowTxTemplate,
tokenByAddressFlowNftTokenScript,
transferFlowNftTokenTxTemplate
} from './flowTransaction';
export enum FlowTxType {
CREATE_ACCOUNT,
ADD_PK_TO_ACCOUNT,
TRANSFER,
DEPLOY_NFT,
MINT_NFT,
MINT_MULTIPLE_NFT,
BURN_NFT,
TRANSFER_NFT,
CUSTOM_TX,
}
interface Account {
addr?: string;
}
interface AccountSignature {
addr: string;
keyId: number;
signature: string;
}
interface AccountAuthorization {
tempId: string;
addr: string;
keyId: number;
signingFunction: (data: { message: string }) => AccountSignature;
}
type Transaction = {
code: string
args: FlowArgs[]
proposer: AccountAuthorizer
authorizations: AccountAuthorizer[]
payer: AccountAuthorizer,
keyHash?: string,
}
type TransactionResult = {
id: string
error: string
events: { data: any }[]
}
type AccountAuthorizer = (
account?: Account
) => Promise<AccountAuthorization>
export const flowSign = (pk: string, msg: Buffer) => {
const keyPair = new elliptic.ec('secp256k1').keyFromPrivate(pk);
const signature = keyPair.sign(new SHA3(256).update(msg).digest());
const r = signature.r.toArrayLike(
Buffer,
'be',
32
);
const s = signature.s.toArrayLike(
Buffer,
'be',
32
);
return Buffer.concat([r, s]).toString('hex')
}
export const getFlowSigner = (
pk: string,
address: string,
keyId = 0,
) => {
return {
signer: (account: any) => {
return {
...account,
tempId: `${address}-${keyId}`,
addr: fcl.sansPrefix(address),
keyId: Number(keyId),
signingFunction: async (data: any) => {
return {
addr: fcl.withPrefix(address),
keyId: Number(keyId),
signature: flowSign(pk, Buffer.from(data.message, 'hex')),
};
}
};
}
};
};
export const getFlowApiSigner = (isPayer: boolean) => {
const keyHash = Date.now();
const signer = async (account: any) => {
const {address, keyId} = await flowGetSignKey(isPayer);
if (!isPayer) {
process.env[`FLOW_PROPOSAL_KEY_${keyHash}`] = `${keyId}`;
}
return {
...account,
tempId: `${address}-${keyId}`,
addr: fcl.sansPrefix(address),
keyId,
signingFunction: async (data: { message: string }) => {
return {
addr: fcl.withPrefix(address),
keyId: Number(keyId),
signature: (await flowSignWithKey(data.message, isPayer)).signature,
};
}
};
};
return {signer, keyHash: `FLOW_PROPOSAL_KEY_${keyHash}`};
};
const sendTransaction = async (testnet: boolean, {
code,
args,
proposer,
authorizations,
payer,
keyHash,
}: Transaction): Promise<TransactionResult> => {
fcl.config().put('accessNode.api', testnet ? 'https://rest-testnet.onflow.org' : 'https://rest-mainnet.onflow.org');
let response;
try {
response = await fcl.send([
fcl.transaction(code),
fcl.args(args.map(arg => fcl.arg(arg.type === 'UInt64' ? parseInt(arg.value as string) : arg.value,
arg.type === 'Array' ? types[arg.type](types[arg.subType]) : types[arg.type]))),
fcl.proposer(proposer),
fcl.authorizations(authorizations),
fcl.payer(payer),
fcl.limit(9999),
]);
} catch (e) {
try {
if (keyHash) {
await flowBroadcastTx('', undefined, keyHash ? parseInt((process.env[keyHash] || '0') as string) : undefined);
delete process.env[keyHash];
}
// eslint-disable-next-line no-empty
} catch (_) {
}
throw e;
}
const {transactionId} = response;
try {
const {error, events} = await fcl.tx(response).onceSealed();
return {
id: transactionId,
error,
events,
};
} finally {
try {
if (keyHash) {
await flowBroadcastTx(transactionId, undefined, keyHash ? parseInt((process.env[keyHash] || '0') as string) : undefined);
delete process.env[keyHash];
}
// eslint-disable-next-line no-empty
} catch (_) {
}
}
};
const sendScript = async (testnet: boolean, code: string, args: FlowArgs[]) => {
fcl.config().put('accessNode.api', testnet ? 'https://rest-testnet.onflow.org' : 'https://rest-mainnet.onflow.org');
const response = await fcl.send([
fcl.script(code),
fcl.args(args.map(arg => fcl.arg(arg.type === 'UInt64' ? parseInt(arg.value as string) : arg.value, types[arg.type]))),
]);
return fcl.decode(response);
};
export const flowSignKMSTransaction = async (tx: TransactionKMS, privateKeys: string[], testnet: boolean) => {
if (tx.chain !== Currency.FLOW) {
throw Error('Unsupported chain.');
}
const {type, body}: { type: FlowTxType, apiManagedProposal: boolean, body: any } = JSON.parse(tx.serializedTransaction);
switch (type) {
case FlowTxType.CREATE_ACCOUNT:
return await flowCreateAccountFromPublicKey(testnet, body.publicKey, body.account, privateKeys[0])
case FlowTxType.ADD_PK_TO_ACCOUNT:
return await flowAddPublicKeyToAccount(testnet, body.publicKey, body.account, privateKeys[0])
case FlowTxType.TRANSFER:
return await flowSendTransaction(testnet, {...body, privateKey: privateKeys[0]});
case FlowTxType.TRANSFER_NFT:
return await sendFlowNftTransferToken(testnet, {...body, privateKey: privateKeys[0]});
case FlowTxType.MINT_NFT:
return await sendFlowNftMintToken(testnet, {...body, privateKey: privateKeys[0]});
case FlowTxType.MINT_MULTIPLE_NFT:
return await sendFlowNftMintMultipleToken(testnet, {...body, privateKey: privateKeys[0]});
case FlowTxType.BURN_NFT:
return await sendFlowNftBurnToken(testnet, {...body, privateKey: privateKeys[0]});
default:
return await flowSendCustomTransaction(testnet, {...body, privateKey: privateKeys[0]});
}
};
/**
* Create account on the FLOW network. It automatically creates 100 0-weight proposal keys, which are managed by Tatum API - index 1-100.
* Main 1000 weight authorizer key is stored as a first one on index 0.
* @param testnet if we use testnet or not
* @param publicKey public key to assign to address as authorizer (1000 weight) key
* @param signerAddress address of the authorizer creator of the address on the chain
* @param signerPrivateKey private key of the authorizer creator of the address on the chain
* @param proposer function to obtain proposer key from
* @param payer function to obtain payer key from
*/
export const flowCreateAccountFromPublicKey = async (testnet: boolean, publicKey: string, signerAddress: string, signerPrivateKey: string,
proposer?: (isPayer: boolean) => any, payer?: (isPayer: boolean) => any):
Promise<{ txId: string, address: string }> => {
const code = prepareCreateAccountWithFUSDFromPublicKeyTxTemplate(testnet);
const encodedPublicKey = encodeKey(
publicKey,
ECDSA_secp256k1,
SHA3_256,
1000
);
const args = [{type: 'String', value: encodedPublicKey}];
const auth = getFlowSigner(signerPrivateKey, signerAddress).signer;
const {signer: proposalSigner, keyHash} = proposer ? proposer(false) : getFlowApiSigner(false);
const result = await sendTransaction(testnet, {
code, args,
proposer: proposer ? proposer(false) : proposalSigner,
authorizations: [auth],
payer: payer ? payer(true) : getFlowApiSigner(true).signer,
keyHash,
});
if (result.error) {
throw new Error(result.error);
}
return {txId: result.id, address: result.events.find((e: any) => e.type === 'flow.AccountCreated')?.data.address};
};
/**
* Add public key to existing blockchain address with defined weight
* @param testnet
* @param publicKey key to add
* @param signerAddress address of the authorizer key
* @param signerPrivateKey key of the authorize key
* @param weight defaults to 1000 - weight of the key
* @param proposer function to obtain proposer key from
* @param payer function to obtain payer key from
*/
export const flowAddPublicKeyToAccount = async (testnet: boolean, publicKey: string, signerAddress: string, signerPrivateKey: string, weight = 0,
proposer?: (...args: any[]) => any, payer?: (...args: any[]) => any):
Promise<{ txId: string, address: string }> => {
const code = prepareAddPublicKeyToAccountTxTemplate();
const encodedPublicKey = encodeKey(
publicKey,
ECDSA_secp256k1,
SHA3_256,
weight
);
const args = [{type: 'String', value: encodedPublicKey}];
const auth = getFlowSigner(signerPrivateKey, signerAddress).signer;
const {signer: proposalSigner, keyHash} = proposer ? proposer(false) : getFlowApiSigner(false);
const result = await sendTransaction(testnet, {
code, args,
proposer: proposer ? proposer(false) : proposalSigner,
authorizations: [auth],
keyHash,
payer: payer ? payer(true) : getFlowApiSigner(true).signer,
});
if (result.error) {
throw new Error(result.error);
}
return {txId: result.id, address: result.events[0].data.address};
};
export const getFlowNftMetadata = async (testnet: boolean, account: string, id: string, tokenType: string) => {
const code = metadataFlowNftTokenScript(testnet);
const args = [{type: 'Address', value: account}, {type: 'UInt64', value: id}, {type: 'String', value: tokenType}];
return await sendScript(testnet, code, args);
};
export const getFlowNftTokenByAddress = async (testnet: boolean, account: string, tokenType: string) => {
const code = tokenByAddressFlowNftTokenScript(testnet);
const args = [{type: 'Address', value: account}, {type: 'String', value: tokenType}];
return await sendScript(testnet, code, args);
};
/**
* Send Flow NFT mint token transaction to the blockchain. This method broadcasts signed transaction to the blockchain.
* This operation is irreversible.
* @param testnet
* @param body content of the transaction to broadcast
* @param proposer function to obtain proposer key from
* @param payer function to obtain payer key from
* @returns txId id of the transaction in the blockchain
*/
export const sendFlowNftMintToken = async (testnet: boolean, body: FlowMintNft, proposer?: (isPayer: boolean) => any, payer?: (isPayer: boolean) => any):
Promise<{ txId: string, tokenId: string }> => {
await validateBody(body, FlowMintNft);
const code = mintFlowNftTokenTxTemplate(testnet);
const {url, contractAddress: tokenType, to, mnemonic, index, account, privateKey} = body;
const args = [{type: 'Address', value: to}, {type: 'String', value: url}, {type: 'String', value: tokenType}];
const pk = (mnemonic && index && index >= 0) ? await generatePrivateKeyFromMnemonic(Currency.FLOW, testnet, mnemonic, index as number) : privateKey as string;
const auth = getFlowSigner(pk, account).signer;
const {signer: proposalSigner, keyHash} = proposer ? proposer(false) : getFlowApiSigner(false);
const result = await sendTransaction(testnet, {
code, args,
proposer: proposer ? proposer(false) : proposalSigner,
authorizations: [auth],
keyHash,
payer: payer ? payer(true) : getFlowApiSigner(true).signer,
});
if (result.error) {
throw new Error(result.error);
}
return {txId: result.id, tokenId: `${result.events.find((e: any) => e.type.includes('TatumMultiNFT.Minted'))?.data.id}`};
};
/**
* Send Flow NFT mint multiple tokens transaction to the blockchain. This method broadcasts signed transaction to the blockchain.
* This operation is irreversible.
* @param testnet
* @param body content of the transaction to broadcast
* @param proposer function to obtain proposer key from
* @param payer function to obtain payer key from
* @returns txId id of the transaction in the blockchain
*/
export const sendFlowNftMintMultipleToken = async (testnet: boolean, body: FlowMintMultipleNft, proposer?: (isPayer: boolean) => any, payer?: (isPayer: boolean) => any):
Promise<{ txId: string, tokenId: number[] }> => {
await validateBody(body, FlowMintMultipleNft);
const code = mintFlowMultipleNftTokenTxTemplate(testnet);
const {url, contractAddress: tokenType, to, mnemonic, index, account, privateKey} = body;
const args = [{type: 'Array', subType: 'Address', value: to}, {type: 'Array', subType: 'String', value: url}, {type: 'String', value: tokenType}];
const pk = (mnemonic && index && index >= 0) ? await generatePrivateKeyFromMnemonic(Currency.FLOW, testnet, mnemonic, index as number) : privateKey as string;
const {signer: proposalSigner, keyHash} = proposer ? proposer(false) : getFlowApiSigner(false);
const auth = getFlowSigner(pk, account).signer;
const result = await sendTransaction(testnet, {
code, args, proposer: proposer ? proposer(false) : proposalSigner, authorizations: [auth],
payer: payer ? payer(true) : getFlowApiSigner(true).signer,
keyHash,
});
if (result.error) {
throw new Error(result.error);
}
return {txId: result.id, tokenId: result.events.filter((e: any) => e.type.includes('TatumMultiNFT.Minted')).map(e => e.data.id)};
};
/**
* Send Flow NFT transfer token transaction to the blockchain. This method broadcasts signed transaction to the blockchain.
* This operation is irreversible.
* @param testnet
* @param body content of the transaction to broadcast
* @param proposer function to obtain proposer key from
* @param payer function to obtain payer key from
* @returns {txId: string, events: any[]} id of the transaction in the blockchain and events this tx produced
*/
export const sendFlowNftTransferToken = async (testnet: boolean, body: FlowTransferNft, proposer?: (isPayer: boolean) => any, payer?: (isPayer: boolean) => any):
Promise<{ txId: string }> => {
await validateBody(body, FlowTransferNft);
const code = transferFlowNftTokenTxTemplate(testnet);
const {tokenId, to, mnemonic, index, account, privateKey} = body;
const args = [{type: 'Address', value: to}, {type: 'UInt64', value: tokenId}];
const pk = (mnemonic && index && index >= 0) ? await generatePrivateKeyFromMnemonic(Currency.FLOW, testnet, mnemonic, index as number) : privateKey as string;
const {signer: proposalSigner, keyHash} = proposer ? proposer(false) : getFlowApiSigner(false);
const auth = getFlowSigner(pk, account).signer;
const result = await sendTransaction(testnet, {
code, args, proposer: proposer ? proposer(false) : proposalSigner, authorizations: [auth],
payer: payer ? payer(true) : getFlowApiSigner(true).signer,
keyHash,
});
if (result.error) {
throw new Error(result.error);
}
return {txId: result.id};
};
/**
* Send Flow NFT burn token transaction to the blockchain. This method broadcasts signed transaction to the blockchain.
* This operation is irreversible.
* @param testnet
* @param body content of the transaction to broadcast
* @param proposer function to obtain proposer key from
* @param payer function to obtain payer key from
* @returns txId id of the transaction in the blockchain
*/
export const sendFlowNftBurnToken = async (testnet: boolean, body: FlowBurnNft, proposer?: (isPayer: boolean) => any, payer?: (isPayer: boolean) => any):
Promise<{ txId: string }> => {
await validateBody(body, FlowBurnNft);
const code = burnFlowNftTokenTxTemplate(testnet);
const {tokenId, contractAddress: tokenType, mnemonic, index, account, privateKey} = body;
const args = [{type: 'UInt64', value: tokenId}, {type: 'String', value: tokenType}];
const pk = (mnemonic && index && index >= 0) ? await generatePrivateKeyFromMnemonic(Currency.FLOW, testnet, mnemonic, index as number) : privateKey as string;
const {signer: proposalSigner, keyHash} = proposer ? proposer(false) : getFlowApiSigner(false);
const auth = getFlowSigner(pk, account).signer;
const result = await sendTransaction(testnet, {
code, args, proposer: proposer ? proposer(false) : proposalSigner, authorizations: [auth],
payer: payer ? payer(true) : getFlowApiSigner(true).signer,
keyHash,
});
if (result.error) {
throw new Error(result.error);
}
return {txId: result.id};
};
/**
* Send custom transaction to the FLOW network
* @param testnet
* @param body content of the transaction to broadcast
* @param proposer function to obtain proposer key from
* @param payer function to obtain payer key from
* @returns txId id of the transaction in the blockchain
*/
export const flowSendCustomTransaction = async (testnet: boolean, body: TransferFlowCustomTx, proposer?: (isPayer: boolean) => any, payer?: (isPayer: boolean) => any):
Promise<{ txId: string, events: any[] }> => {
await validateBody(body, TransferFlowCustomTx);
const pk = body.privateKey || await generatePrivateKeyFromMnemonic(Currency.FLOW, testnet, body.mnemonic as string, body.index as number);
const auth = getFlowSigner(pk, body.account).signer;
const {signer: proposalSigner, keyHash} = proposer ? proposer(false) : getFlowApiSigner(false);
const result = await sendTransaction(testnet, {
code: body.transaction, args: body.args,
proposer: proposer ? proposer(false) : proposalSigner,
authorizations: [auth],
keyHash,
payer: payer ? payer(true) : getFlowApiSigner(true).signer
});
if (result.error) {
throw new Error(result.error);
}
return {txId: result.id, events: result.events};
};
/**
* Send FLOW or FUSD from account to account.
* @param testnet
* @param body content of the transaction to broadcast
* @param proposer function to obtain proposer key from
* @param payer function to obtain payer key from
* @returns txId id of the transaction in the blockchain
*/
export const flowSendTransaction = async (testnet: boolean, body: TransferFlow, proposer?: (isPayer: boolean) => any, payer?: (isPayer: boolean) => any):
Promise<{ txId: string }> => {
await validateBody(body, TransferFlow);
let tokenAddress;
let tokenName;
let tokenStorage;
if (body.currency === Currency.FLOW) {
tokenAddress = testnet ? FLOW_TESTNET_ADDRESSES.FlowToken : FLOW_MAINNET_ADDRESSES.FlowToken;
tokenName = 'FlowToken';
tokenStorage = 'flowToken';
} else {
tokenAddress = testnet ? FLOW_TESTNET_ADDRESSES.FUSD : FLOW_MAINNET_ADDRESSES.FUSD;
tokenName = 'FUSD';
tokenStorage = 'fusd';
}
const code = prepareTransferFlowTxTemplate(testnet, tokenAddress, tokenName, tokenStorage);
const args = [{value: parseFloat(body.amount).toFixed(8), type: 'UFix64'}, {value: body.to, type: 'Address'}];
const pk = body.privateKey || await generatePrivateKeyFromMnemonic(Currency.FLOW, testnet, body.mnemonic as string, body.index as number);
const {signer: proposalSigner, keyHash} = proposer ? proposer(false) : getFlowApiSigner(false);
const auth = getFlowSigner(pk, body.account).signer;
const result = await sendTransaction(testnet, {
code, args, proposer: proposer ? proposer(false) : proposalSigner, authorizations: [auth],
payer: payer ? payer(true) : getFlowApiSigner(true).signer,
keyHash,
});
if (result.error) {
throw new Error(result.error);
}
return {txId: result.id};
};