-
Notifications
You must be signed in to change notification settings - Fork 0
/
wallet.js
144 lines (110 loc) · 4.73 KB
/
wallet.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
const { Client, PrivateKey, AccountCreateTransaction, AccountBalanceQuery, Hbar, TransferTransaction} = require("@hashgraph/sdk");
require("dotenv").config();
/////////////////////
// CONNECT TO HEDERA
/////////////////////
// get ID and private key from .env file
const myAccountId = process.env.MY_ACCOUNT_ID;
const myPrivateKey = process.env.MY_PRIVATE_KEY;
if (myAccountId == null ||
myPrivateKey == null ) {
throw new Error("Environment variables myAccountId and myPrivateKey must be present");
}
console.log("accountid: " + myAccountId);
console.log("privacykey: " + myPrivateKey);
// Create connection to the Hedera network with my credentials
let client = Client.forTestnet();
client.setOperator(myAccountId, myPrivateKey);
///////////////////////////
// CONNECT TO HEDERA DONE
///////////////////////////
// players for mocking
const player1 = {
id: "0.0.222772",//"0.0.221531",
privateKey: "302e020100300506032b65700422042082ba57e7e71d5a50381b0e7bb09c1fc811d65f8667ebd0cf5204ed02f23d4ba0",
publicKey: "302a300506032b6570032100c541045da8f65de030644c60896c663df4f4fd58e3279384fa7f81b1739a8caa",
}
const player2 = {
id: "0.0.222773",//"0.0.221532",
privateKey: "302e020100300506032b657004220420c0f38294900113598e492172c0c214d449f34069ea69bb4a90a41221c3d061ea",
publicKey: "302a300506032b65700321003afec291cf9c35139769b5ae43a9151a26d057b49a272ac6ba78e17ee9144991",
}
/*
output
private key: 302e020100300506032b65700422042082ba57e7e71d5a50381b0e7bb09c1fc811d65f8667ebd0cf5204ed02f23d4ba0
public key: 302a300506032b6570032100c541045da8f65de030644c60896c663df4f4fd58e3279384fa7f81b1739a8caa
private key: 302e020100300506032b657004220420c0f38294900113598e492172c0c214d449f34069ea69bb4a90a41221c3d061ea
public key: 302a300506032b65700321003afec291cf9c35139769b5ae43a9151a26d057b49a272ac6ba78e17ee9144991
The new account ID is: 0.0.222772
The new account ID is: 0.0.222773
The new account balance is: 1000 tinybar.
The new account balance is: 1000 tinybar.
*/
// set timer
function startTimer() {
console.log('timer started');
}
//window.startTimer = startTimer;
// create an account and return the account id
// we dont need this because every time we run the code a new account is created and saved forever in the hedera (test) database
// so just use player 1 and player 2 as generated already
async function createAccount() {
//Create new keys
const newAccountPrivateKey = await PrivateKey.generate();
const newAccountPublicKey = newAccountPrivateKey.publicKey;
console.log('private key: ' + newAccountPrivateKey);
console.log('public key: ' + newAccountPublicKey);
//Create a new account with 1,000 tinybar starting balance
const newAccountTransactionResponse = await new AccountCreateTransaction()
.setKey(newAccountPublicKey)
.setInitialBalance(Hbar.fromTinybars(1000))
.execute(client);
// Get the new account ID
const getReceipt = await newAccountTransactionResponse.getReceipt(client);
const newAccountId = getReceipt.accountId;
console.log("The new account ID is: " +newAccountId);
//Verify the account balance
const accountBalance = await new AccountBalanceQuery()
.setAccountId(newAccountId)
.execute(client);
console.log("The new account balance is: " +accountBalance.hbars.toTinybars() +" tinybar.");
return newAccountId;
}
async function getBalance(id) {
const balance = await new AccountBalanceQuery()
.setAccountId(id)
.execute(client);
console.log("The account balance of " + id + " is: " + balance.hbars.toTinybars() +" tinybar.");
return balance.hbars.toTinybars();
}
//window.getBalance = getBalance;
//document.getElementById("addvalue").onClick = getBalance;
async function transaction(from, to, amount) {
// balance check
getBalance(from.id);
getBalance(to.id);
//Create the transfer transaction
const transferTransaction = await new TransferTransaction()
.addHbarTransfer(from.id, Hbar.fromTinybars(0 - amount))
.addHbarTransfer(to.id, Hbar.fromTinybars(amount))
.freezeWith(client)
.sign(PrivateKey.fromString(from.privateKey));
// sign with private key of fromID user
const signedTransaction = await transferTransaction
.execute(client);
//Verify the transaction status
const transactionReceipt = await signedTransaction.getReceipt(client);
console.log("The transfer transaction from my account to the new account was: " + transactionReceipt.status.toString());
// balance check
getBalance(from.id);
getBalance(to.id);
}
// try a transaction!
// transaction(from, to, amount[in tinybars])
transaction(player2, player1, 500);
module.exports = {
startTimer,
createAccount,
getBalance,
transaction,
}