dpos-offline
Version:
Offline Signing Transactions for DPOS Blockchains
176 lines (175 loc) • 7.18 kB
JavaScript
;
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);
};
var __read = (this && this.__read) || function (o, n) {
var m = typeof Symbol === "function" && o[Symbol.iterator];
if (!m) return o;
var i = m.call(o), r, ar = [], e;
try {
while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
}
catch (error) { e = { error: error }; }
finally {
try {
if (r && !r.done && (m = i["return"])) m.call(i);
}
finally { if (e) throw e.error; }
}
return ar;
};
var __spread = (this && this.__spread) || function () {
for (var ar = [], i = 0; i < arguments.length; i++) ar = ar.concat(__read(arguments[i]));
return ar;
};
Object.defineProperty(exports, "__esModule", { value: true });
var bech32 = require("bech32-buffer");
var empty = require("is-empty");
var Long = require("long");
var sha256_1 = require("../utils/sha256");
var toTransportable_1 = require("../utils/toTransportable");
var lisk_1 = require("./lisk");
var rise_1 = require("./txs/rise");
var RiseV2Txs = /** @class */ (function () {
function RiseV2Txs() {
}
RiseV2Txs.prototype.bytes = function (tx) {
return rise_1.riseCodecUtils.findCodecFromType(tx.type)
.calcFullBytes(tx);
};
RiseV2Txs.prototype.bytesForSignature = function (tx) {
return rise_1.riseCodecUtils.findCodecFromType(tx.type)
.calcBytes(tx);
};
RiseV2Txs.prototype.calcSignature = function (tx, kp) {
return this._codec.raw.sign(sha256_1.toSha256(this.bytesForSignature(tx)), typeof (kp) === 'string' ? this._codec.deriveKeypair(kp) : kp);
};
RiseV2Txs.prototype.calc2ndSignature = function (tx, kp) {
return this._codec.raw.sign(sha256_1.toSha256(Buffer.concat([this.bytesForSignature(tx), tx.signatures[0]])), typeof (kp) === 'string' ? this._codec.deriveKeypair(kp) : kp);
};
// tslint:disable-next-line variable-name
RiseV2Txs.prototype.createAndSign = function (tx, _kp, net, inRawFormat) {
var kp = typeof (_kp) === 'string' ? this._codec.deriveKeypair(_kp) : _kp;
if (!net) {
net = 'main';
}
if (net === true) {
net = 'main';
inRawFormat = true;
}
if (empty(tx.sender)) {
tx.sender = {
address: this._codec.calcAddress(kp.publicKey, net),
publicKey: kp.publicKey,
};
}
var signableTx = this.transform(tx, net);
var signedTx = this.sign(signableTx, kp);
if (inRawFormat) {
return signedTx;
}
return this.toPostable(signedTx);
};
RiseV2Txs.prototype.createNonce = function () {
return "" + Math.floor((Date.now() - Date.UTC(2016, 4, 24, 17, 0, 0, 0)) / 1000);
};
RiseV2Txs.prototype.fromPostable = function (ptx) {
return rise_1.riseCodecUtils.findCodecFromType(ptx.type)
.fromPostable(ptx);
};
RiseV2Txs.prototype.getAddressBytes = function (address) {
return rise_1.RiseIdsHandler.addressToBytes(address);
};
RiseV2Txs.prototype.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();
};
// tslint:disable-next-line variable-name
RiseV2Txs.prototype.sign = function (tx, _kp) {
var kp = typeof (_kp) === 'string' ? this._codec.deriveKeypair(_kp) : _kp;
if (!tx.senderPubData) {
if ([0, 1, 2, 3].indexOf(tx.type) !== -1) {
tx.senderPubData = kp.publicKey;
}
else {
tx.senderPubData = Buffer.concat([new Buffer([1]), kp.publicKey]);
}
}
tx.signatures = tx.signatures || [];
tx.signatures.push(this.calcSignature(tx, kp));
tx.id = this.identifier(tx);
return tx;
};
RiseV2Txs.prototype.toPostable = function (tx) {
return __assign({}, toTransportable_1.toTransportable(tx), { id: this.identifier(tx) });
};
RiseV2Txs.prototype.transform = function (tx, net) {
if (net === void 0) { net = 'main'; }
tx.sender.address = tx.sender.address || this._codec.calcAddress(tx.sender.publicKey, net, 'v1');
return rise_1.riseCodecUtils.findCodecFromIdentifier(tx.kind)
.transform(tx);
};
RiseV2Txs.prototype.verify = function (tx, signature, pubKey) {
var hash = sha256_1.toSha256(this.bytesForSignature(tx));
return this._codec.raw.verify(hash, signature || tx.signatures[0], pubKey || this.derivePublicKeyFromPubData(tx.senderPubData));
};
RiseV2Txs.prototype.fromV1Format = function (tx) {
var asset = tx.asset;
if (tx.type === 1 || tx.type === 11) {
asset.signature.publicKey = Buffer.from(asset.signature.publicKey, 'hex');
}
return {
amount: "" + tx.amount,
asset: asset,
fee: "" + tx.fee,
id: tx.id,
recipientId: tx.recipientId,
senderId: tx.senderId,
senderPubData: tx.senderPublicKey,
signatures: __spread([
tx.signature,
tx.signSignature
], (tx.signatures || [])).filter(function (s) { return typeof (s) !== 'undefined' && s !== null; }),
timestamp: tx.timestamp,
type: tx.type,
version: 0,
};
};
RiseV2Txs.prototype.toV1Format = function (tx) {
return __assign({}, tx, { amount: parseInt(tx.amount, 10), fee: parseInt(tx.fee, 10), senderPublicKey: tx.senderPubData, signSignature: tx.signatures[1], signature: tx.signatures[0] });
};
RiseV2Txs.prototype.derivePublicKeyFromPubData = function (pubData) {
if (pubData.length === 33) {
return pubData.slice(1, 33);
}
return pubData;
};
return RiseV2Txs;
}());
exports.RiseV2Txs = RiseV2Txs;
exports.RiseV2 = __assign({}, lisk_1.Lisk, { msgs: __assign({}, lisk_1.Lisk.msgs, { prefix: new Buffer('RISE Signed Message:\n', 'utf8') }), txs: new RiseV2Txs(), calcAddress: function (publicKey, net, type) {
if (net === void 0) { net = 'main'; }
if (type === void 0) { type = 'v1'; }
if (type === 'v0') {
return lisk_1.Lisk.calcAddress(publicKey).replace('L', 'R');
}
var pubKey = Buffer.isBuffer(publicKey) ? publicKey : Buffer.from(publicKey, 'hex');
return bech32.encode(net === 'main' ? 'rise' : (net === 'test' ? 'tise' : 'dise'), Buffer.concat([
Buffer.from([1]),
pubKey,
]));
} });
exports.RiseV2.msgs._codec = exports.RiseV2;
exports.RiseV2.txs._codec = exports.RiseV2;