-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathflowoffchain.mjs
186 lines (150 loc) · 5.58 KB
/
flowoffchain.mjs
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
import fs from 'fs';
import path from 'path';
import fcl from '@onflow/fcl';
import elliptic from 'elliptic';
import {sha256} from 'js-sha256';
import { SHA3 } from 'sha3';
import {createSign, createHash, generateKeyPairSync} from 'node:crypto';
import * as eccrypto from 'eccrypto';
fcl.config().put('accessNode.api', 'http://127.0.0.1:8888');
fcl.config().put('0xProfile', '0xf8d6e0586b0a20c7');
fcl.config().put('Profile', 'f8d6e0586b0a20c7');
// Get block at height (uses builder function)
// const response = await fcl.send([fcl.getBlock(), fcl.atBlockHeight(1)]).then(fcl.decode);
// console.log(response);
// const account = await fcl.account("0xf8d6e0586b0a20c7");
// console.log(fcl.sansPrefix(account.address));
// console.log(fcl.withPrefix(account.address));
class FlowService {
constructor(address, privateKey, keyId, hashFun, curveName) {
this.signerFlowAddress = address;// signer address
this.signerPrivateKeyHex = privateKey;// signer private key
this.signerAccountIndex = keyId;// singer key index
this.ec = new elliptic.ec(curveName);
this.hashFunc = hashFun;
}
executeScripts = async ({ script, args }) => {
const response = await fcl.send([fcl.script`${script}`, fcl.args(args)]);
return await fcl.decode(response);
}
sendTx = async ({
transaction,
args,
}) => {
const response = await fcl.send([
fcl.transaction`
${transaction}
`,
fcl.args(args),
fcl.proposer(this.authzFn),
fcl.authorizations([this.authzFn]),
fcl.payer(this.authzFn),
fcl.limit(9999)
]);
return response;
};
authzFn = async (txAccount) => {
const user = await fcl.account(this.signerFlowAddress);
const key = user.keys[this.signerAccountIndex];
const pk = this.signerPrivateKeyHex;
return {
...txAccount,
tempId: `${user.address}-${key.index}`,
addr: fcl.sansPrefix(user.address),
keyId: Number(key.index),
signingFunction: async(signable) => {
return {
addr: fcl.withPrefix(user.address),
keyId: Number(key.index),
signature: this.sign2string(signable.message)
}
}
}
}
sign2string = (msg) => {
const key = this.ec.keyFromPrivate(Buffer.from(this.signerPrivateKeyHex, 'hex'));
const sig = key.sign(this.hashFunc(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');
};
sign2buffer = (msg) => {
const key = this.ec.keyFromPrivate(Buffer.from(this.signerPrivateKeyHex, 'hex'));
const sig = key.sign(this.hashFunc(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]);
};
}
async function createSubmittion() {
const fService = new FlowService();
const script = fs.readFileSync(
path.join(
process.cwd(),
'../scripts/addressTest.cdc'
),
'utf8'
);
const response = await fService.executeScripts(script, []);
console.log(response);
}
const ec = new elliptic.ec('p256');
const sha3_256FromString = (msg) => {
const sha = new SHA3(256);
sha.update(Buffer.from(msg, 'utf8'));
return sha.digest();
};
const sha3_256FromBytes = (msgBytes) => {
const sha = new SHA3(256);
sha.update(msgBytes);
return sha.digest();
}
function signWithKey(msg) {
const key = ec.keyFromPrivate(Buffer.from("69e7e51ead557351ade7a575e947c4d4bd19dd8a6cdf00c51f9c7f6f721b72dc", 'hex'));
const sig = key.sign(sha3_256Hash(msg));
const n = 32;
const r = sig.r.toArrayLike(Buffer, 'be', n);
const s = sig.s.toArrayLike(Buffer, 'be', n);
console.log(sig.recoveryParam);
return Buffer.concat([r, s]).toString('hex');
};
async function testSignature() {
const fService = new FlowService("0xf8d6e0586b0a20c7", "69e7e51ead557351ade7a575e947c4d4bd19dd8a6cdf00c51f9c7f6f721b72dc", 0, sha3_256FromString, "p256");
const signed = fService.sign2string('hello nika');
console.log(signed);
}
async function exampleHash() {
const msg2sign = "hello nika";
console.log(sha256(msg2sign));
console.log(sha3_256FromString(msg2sign).toString('hex'));
console.log(sha3_256FromBytes(Buffer.from(msg2sign, 'utf8')).toString('hex'));
}
async function signatureWithCrypto() {
const { privateKey, publicKey } = generateKeyPairSync('ec', {
namedCurve: 'P-256'
});
const privateKeyStr = privateKey.export({ format: 'pem', type: 'pkcs8' }).toString();
console.log(privateKeyStr);
const sign = createSign('SHA3-256');
sign.update('hello nika');
sign.end();
const signature = sign.sign(privateKey, 'hex');
console.log(signature);
}
async function signWithEccrypto() {
const privateKey = Buffer.from('69e7e51ead557351ade7a575e947c4d4bd19dd8a6cdf00c51f9c7f6f721b72dc', 'hex');
var msg = createHash("SHA3-256").update('hello nika').digest();
// console.log(msg.toString('hex'));
const signature = await eccrypto.sign(privateKey, msg);
console.log(signature.toString('hex'));
}
// await createSubmittion();
// await exampleHash();
// await testSignature();
// await signatureWithCrypto();
// await signWithEccrypto();
// const signed2 = signWithKey('hello nika');
// console.log(signed2);
export default FlowService;