dpos-offline
Version:
Offline Signing Transactions for DPOS Blockchains
141 lines (140 loc) • 5.51 kB
JavaScript
"use strict";
var __assign = (this && this.__assign) || function () {
__assign = Object.assign || function(t) {
for (var s, i = 1, n = arguments.length; i < n; i++) {
s = arguments[i];
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
t[p] = s[p];
}
return t;
};
return __assign.apply(this, arguments);
};
Object.defineProperty(exports, "__esModule", { value: true });
// tslint:disable max-line-length
var empty = require("is-empty");
var Long = require("long");
var varuint_bitcoin_1 = require("varuint-bitcoin");
var sha256_1 = require("../utils/sha256");
var sodium_1 = require("../utils/sodium");
var lisk_1 = require("./txs/lisk");
exports.Lisk = {
txs: {
_codec: null,
getAddressBytes: function (address) {
return new Buffer(Long.fromString(address.slice(0, -1)).toBytesBE());
},
bytes: function (tx) {
return lisk_1.liskCodecUtils.findCodecFromType(tx.type)
.calcFullBytes(tx);
},
bytesForSignature: function (tx) {
return lisk_1.liskCodecUtils
.findCodecFromType(tx.type)
.calcBytes(tx);
},
transform: function (tx) {
return lisk_1.liskCodecUtils.findCodecFromIdentifier(tx.kind)
.transform(tx);
},
// tslint:disable-next-line max-line-length
calcSignature: function (tx, kp) {
return this._codec.raw.sign(sha256_1.toSha256(this.bytes(tx)), typeof (kp) === 'string' ? this._codec.deriveKeypair(kp) : kp);
},
// tslint:disable-next-line variable-name
createAndSign: function (tx, _kp, inRawFormat) {
var kp = typeof (_kp) === 'string' ? this._codec.deriveKeypair(_kp) : _kp;
if (empty(tx.sender)) {
tx.sender = {
address: this._codec.calcAddress(kp.publicKey),
publicKey: kp.publicKey,
};
}
var signableTx = this.transform(tx);
var signedTx = this.sign(signableTx, kp);
if (inRawFormat) {
return signedTx;
}
return this.toPostable(signedTx);
},
// tslint:disable-next-line variable-name
sign: function (tx, _kp) {
var kp = typeof (_kp) === 'string' ? this._codec.deriveKeypair(_kp) : _kp;
if (!tx.senderPublicKey) {
tx.senderPublicKey = kp.publicKey;
}
tx.signature = this.calcSignature(tx, kp, {
skipSecondSign: true,
skipSignature: true,
});
tx.id = this.identifier(tx);
return tx;
},
verify: function (tx, signature, pubKey) {
var hash = sha256_1.toSha256(this.bytesForSignature(tx));
return this._codec.raw.verify(hash, signature || tx.signature, pubKey || tx.senderPublicKey);
},
toPostable: function (tx) {
return __assign({}, lisk_1.liskCodecUtils.findCodecFromType(tx.type)
.toPostable(tx), { id: this.identifier(tx) });
},
fromPostable: function (ptx) {
return lisk_1.liskCodecUtils.findCodecFromType(ptx.type)
.fromPostable(ptx);
},
identifier: function (tx) {
var hash = sha256_1.toSha256(this.bytes(tx));
var temp = [];
for (var i = 0; i < 8; i++) {
temp.push(hash[7 - i]);
}
return Long.fromBytesBE(temp, true).toString();
},
},
msgs: {
_codec: null,
prefix: new Buffer('Lisk Signed Message:\n', 'utf8'),
signablePayload: function (message) {
var msgBuf = Buffer.isBuffer(message) ? message : Buffer.from(message, 'utf8');
var buf = Buffer.concat([
varuint_bitcoin_1.encode(this.prefix.length),
this.prefix,
varuint_bitcoin_1.encode(msgBuf.length),
msgBuf,
]);
return sha256_1.toSha256(sha256_1.toSha256(buf));
},
sign: function (message, kp) {
return this._codec.raw.sign(this.signablePayload(message), kp);
},
verify: function (message, signature, publicKey) {
return this._codec.raw.verify(this.signablePayload(message), signature, publicKey);
},
},
deriveKeypair: function (secret) {
var hash = sha256_1.toSha256(Buffer.isBuffer(secret) ? secret : Buffer.from(secret, 'utf8'));
var r = sodium_1.ed25519.crypto_sign_seed_keypair(hash);
return {
privateKey: r.secretKey,
publicKey: r.publicKey,
};
},
calcAddress: function (publicKey) {
var hash = sha256_1.toSha256(Buffer.isBuffer(publicKey) ? publicKey : Buffer.from(publicKey, 'hex'));
var temp = [];
for (var i = 0; i < 8; i++) {
temp.push(hash[7 - i]);
}
return Long.fromBytesBE(temp, true).toString() + "L";
},
raw: {
sign: function (buf, kp) {
return sodium_1.ed25519.crypto_sign_detached(buf, kp.privateKey);
},
verify: function (buf, signature, publicKey) {
return sodium_1.ed25519.crypto_sign_verify_detached(signature, buf, publicKey);
},
},
};
exports.Lisk.msgs._codec = exports.Lisk;
exports.Lisk.txs._codec = exports.Lisk;