-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest-wallet.js
34 lines (27 loc) · 1.24 KB
/
test-wallet.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
const Wallet = require("../wallet");
const {createSign, createVerify} = require('crypto');
// returning data signature:
const sign = (data, publicKey, privateKey) => {
const cert = Wallet.getNodePrivateKey(publicKey, privateKey);
return createSign('SHA256').update(data).sign(cert, 'hex');
};
// verifing signature:
const verify = (data, publicKey, signature) => {
const cert = Wallet.getNodePublicKey(publicKey);
return createVerify('SHA256').update(data).verify(cert, signature, 'hex');
};
// Example wallets:
const bob = Wallet.create();
const alice = Wallet.create();
// Case #1: Order that has a valid signature:
const orderOne = 'send ten bucks from bob to alice';
const orderOneIssuer = bob.publicKey;
const orderOneSignature = sign(orderOne, orderOneIssuer, bob.privateKey);
// Case #2: Order that has a fraud signature from another person:
const orderTwo = 'send thousand bucks from bob to alice';
const orderTwoIssuer = bob.publicKey;
const orderTwoSignature = sign(orderTwo, orderOneIssuer, alice.privateKey);
// Verification:
console.log('Order #1 status:', verify(orderOne, orderOneIssuer, orderOneSignature));
console.log('Order #2 status:', verify(orderTwo, orderTwoIssuer, orderTwoSignature));
// Expected results: true, false