Skip to content
This repository was archived by the owner on Oct 8, 2019. It is now read-only.

Commit e8ca065

Browse files
authored
Merge pull request #37 from wownmedia/fix-error-handling
Fix: issue 25 - Crash if trying to send a transaction from a new account
2 parents c9a75ca + f8887ed commit e8ca065

File tree

1 file changed

+28
-24
lines changed

1 file changed

+28
-24
lines changed

index.js

Lines changed: 28 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -153,30 +153,34 @@ function postTransaction(container, transaction, cb){
153153

154154
let senderAddress = arkjs.crypto.getAddress(transaction.senderPublicKey);
155155
getFromNode('http://' + server + '/api/accounts?address=' + senderAddress, function(err, response, body){
156-
if(!body) {
157-
performPost();
158-
} else {
159-
body = JSON.parse(body);
160-
if (body.account.secondSignature) {
161-
container.prompt({
162-
type: 'password',
163-
name: 'passphrase',
164-
message: 'Second passphrase: ',
165-
}, function(result) {
166-
if (result.passphrase) {
167-
var secondKeys = arkjs.crypto.getKeys(result.passphrase);
168-
arkjs.crypto.secondSign(transaction, secondKeys);
169-
transaction.id = arkjs.crypto.getId(transaction);
170-
performPost();
171-
} else {
172-
vorpal.log('No second passphrase given. Trying without.');
173-
performPost();
174-
}
175-
});
176-
} else {
177-
performPost();
178-
}
179-
}
156+
157+
if(!err && body) {
158+
try {
159+
body = JSON.parse(body);
160+
if ( !body.hasOwnProperty('success') || body.success === false) {
161+
// The account does not yet exist on the connected node.
162+
throw "Failed: " + body.error;
163+
}
164+
if (body.hasOwnProperty('account') && body.account.secondSignature) {
165+
container.prompt({
166+
type: 'password',
167+
name: 'passphrase',
168+
message: 'Second passphrase: ',
169+
}, function(result) {
170+
if (result.passphrase) {
171+
var secondKeys = arkjs.crypto.getKeys(result.passphrase);
172+
arkjs.crypto.secondSign(transaction, secondKeys);
173+
transaction.id = arkjs.crypto.getId(transaction);
174+
} else {
175+
vorpal.log('No second passphrase given. Trying without.');
176+
}
177+
});
178+
}
179+
} catch (error) {
180+
vorpal.log(colors.red(error));
181+
}
182+
} // if(body)
183+
performPost();
180184
});
181185
}
182186

0 commit comments

Comments
 (0)