-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
69 lines (60 loc) · 2.68 KB
/
index.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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
const liskCryptography = require('@liskhq/lisk-cryptography');
const liskTransactions = require('@liskhq/lisk-transactions');
class LiskChainCrypto {
constructor({chainOptions}) {
this.sharedPassphrase = chainOptions.sharedPassphrase;
this.passphrase = chainOptions.passphrase;
}
async load() {}
async unload() {}
// This method checks that:
// 1. The signerAddress corresponds to the publicKey.
// 2. The publicKey corresponds to the signature.
async verifyTransactionSignature(transaction, signaturePacket) {
let { signature: signatureToVerify, publicKey, signerAddress } = signaturePacket;
let expectedAddress = liskCryptography.getAddressFromPublicKey(publicKey);
if (signerAddress !== expectedAddress) {
return false;
}
let { signature, signSignature, signatures, ...transactionToHash } = transaction;
let txnHash = liskCryptography.hash(liskTransactions.utils.getTransactionBytes(transactionToHash));
return liskCryptography.verifyData(txnHash, signatureToVerify, publicKey);
}
async prepareTransaction(transactionData) {
try {
liskTransactions.utils.validateAddress(transactionData.recipientAddress);
} catch (error) {
throw new Error(
'Failed to prepare the transaction because the recipientAddress was invalid'
);
}
let sharedPassphrase = this.sharedPassphrase;
let passphrase = this.passphrase;
let txn = {
type: 0,
amount: transactionData.amount.toString(),
recipientId: transactionData.recipientAddress,
fee: liskTransactions.constants.TRANSFER_FEE.toString(),
asset: {},
timestamp: transactionData.timestamp,
senderPublicKey: liskCryptography.getAddressAndPublicKeyFromPassphrase(sharedPassphrase).publicKey
};
if (transactionData.message != null) {
txn.asset.data = transactionData.message;
}
let preparedTxn = liskTransactions.utils.prepareTransaction(txn, sharedPassphrase);
let { signature, signSignature, signatures, ...transactionToHash } = preparedTxn;
let txnHash = liskCryptography.hash(liskTransactions.utils.getTransactionBytes(transactionToHash));
let { address: signerAddress, publicKey } = liskCryptography.getAddressAndPublicKeyFromPassphrase(passphrase);
// The signature needs to be an object with a signerAddress property, the other
// properties are flexible and depend on the requirements of the underlying blockchain.
let multisigTxnSignature = {
signerAddress,
publicKey,
signature: liskCryptography.signData(txnHash, passphrase)
};
preparedTxn.signatures = [];
return {transaction: preparedTxn, signature: multisigTxnSignature};
}
}
module.exports = LiskChainCrypto;