Skip to content

Commit b62c53c

Browse files
committed
add send tx. not tested yet
1 parent 2d801cb commit b62c53c

File tree

1 file changed

+91
-16
lines changed

1 file changed

+91
-16
lines changed

app.js

Lines changed: 91 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ app.use(txOracleHandler);
9494
app.use(txChallengeHandler);
9595
app.use(txVoteHandler);
9696
app.use(txDistributeHandler);
97-
97+
app.use(txSendHandler);
9898

9999
app.listen(3000).then(function(appInfo) {
100100
console.log(appInfo);
@@ -110,22 +110,12 @@ function txVerifySigHandler(state, tx, chainInfo) {
110110
if (tx.type === "verifySig") {
111111
let cloned = Object.assign({}, tx);
112112
console.log("verifySig tx: ", JSON.stringify(cloned));
113-
let from = cloned.from;
114-
let pubkey = from.pubkey;
115-
let signature = from.signature;
116-
let sigHash = getSigHash(tx);
117-
let addr = addressHash(pubkey);
118-
console.log("pubkey: ", pubkey);
119-
console.log("signature: ", signature);
120-
console.log("sigHash: ", sigHash);
121-
console.log("addr: ", addr);
122-
123-
// verify signature
124-
if (!secp.verify(sigHash, signature, pubkey)) {
125-
console.log('invalid signature');
113+
let result = verifySig(tx);
114+
if (result.verified) {
115+
console.log("signature verified!");
126116
} else {
127-
console.log("signature verified! ***");
128-
}
117+
throw Error("invalid signature");
118+
}
129119
}
130120
}
131121

@@ -369,9 +359,94 @@ function txDistributeHandler(state, tx, chainInfo) {
369359
}
370360
}
371361

362+
/**
363+
* send token (balances) from a to b
364+
* @param {*} state
365+
* @param {*} tx
366+
* @param {*} chainInfo
367+
*/
368+
function txSendHandler(state, tx, chainInfo) {
369+
if (tx.type === "send") {
370+
let cloned = Object.assign({}, tx);
371+
console.log("send tx: ", JSON.stringify(cloned));
372+
let from = cloned.from;
373+
let pubkey = from.pubkey;
374+
let signature = from.signature;
375+
let amount = new BigNumber(from.amount);
376+
let sigHash = getSigHash(tx);
377+
let fromAddr = addressHash(pubkey);
378+
let to = cloned.to;
379+
let toAddr = to.address;
380+
console.log("pubkey: ", pubkey);
381+
console.log("signature: ", signature);
382+
console.log("sigHash: ", sigHash);
383+
console.log("from addr: ", fromAddr);
384+
console.log("to addr: ", toAddr);
385+
386+
// verify signature
387+
if (!secp.verify(sigHash, signature, pubkey)) {
388+
console.log('invalid signature! *** ');
389+
throw Error('invalid signature!');
390+
} else {
391+
console.log("signature verified! ***");
392+
console.log("send " + amount + " from " + fromAddr + " to " + toAddr);
393+
394+
send(from, to);
395+
}
396+
}
397+
}
398+
399+
function send(from, to) {
400+
let fromBalance = new BigNumber(state.balances[from]);
401+
state.balances[from] = fromBalance.minus(amount).toNumber();
402+
403+
let toBalance = new BigNumber(state.balances[to]);
404+
state.balances[to] = toBalance.minus(amount).toNumber();
405+
}
406+
407+
/**
408+
* verify is signature is correct.
409+
*
410+
* @param {*} tx
411+
* @return {verified: [boolean], address: [string]}
412+
* verified is true if signature is verified. and if user is provided, addr is same as user.
413+
* address is the address derived form the public key provided in tx.from.pubkey.
414+
*/
415+
function verifySig(tx) {
416+
let verified = false;
417+
let addrSame = true;
418+
let cloned = Object.assign({}, tx);
419+
console.log("send tx: ", JSON.stringify(cloned));
420+
let from = cloned.from;
421+
let pubkey = from.pubkey;
422+
let signature = from.signature;
423+
let sigHash = getSigHash(tx);
424+
let fromAddr = addressHash(pubkey);
425+
console.log("pubkey: ", pubkey);
426+
console.log("signature: ", signature);
427+
console.log("sigHash: ", sigHash);
428+
console.log("from addr: ", fromAddr);
429+
let user = cloned.user;
430+
if (typeof cloned.user !== "undefined") {
431+
addrSame = (fromAddr == user);
432+
}
433+
434+
// verify signature
435+
if (!secp.verify(sigHash, signature, pubkey)) {
436+
console.log('invalid signature! *** ');
437+
438+
} else {
439+
console.log("signature verified! ***");
440+
verified = true;
441+
}
442+
443+
return {"verified": (verified && addrSame), "address": fromAddr};
444+
}
445+
372446

373447
class LocalContractStorage {
374448

449+
375450
constructor(state) {
376451
this.state = state;
377452
}

0 commit comments

Comments
 (0)