/
App.js
220 lines (192 loc) · 6.12 KB
/
App.js
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
// Imports
import './App.css';
import algosdk from "algosdk";
import { DeflyWalletConnect } from '@blockshake/defly-connect';
import { useEffect } from 'react';
// deflywallet instantiating
const deflywallet = new DeflyWalletConnect()
// algoClient
const algod_address = "https://mainnet-api.algonode.cloud"
const algod_token = ""
const headers = {"X-Algo-API-Token": algod_token }
const algodClient = new algosdk.Algodv2(algod_token, algod_address, headers);
//get address
const address = localStorage.getItem('address');
// prize address
const prizeAddress = ''
/////////////////////////
/////////////////////////
// THIS IS THE VARIABLE THAT NEEDS PROTECTED.
// !!!!!!!!!!!!!!!!!!!!!!!!!!!
/////////////////////////
/////////////////////////
const private_key = ''
/////////////////////////
/////////////////////////
/////////////////////////
//asset id
const ASSET_ID = 297995609;
/// transaction code
const transaction = async () => {
try{
const suggestedParams = await algodClient.getTransactionParams().do();
const txn = algosdk.makeAssetTransferTxnWithSuggestedParamsFromObject({
from: address,
to: prizeAddress,
amount: 200,
assetIndex: ASSET_ID,
suggestedParams,
});
const optInTxn = [{txn : txn, signers: [address]}]
const signedTxn = await deflywallet.signTransaction([optInTxn])
const success = await algodClient.sendRawTransaction(signedTxn).do();
return success
}
catch(err){
console.log(err)
return false
}
}
// Wallet Connect
async function walletConnect() {
const newAccounts= await deflywallet.connect()
localStorage.setItem("address", newAccounts[0]);
window.location.reload()
console.log('Connect')
}
// wallet disconnect
const disconnect = () => {
deflywallet.disconnect()
localStorage.removeItem("address");
window.location.reload()
}
/////////////////////////
/////////////////////////
// Transaction from wallet to user upon conditional logic.
/////////////////////////
/////////////////////////
/////////////////////////
/////////////////////////
/////////////////////////
const smallestprizetransaction = async () => {
/////////////////////////
/////////////////////////
// Secure variable call for private key.
// !!!!!!!!!!!!!!!!!!!!!!!!!!!
/////////////////////////
const mnemonic = private_key;
/////////////////////////
/////////////////////////
/////////////////////////
/////////////////////////
const recoveblueAccount = algosdk.mnemonicToSecretKey(mnemonic);
const suggestedParams = await algodClient.getTransactionParams().do();
const txn = algosdk.makeAssetTransferTxnWithSuggestedParamsFromObject({
from: prizeAddress,
to: address,
amount: 200,
assetIndex: ASSET_ID,
suggestedParams,
});
// Sign the transaction
const signedTxn = txn.signTxn(recoveblueAccount.sk);
const sendTx = algodClient.sendRawTransaction(signedTxn).do();
const txId = txn.txID().toString();
console.log("Transaction sent with ID " + sendTx.txId);
console.log("Signed transaction with txID: %s", txId);
// Wait for confirmation
algosdk.waitForConfirmation(algodClient, txId, 4);
}
/////////////////////////
/////////////////////////
// Conditional logic triggering transaction.
/////////////////////////
/////////////////////////
/////////////////////////
/////////////////////////
/////////////////////////
const guess = async () => {
const transactionSuccess = await transaction()
if (transactionSuccess){
////////////////////
const generateNumberOne = Math.floor(Math.random() * 3)
const generateNumberTwo = Math.floor(Math.random() * 3)
const generateNumberThree = Math.floor(Math.random() * 3)
document.getElementById('message1').textContent = generateNumberOne
document.getElementById('message2').textContent = generateNumberTwo
document.getElementById('message3').textContent = generateNumberThree
// Convert to Int
const generateNumberOneInt = parseInt(generateNumberOne);
const generateNumberTwoInt = parseInt(generateNumberTwo);
const generateNumberThreeInt = parseInt(generateNumberThree);
console.log(generateNumberOneInt)
console.log(generateNumberTwoInt)
console.log(generateNumberThreeInt)
/////////////////////////
/////////////////////////
// Function call on conditional happening.
/////////////////////////
/////////////////////////
/////////////////////////
/////////////////////////
/////////////////////////
if(generateNumberOneInt===2){
document.getElementById('message4').textContent = 'Win 2k Choice!'
smallestprizetransaction();
} else if(generateNumberTwoInt===2){
document.getElementById('message4').textContent = 'Win 2k Choice'
smallestprizetransaction();
} else if(generateNumberThreeInt===2){
document.getElementById('message4').textContent = 'Win 2k Choice!'
smallestprizetransaction();
}
};
}
/////////////////////////
/////////////////////////
// React App
/////////////////////////
/////////////////////////
/////////////////////////
/////////////////////////
/////////////////////////
function App() {
useEffect(() => {
deflywallet.reconnectSession().then((accounts) => {
if (accounts.length) {
localStorage.setItem("address", accounts[0]);
}
deflywallet.connector?.on("disconnect", () => {
localStorage.removeItem("address");
});
})
.catch((e) => console.log(e));
}, [])
return (
<div className="App">
<header className="App-header">
<h1>
<div id = "displaytext" style={{ color: "blue" }}> Private Key Storage Example </div>
</h1>
<p>
<p>
<button id='button1' onClick={walletConnect}> Connect</button>
<button id='button2' onClick={disconnect}> Disconnect</button>
</p>
</p>
<table>
<tr>
<td id='message1'></td>
<td id='message2'></td>
<td id='message3'></td>
</tr>
</table>
<div id='message4'></div>
<div>
<button id='button3' onClick={guess}>Button</button>
</div>
</header>
</div>
);
}
export default App;