enable usage of Infura or similar

This commit is contained in:
Michele Balistreri 2020-02-26 19:41:15 +03:00
parent 2c7db2bb09
commit c390b06dd2
No known key found for this signature in database
GPG Key ID: E9567DA33A4F791A
3 changed files with 42 additions and 5 deletions

View File

@ -31,7 +31,11 @@ or
Other options are
`--endpoint`: the address of the RPC endpoint. The client as an empty origin so make sure to start the Ethereum node as needed. For ws, you can just start it with --wsorigins="*". The default value is ws://127.0.0.1:8546.
`--sender`: the address signing and sending the transactions. This account will pay for gas. **THIS ACCOUNT MUST BE ALREADY UNLOCKED**. If not specified, accounts[0] is used. Also in this case, the account must be unlocked.
`--sender`: the address signing and sending the transactions. This account will pay for gas. **THIS ACCOUNT MUST BE ALREADY UNLOCKED**. If not specified, accounts[0] is used. Also in this case, the account must be unlocked. Ignored if --account is used.
`--account`: the path to a JSON encoded private key. If this is specified, sender will be ignored. Use this if your endpoint is Infura or similar. You do not need a local node in this case. You also need to specify --passfile.
`--passfile`: the path to a file storing the password for the JSON encoded private key. Always used with --account.
`--maxTxValue`: the maxTxValue for payment transaction. This can be changed later but providing a meaningful value on creation can be convenient. Defaults to 100000000.

View File

@ -16,12 +16,28 @@ async function getDefaultSender() {
return accounts[0];
}
function loadAccount(account, passfile) {
let json = fs.readFileSync(account, "utf-8");
let pass = fs.readFileSync(passfile, "utf-8").split("\n")[0].replace("\r", "");
return web3.eth.accounts.decrypt(json, pass);
}
async function createWallet(sender, keycard, maxTxValue, minBlockDistance) {
let methodCall = KeycardWalletFactory.methods.create(keycard.toLowerCase(), {maxTxValue: maxTxValue, minBlockDistance: minBlockDistance}, true);
try {
let gasAmount = await methodCall.estimateGas({from: sender})
let receipt = await methodCall.send({from: sender, gas: gasAmount});
let receipt;
if (typeof(sender) == "string") {
let gasAmount = await methodCall.estimateGas({from: sender});
receipt = await methodCall.send({from: sender, gas: gasAmount});
} else {
let gasAmount = await methodCall.estimateGas({from: sender.address});
let data = methodCall.encodeABI();
let signedTx = await sender.signTransaction({to: KeycardWalletFactory.options.address, data: data, gas: gasAmount});
receipt = await web3.eth.sendSignedTransaction(signedTx.rawTransaction);
}
const event = receipt.events.NewWallet;
return event.returnValues.wallet;
} catch(err) {
@ -32,7 +48,6 @@ async function createWallet(sender, keycard, maxTxValue, minBlockDistance) {
async function run() {
KeycardWalletFactory.transactionConfirmationBlocks = 3;
let sender = argv["sender"] || await getDefaultSender();
let keycards;
@ -51,6 +66,23 @@ async function run() {
process.exit(1);
}
let sender;
if (argv["account"]) {
if (!argv["passfile"]) {
console.error("the ---passfile option must be specified when using the --account option");
process.exit(1);
}
if (argv["sender"]) {
console.warn("--account used, --sender will be ignored");
}
sender = loadAccount(argv["account"], argv["passfile"]);
} else {
sender = argv["sender"] || await getDefaultSender();
}
let walletAddresses = await Promise.all(keycards.map((keycard) => createWallet(sender, keycard, argv["maxTxValue"], argv["minBlockDistance"])));
let zippedAddresses = keycards.map((keycard, i) => [keycard, walletAddresses[i]]);

View File

@ -13,6 +13,7 @@
"dependencies": {
"esm": "^3.2.25",
"minimist": "^1.2.0",
"scrypt": "^6.0.3",
"web3": "^1.2.6"
}
}