-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathaddCap.js
More file actions
81 lines (71 loc) · 2.33 KB
/
addCap.js
File metadata and controls
81 lines (71 loc) · 2.33 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
const fcl = require("@onflow/fcl");
const t = require("@onflow/types");
const { SHA3 } = require("sha3");
require('dotenv').config()
console.log(process.env)
var EC = require('elliptic').ec;
var ec = new EC('p256');
fcl.config()
.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 CONTRACT_NAME = process.env.CONTRACT_NAME;
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")
}
const hash = (message) => {
const sha = new SHA3(256);
sha.update(Buffer.from(message, "hex"));
return sha.digest();
}
async function authorizationFunction(account) {
return {
...account,
tempId: `${ADDRESS}-${KEY_ID}`,
addr: fcl.sansPrefix(ADDRESS),
keyId: Number(KEY_ID),
signingFunction: async (signable) => {
return {
addr: fcl.withPrefix(ADDRESS),
keyId: Number(KEY_ID),
signature: sign(signable.message)
}
}
}
}
async function performTransaction() {
const addr = process.env.RECIPIENT_ADDRESS
const transactionId = await fcl.mutate({
cadence: `
import Tickets from 0x24466f7fc36e3388
transaction(address: Address) {
prepare(signer: AuthAccount) {
let capabilityReceiver: Capability<&Tickets.Admin> = signer.getCapability<&Tickets.Admin>(Tickets.AdminPrivatePath)
let account = getAccount(address)
let capReceiverVault = account.getCapability<&Tickets.CapabilityReceiverVault{Tickets.IProxyCapabilityReceiverPublic}>(Tickets.CapabilityReceiverVaultPublicPath).borrow()
?? panic("Could not borrow CapabilityReceiverVault Capability.")
capReceiverVault.deposit(cap: capabilityReceiver)
}
}
`,
args: (arg, t) => [
arg(addr, t.Address)
],
payer: authorizationFunction,
proposer: authorizationFunction,
authorizations: [authorizationFunction],
limit: 999
});
console.log({transactionId});
fcl.tx(transactionId).subscribe(res => {
console.log(res);
})
}
performTransaction();