Skip to content

Commit 2ca6a2c

Browse files
authored
Merge pull request #15 from temt-ceo/develop
β version complete.
2 parents e3883bf + c70deee commit 2ca6a2c

13 files changed

Lines changed: 6800 additions & 21 deletions

File tree

aws_lambda/mainnet/src.zip

-7 Bytes
Binary file not shown.

aws_lambda/mainnet/src/index.mjs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { config, sansPrefix, withPrefix, mutate, tx } from "@onflow/fcl";
33
import { SHA3 } from "sha3";
44
import pkg from "elliptic";
55
const { ec: EC } = pkg;
6-
const ec = new EC("secp256k1");
6+
const ec = new EC("p256");
77

88
export const handler = async (event) => {
99
console.log("Event", JSON.stringify(event, 3));
@@ -14,7 +14,7 @@ export const handler = async (event) => {
1414

1515
if (input.type === "shooting_game_outcome") {
1616
transaction = `
17-
import OragaESports from 0x975b04756864e9ea
17+
import OragaESports from 0xb576a3926d239682
1818
1919
transaction(gamerId: UInt, outcome: Bool) {
2020
prepare(signer: auth(BorrowValue) &Account) {
@@ -29,7 +29,7 @@ export const handler = async (event) => {
2929
`;
3030
} else if (input.type === "free_play") {
3131
transaction = `
32-
import OragaESports from 0x975b04756864e9ea
32+
import OragaESports from 0xb576a3926d239682
3333
3434
transaction(gamerId: UInt) {
3535
prepare(signer: auth(BorrowValue) &Account) {
@@ -57,14 +57,14 @@ export const handler = async (event) => {
5757
fs.readFileSync("/tmp/sequence.txt", { encoding: "utf8" })
5858
);
5959
} else {
60-
IT_KEY_ID = 10;
60+
IT_KEY_ID = 50;
6161
}
62-
IT_KEY_ID = !IT_KEY_ID || IT_KEY_ID >= 10 ? 0 : IT_KEY_ID + 1;
62+
IT_KEY_ID = !IT_KEY_ID || IT_KEY_ID >= 50 ? 0 : IT_KEY_ID + 1;
6363
fs.writeFileSync("/tmp/sequence.txt", IT_KEY_ID.toString());
6464
console.log("IT_KEY_ID", IT_KEY_ID);
6565

6666
const PRIVATE_KEY = fs.readFileSync("mainnet-account.pkey", "utf8");
67-
const ADDRESS = "0x975b04756864e9ea";
67+
const ADDRESS = "0xb576a3926d239682";
6868
const KEY_ID = 0;
6969

7070
const hash = (message) => {

aws_lambda/testnet/index.mjs

Lines changed: 166 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,166 @@
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

Comments
 (0)