-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsendFlow.js
More file actions
85 lines (75 loc) · 2.35 KB
/
sendFlow.js
File metadata and controls
85 lines (75 loc) · 2.35 KB
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
import { config, mutate, tx, sansPrefix, withPrefix } from "@onflow/fcl";
import { SHA3 } from "sha3";
import "dotenv/config";
import pkg from "elliptic";
const { ec: EC } = pkg;
const ec = new EC("p256");
config()
.put("flow.network", "mainnet")
.put("accessNode.api", "https://rest-mainnet.onflow.org");
// CHANGE THESE THINGS FOR YOU
const PRIVATE_KEY = process.env.PRIVATE_KEY;
const ADDRESS = process.env.ADDRESS;
const KEY_ID = parseInt(process.env.KEY_ID);
const hash = (message) => {
const sha = new SHA3(256);
sha.update(Buffer.from(message, "hex"));
return sha.digest();
};
const sign = (message) => {
const key = ec.keyFromPrivate(Buffer.from(PRIVATE_KEY, "hex"));
const sig = key.sign(hash(message)); // hashMsgHex -> hash
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");
};
async function authorizationFunction(account) {
return {
...account,
tempId: `${ADDRESS}-${KEY_ID}`,
addr: sansPrefix(ADDRESS),
keyId: Number(KEY_ID),
signingFunction: async (signable) => {
return {
addr: withPrefix(ADDRESS),
keyId: Number(KEY_ID),
signature: sign(signable.message),
};
},
};
}
async function sendTx() {
const addr = process.env.RECIPIENT_ADDRESS2;
const transactionId = await mutate({
cadence: `
import FungibleToken from 0xf233dcee88fe0abe
import FlowToken from 0x1654653399040a61
transaction(amount: UFix64, to: Address) {
let vault: @FlowToken.Vault
prepare(signer: auth(BorrowValue) &Account) {
self.vault <- signer.storage
.borrow<auth(FungibleToken.Withdraw) &{FungibleToken.Provider}>(from: /storage/flowTokenVault)!
.withdraw(amount: amount) as! @FlowToken.Vault
}
execute {
getAccount(to)
.capabilities
.get<&{FungibleToken.Receiver}>(/public/flowTokenReceiver)
.borrow()!
.deposit(from: <-self.vault)
}
}
`,
args: (arg, t) => [arg(13.55, t.UFix64), arg(addr, t.Address)],
payer: authorizationFunction,
proposer: authorizationFunction,
authorizations: [authorizationFunction],
limit: 999,
});
console.log({ transactionId });
tx(transactionId).subscribe((res) => {
console.log(res);
});
}
sendTx();