Last active
June 21, 2019 17:44
-
-
Save macolo/cdb655e6bb2a9a5b3c8509a69b62bbec to your computer and use it in GitHub Desktop.
example of signing and recovering public address
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
try { | |
let account; | |
// let web3js = new Web3(new Web3.providers.HttpProvider(RCP_ADDRESS)); | |
let web3js_ws = new Web3(new Web3.providers.WebsocketProvider(WS_ADDRESS)); | |
document.getElementById('private-key').value = WALLET_PRIVATE_KEY; | |
// web3js_ws.eth.personal.newAccount(WALLET_PRIVATE_KEY, function(data) { | |
web3js_ws.eth.getAccounts().then( function(accounts) { | |
add_log_entry('Accounts available: ' + accounts); | |
account = accounts[0]; | |
add_log_entry('Using first account as sender account: ' + account); | |
}); | |
// }); | |
// var account = web3js_ws.eth.accounts.privateKeyToAccount(WALLET_PRIVATE_KEY); | |
let ticketing_contract = new web3js_ws.eth.Contract(SC_TICKETNG_ABI, SC_TICKETNG_ADDRESS); | |
// Listenting to the TripCreated event | |
// https://ethereum.stackexchange.com/questions/47362/how-to-listen-to-events-generated-by-an-existing-contract-in-web3-1-x-x | |
ticketing_contract.events.TripCreated((err, events) => { | |
add_log_entry(err, events); | |
}); | |
// create test event | |
document.getElementById('create-test-event-button').addEventListener("click", function () { | |
// check balance... | |
web3js_ws.eth.getBalance(WALLET_ADDRESS).then(function(balance){ | |
add_log_entry('Sender wallet balance (WALLET_ADDRESS): ' + balance); | |
}); | |
ticketing_contract.methods.checkIn(TRANSPORTER_ADDRESS).send({ | |
'from': WALLET_ADDRESS, | |
'gas': 3000000, | |
}).then(function(data) { | |
add_log_entry('Checked in with hash '+ JSON.stringify(data)) | |
}); | |
}); | |
document.getElementById('sign-recover-button').addEventListener("click", function () { | |
message = document.getElementById('message').value; | |
add_log_entry('using private key ' + WALLET_PRIVATE_KEY); | |
// sign | |
var sig_obj = web3js_ws.eth.accounts.sign(message, WALLET_PRIVATE_KEY); | |
add_log_entry("signature object: " + JSON.stringify(sig_obj)); | |
add_log_entry("trying to get the public address from the signature..."); | |
var pub_key = web3js_ws.eth.accounts.recover(message, sig_obj.signature); | |
add_log_entry(pub_key); | |
if (pub_key === account) { | |
add_log_entry("the calculated pub key matches the originally given one!") | |
} else { | |
add_log_entry("Something is not right, the calculated key doesnt match the one from the private key!") | |
} | |
}); | |
document.getElementById('clear-log-button').addEventListener("click", function () { | |
var log = document.getElementById("log"); | |
log.innerHTML = ''; | |
}); | |
} catch (e) { | |
add_log_entry(e); | |
} | |
function add_log_entry(text) { | |
console.log(text) | |
var p = document.createElement("p"); | |
p.textContent = text; | |
document.getElementById('log').appendChild(p); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// local ganache instance | |
WS_ADDRESS = 'ws://0.0.0.0:7545'; | |
RCP_ADDRESS = 'http://0.0.0.0:7545'; | |
// RCP_ADDRESS = 'http://159.100.249.117:8545'; | |
// WS_ADDRESS = 'ws://159.100.249.117:8545'; | |
SC_TICKETNG_ADDRESS = "0xC45657951eFbbeeA2da3131Df796c0088e34439A"; | |
WALLET_ADDRESS = "0x70eD53133d3dddeC777D9E159C430cA7D3C9e1E3"; | |
WALLET_PRIVATE_KEY = '0x43ad9808d861dd1dc0ae09da94c3af96f3217fc41f3afee2acfa6bfda1f8dbb5'; | |
TRANSPORTER_ADDRESS = "0x7ED0e0b6DaF189af08a067852CcF45719f868203"; | |
SC_TICKETNG_ABI = [ | |
{ | |
"constant": true, | |
"inputs": [ | |
{ | |
"name": "", | |
"type": "address" | |
} | |
], | |
"name": "passengers", | |
"outputs": [ | |
{ | |
"name": "isCheckedIn", | |
"type": "bool" | |
}, | |
{ | |
"name": "checkedInTspKey", | |
"type": "address" | |
} | |
], | |
"payable": false, | |
"stateMutability": "view", | |
"type": "function" | |
}, | |
{ | |
"inputs": [], | |
"payable": false, | |
"stateMutability": "nonpayable", | |
"type": "constructor" | |
}, | |
{ | |
"anonymous": false, | |
"inputs": [ | |
{ | |
"indexed": false, | |
"name": "startTimestamp", | |
"type": "uint256" | |
}, | |
{ | |
"indexed": false, | |
"name": "endTimestamp", | |
"type": "uint256" | |
}, | |
{ | |
"indexed": false, | |
"name": "transporter", | |
"type": "address" | |
}, | |
{ | |
"indexed": false, | |
"name": "passenger", | |
"type": "address" | |
}, | |
{ | |
"indexed": false, | |
"name": "isCheckedOut", | |
"type": "bool" | |
}, | |
{ | |
"indexed": false, | |
"name": "isPaid", | |
"type": "bool" | |
}, | |
{ | |
"indexed": false, | |
"name": "price", | |
"type": "uint256" | |
} | |
], | |
"name": "TripCreated", | |
"type": "event" | |
}, | |
{ | |
"anonymous": false, | |
"inputs": [ | |
{ | |
"indexed": false, | |
"name": "startTimestamp", | |
"type": "uint256" | |
}, | |
{ | |
"indexed": false, | |
"name": "endTimestamp", | |
"type": "uint256" | |
}, | |
{ | |
"indexed": false, | |
"name": "transporter", | |
"type": "address" | |
}, | |
{ | |
"indexed": false, | |
"name": "passenger", | |
"type": "address" | |
} | |
], | |
"name": "CheckedOut", | |
"type": "event" | |
}, | |
{ | |
"constant": false, | |
"inputs": [ | |
{ | |
"name": "transporterAddress", | |
"type": "address" | |
} | |
], | |
"name": "checkIn", | |
"outputs": [], | |
"payable": false, | |
"stateMutability": "nonpayable", | |
"type": "function" | |
}, | |
{ | |
"constant": true, | |
"inputs": [ | |
{ | |
"name": "passengerAddress", | |
"type": "address" | |
} | |
], | |
"name": "getTrips", | |
"outputs": [ | |
{ | |
"components": [ | |
{ | |
"name": "startTimestamp", | |
"type": "uint256" | |
}, | |
{ | |
"name": "endTimestamp", | |
"type": "uint256" | |
}, | |
{ | |
"name": "transporter", | |
"type": "address" | |
}, | |
{ | |
"name": "passenger", | |
"type": "address" | |
}, | |
{ | |
"name": "isCheckedOut", | |
"type": "bool" | |
}, | |
{ | |
"name": "isPaid", | |
"type": "bool" | |
}, | |
{ | |
"name": "price", | |
"type": "uint256" | |
} | |
], | |
"name": "", | |
"type": "tuple[]" | |
} | |
], | |
"payable": false, | |
"stateMutability": "view", | |
"type": "function" | |
}, | |
{ | |
"constant": false, | |
"inputs": [], | |
"name": "checkOut", | |
"outputs": [], | |
"payable": false, | |
"stateMutability": "nonpayable", | |
"type": "function" | |
} | |
] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment