@@ -94,7 +94,7 @@ app.use(txOracleHandler);
94
94
app . use ( txChallengeHandler ) ;
95
95
app . use ( txVoteHandler ) ;
96
96
app . use ( txDistributeHandler ) ;
97
-
97
+ app . use ( txSendHandler ) ;
98
98
99
99
app . listen ( 3000 ) . then ( function ( appInfo ) {
100
100
console . log ( appInfo ) ;
@@ -110,22 +110,12 @@ function txVerifySigHandler(state, tx, chainInfo) {
110
110
if ( tx . type === "verifySig" ) {
111
111
let cloned = Object . assign ( { } , tx ) ;
112
112
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!" ) ;
126
116
} else {
127
- console . log ( "signature verified! *** ") ;
128
- }
117
+ throw Error ( "invalid signature ") ;
118
+ }
129
119
}
130
120
}
131
121
@@ -369,9 +359,94 @@ function txDistributeHandler(state, tx, chainInfo) {
369
359
}
370
360
}
371
361
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
+
372
446
373
447
class LocalContractStorage {
374
448
449
+
375
450
constructor ( state ) {
376
451
this . state = state ;
377
452
}
0 commit comments