This repository has been archived by the owner on Jul 8, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 213
/
flow.ts
99 lines (84 loc) · 2.37 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
import * as fcl from "@onflow/fcl";
import { ec as EC } from "elliptic";
import { SHA3 } from "sha3";
const ec: EC = new EC("p256");
class FlowService {
constructor(
private readonly minterFlowAddress: string,
private readonly minterPrivateKeyHex: string,
private readonly minterAccountIndex: string | number
) {}
authorizeMinter = () => {
return async (account: any = {}) => {
const user = await this.getAccount(this.minterFlowAddress);
const key = user.keys[this.minterAccountIndex];
const sign = this.signWithKey;
const pk = this.minterPrivateKeyHex;
return {
...account,
tempId: `${user.address}-${key.index}`,
addr: fcl.sansPrefix(user.address),
keyId: Number(key.index),
signingFunction: (signable) => {
return {
addr: fcl.withPrefix(user.address),
keyId: Number(key.index),
signature: sign(pk, signable.message),
};
},
};
};
};
getAccount = async (addr: string) => {
const { account } = await fcl.send([fcl.getAccount(addr)]);
return account;
};
private signWithKey = (privateKey: string, msg: string) => {
const key = ec.keyFromPrivate(Buffer.from(privateKey, "hex"));
const sig = key.sign(this.hashMsg(msg));
const n = 32;
const r = sig.r.toArrayLike(Buffer, "be", n);
const s = sig.s.toArrayLike(Buffer, "be", n);
return Buffer.concat([r, s]).toString("hex");
};
private hashMsg = (msg: string) => {
const sha = new SHA3(256);
sha.update(Buffer.from(msg, "hex"));
return sha.digest();
};
sendTx = async ({
transaction,
args,
proposer,
authorizations,
payer,
skipSeal
}: any): Promise<any> => {
const response = await fcl.mutate(
{
cadence: transaction,
args: (_arg, _t) => args,
proposer,
authorizations,
payer,
limit: 9999,
},
)
if (skipSeal) return response;
return await fcl.tx(response).onceSealed();
};
async executeScript<T>({ script, args }): Promise<T> {
return await fcl.query(
{
cadence: script,
args: (_arg, _t) => args,
},
);
}
async getLatestBlockHeight() {
const block = await fcl.send([fcl.getBlock(true)]);
const decoded = await fcl.decode(block);
return decoded.height;
}
}
export { FlowService };