|
| 1 | +import fs from "node:fs"; |
| 2 | +import { config, sansPrefix, withPrefix, mutate, tx } from "@onflow/fcl"; |
| 3 | +import { SHA3 } from "sha3"; |
| 4 | +import pkg from "elliptic"; |
| 5 | +const { ec: EC } = pkg; |
| 6 | +const ec = new EC("secp256k1"); |
| 7 | + |
| 8 | +export const handler = async (event) => { |
| 9 | + console.log("Event", JSON.stringify(event, 3)); |
| 10 | + const input = event.input || {}; |
| 11 | + let gamerId = input.playerId ? parseInt(input.playerId) : 0; |
| 12 | + let message = input.message ? JSON.parse(input.message) : {}; |
| 13 | + let transaction = ""; |
| 14 | + |
| 15 | + if (input.type === "shooting_game_outcome") { |
| 16 | + transaction = ` |
| 17 | + import TestnetTest5 from 0x975b04756864e9ea |
| 18 | +
|
| 19 | + transaction(gamerId: UInt, outcome: Bool) { |
| 20 | + prepare(signer: auth(BorrowValue) &Account) { |
| 21 | + let admin = signer.storage.borrow<&TestnetTest5.Admin>(from: /storage/TestnetTest5Admin) |
| 22 | + ?? panic("Could not borrow reference to the Administrator Resource.") |
| 23 | + admin.shootingGameOutcome(gamerId: gamerId, outcome: outcome) |
| 24 | + } |
| 25 | + execute { |
| 26 | + log("success") |
| 27 | + } |
| 28 | + } |
| 29 | + `; |
| 30 | + } else if (input.type === "free_play") { |
| 31 | + transaction = ` |
| 32 | + import TestnetTest5 from 0x975b04756864e9ea |
| 33 | +
|
| 34 | + transaction(gamerId: UInt) { |
| 35 | + prepare(signer: auth(BorrowValue) &Account) { |
| 36 | + let admin = signer.storage.borrow<&TestnetTest5.Admin>(from: /storage/TestnetTest5Admin) |
| 37 | + ?? panic("Could not borrow reference to the Administrator Resource.") |
| 38 | + admin.useTipJarForFreePlay(gamerId: gamerId) |
| 39 | + } |
| 40 | + execute { |
| 41 | + log("success") |
| 42 | + } |
| 43 | + } |
| 44 | + `; |
| 45 | + } |
| 46 | + |
| 47 | + config({ |
| 48 | + "flow.network": "testnet", |
| 49 | + "accessNode.api": "https://rest-testnet.onflow.org", |
| 50 | + }); |
| 51 | + |
| 52 | + let txId; |
| 53 | + try { |
| 54 | + var IT_KEY_ID = 0; |
| 55 | + if (fs.existsSync("/tmp/sequence.txt")) { |
| 56 | + IT_KEY_ID = parseInt( |
| 57 | + fs.readFileSync("/tmp/sequence.txt", { encoding: "utf8" }) |
| 58 | + ); |
| 59 | + } else { |
| 60 | + IT_KEY_ID = 10; |
| 61 | + } |
| 62 | + IT_KEY_ID = !IT_KEY_ID || IT_KEY_ID >= 10 ? 0 : IT_KEY_ID + 1; |
| 63 | + fs.writeFileSync("/tmp/sequence.txt", IT_KEY_ID.toString()); |
| 64 | + console.log("IT_KEY_ID", IT_KEY_ID); |
| 65 | + |
| 66 | + const PRIVATE_KEY = fs.readFileSync("testnet-account.pkey", "utf8"); |
| 67 | + const ADDRESS = "0x975b04756864e9ea"; |
| 68 | + const KEY_ID = 0; |
| 69 | + |
| 70 | + const hash = (message) => { |
| 71 | + const sha = new SHA3(256); |
| 72 | + sha.update(Buffer.from(message, "hex")); |
| 73 | + return sha.digest(); |
| 74 | + }; |
| 75 | + |
| 76 | + const sign = (message) => { |
| 77 | + const key = ec.keyFromPrivate(Buffer.from(PRIVATE_KEY, "hex")); |
| 78 | + const sig = key.sign(hash(message)); // hashMsgHex -> hash |
| 79 | + const n = 32; |
| 80 | + const r = sig.r.toArrayLike(Buffer, "be", n); |
| 81 | + const s = sig.s.toArrayLike(Buffer, "be", n); |
| 82 | + return Buffer.concat([r, s]).toString("hex"); |
| 83 | + }; |
| 84 | + |
| 85 | + async function authFunction(account) { |
| 86 | + return { |
| 87 | + ...account, |
| 88 | + tempId: `${ADDRESS}-${KEY_ID}`, |
| 89 | + addr: sansPrefix(ADDRESS), |
| 90 | + keyId: Number(KEY_ID), |
| 91 | + signingFunction: async (signable) => { |
| 92 | + return { |
| 93 | + addr: withPrefix(ADDRESS), |
| 94 | + keyId: Number(KEY_ID), |
| 95 | + signature: sign(signable.message), |
| 96 | + }; |
| 97 | + }, |
| 98 | + }; |
| 99 | + } |
| 100 | + async function authFunctionForProposer(account) { |
| 101 | + return { |
| 102 | + ...account, |
| 103 | + tempId: `${ADDRESS}-${IT_KEY_ID}`, |
| 104 | + addr: sansPrefix(ADDRESS), |
| 105 | + keyId: Number(IT_KEY_ID), |
| 106 | + signingFunction: async (signable) => { |
| 107 | + return { |
| 108 | + addr: withPrefix(ADDRESS), |
| 109 | + keyId: Number(IT_KEY_ID), |
| 110 | + signature: sign(signable.message), |
| 111 | + }; |
| 112 | + }, |
| 113 | + }; |
| 114 | + } |
| 115 | + |
| 116 | + console.log("transaction", transaction, input); |
| 117 | + if (input.type === "shooting_game_outcome") { |
| 118 | + const outcome = message == "true" || message == true; |
| 119 | + txId = await mutate({ |
| 120 | + cadence: transaction, |
| 121 | + args: (arg, t) => [arg(gamerId, t.UInt), arg(outcome, t.Bool)], |
| 122 | + proposer: authFunctionForProposer, |
| 123 | + payer: authFunction, |
| 124 | + authorizations: [authFunction], |
| 125 | + limit: 999, |
| 126 | + }); |
| 127 | + console.log(`txId: ${txId}`); |
| 128 | + message = `Tx[shooting_game_outcome] is On Going.`; |
| 129 | + tx(txId).subscribe((res) => { |
| 130 | + console.log(res); |
| 131 | + }); |
| 132 | + } else if (input.type === "free_play") { |
| 133 | + txId = await mutate({ |
| 134 | + cadence: transaction, |
| 135 | + args: (arg, t) => [arg(gamerId, t.UInt)], |
| 136 | + proposer: authFunctionForProposer, |
| 137 | + payer: authFunction, |
| 138 | + authorizations: [authFunction], |
| 139 | + limit: 999, |
| 140 | + }); |
| 141 | + console.log(`txId: ${txId}`); |
| 142 | + message = `Tx[free_play] is On Going.`; |
| 143 | + tx(txId).subscribe((res) => { |
| 144 | + console.log(res); |
| 145 | + }); |
| 146 | + } |
| 147 | + |
| 148 | + return { |
| 149 | + id: new Date().getTime(), |
| 150 | + type: input.type || "", |
| 151 | + message: `${input.message} , txId: ${txId}`, |
| 152 | + playerId: gamerId, |
| 153 | + createdAt: new Date(), |
| 154 | + updatedAt: new Date(), |
| 155 | + }; |
| 156 | + } catch (error) { |
| 157 | + return { |
| 158 | + id: new Date().getTime(), |
| 159 | + type: "E:" + input.type, |
| 160 | + message: error.toString(), |
| 161 | + playerId: gamerId, |
| 162 | + createdAt: new Date(), |
| 163 | + updatedAt: new Date(), |
| 164 | + }; |
| 165 | + } |
| 166 | +}; |
0 commit comments