UNPKG

dpos-offline

Version:

Offline Signing Transactions for DPOS Blockchains

171 lines (170 loc) 7.03 kB
"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); }; 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 }); // tslint:disable max-line-length var ByteBuffer = require("bytebuffer"); var empty = require("is-empty"); var Long = require("long"); var BaseLiskCodec = /** @class */ (function () { function BaseLiskCodec(type, identifier) { this.type = type; this.identifier = identifier; } BaseLiskCodec.prototype.calcBytes = function (tx) { var assetBytes = this.assetBytes(tx); var bb = new ByteBuffer(1 + 4 + 32 + 32 + 8 + 8 + 64 + 64 + assetBytes.length, true); bb.writeByte(tx.type); bb.writeUint32(tx.timestamp); bb.append(tx.senderPublicKey); if (!empty(tx.requesterPublicKey)) { bb.append(tx.requesterPublicKey); } if (!empty(tx.recipientId)) { bb.append(new Buffer(Long.fromString(tx.recipientId.slice(0, -1)).toBytesBE())); } else { bb.append(Buffer.alloc(8).fill(0)); } // tslint:disable-next-line no-string-literal bb['writeLong'](tx.amount); bb.append(assetBytes); bb.flip(); return new Buffer(bb.toBuffer()); }; BaseLiskCodec.prototype.calcFullBytes = function (tx) { return Buffer.concat(__spread([ this.calcBytes(tx) ], __spread([ tx.signature, tx.signSignature ], (tx.signatures || [])).filter(function (el) { return !!el; }))); }; BaseLiskCodec.prototype.fromPostable = function (ptx) { var toRet = __assign({}, ptx, { amount: parseInt("" + ptx.amount, 10), fee: parseInt("" + ptx.fee, 10), requesterPublicKey: (ptx.requesterPublicKey ? Buffer.from(ptx.requesterPublicKey, 'hex') : null), senderPublicKey: (ptx.senderPublicKey ? Buffer.from(ptx.senderPublicKey, 'hex') : null), signSignature: (ptx.signSignature ? Buffer.from(ptx.signSignature, 'hex') : null), signature: (ptx.signature ? Buffer.from(ptx.signature, 'hex') : null), signatures: (ptx.signatures ? ptx.signatures.map(function (s) { return Buffer.from(s, 'hex'); }) : null) }); ['requesterPublicKey', 'senderPublicKey', 'signSignature', 'signatures', 'signature'] .forEach(function (k) { if (toRet[k] === null) { delete toRet[k]; } }); return toRet; }; BaseLiskCodec.prototype.toPostable = function (tx) { var toRet = __assign({}, tx, { amount: "" + tx.amount, fee: "" + tx.fee, requesterPublicKey: tx.requesterPublicKey ? tx.requesterPublicKey.toString('hex') : null, senderPublicKey: tx.senderPublicKey.toString('hex'), signSignature: tx.signSignature ? tx.signSignature.toString('hex') : null, signature: tx.signature.toString('hex'), signatures: tx.signatures ? tx.signatures.map(function (s) { return s.toString('hex'); }) : null }); ['requesterPublicKey', 'senderPublicKey', 'signSignature', 'signatures'] .forEach(function (k) { if (toRet[k] === null) { delete toRet[k]; } }); return toRet; }; BaseLiskCodec.prototype.transform = function (from) { var toRet = { amount: 0, asset: null, fee: null, id: null, recipientId: null, requesterPublicKey: null, senderId: null, senderPublicKey: null, timestamp: null, type: this.type, }; if (empty(from.fee)) { toRet.fee = this.calcFees(from); } else { toRet.fee = parseInt(from.fee, 10); } if (empty(from.sender.publicKey)) { throw new Error('Please set sender publicKey'); } toRet.senderPublicKey = from.sender.publicKey; toRet.senderId = from.sender.address || from.sender.address; if (!empty(from.nonce)) { toRet.timestamp = parseInt(from.nonce, 10); } else { toRet.timestamp = parseInt(this.createNonce(), 10); } // if (tx.kind === 'vote') { // const votes: string[] = []; // for (const pref of tx.preferences) { // votes.push(`${pref.action}${pref.delegateIdentifier.toString('hex')}`); // } // toRet.asset = { votes } as any; // } else if (tx.kind === 'register-delegate') { // toRet.asset = { delegate: { username: tx.identifier } } as any; // } else if (tx.kind === 'second-signature') { // toRet.asset = { signature: { publicKey: tx.publicKey.toString('hex') } } as any; // } else if (tx.kind === 'multisignature') { // toRet.asset = { // multisignature: { // keysgroup: tx.config.added // .map((p) => `+${p.toString('hex')}`) // .concat(tx.config.removed // .map((p) => `-${p.toString('hex')}`) // ), // lifetime : tx.lifetime, // min : tx.min, // }, // } as any; // } else if (tx.kind === 'send') { // if (!empty(tx.memo)) { // toRet.asset = { data: tx.memo } as any; // } // } if (from.signature) { toRet.signature = from.signature; } if (from.extraSignatures) { if (from.extraSignatures.length === 1) { toRet.signSignature = from.extraSignatures[0]; } else { toRet.signatures = from.extraSignatures; } } return toRet; }; BaseLiskCodec.prototype.createNonce = function () { return "" + Math.floor((Date.now() - Date.UTC(2016, 4, 24, 17, 0, 0, 0)) / 1000); }; BaseLiskCodec.prototype.fromBytes = function (buff) { throw new Error('Cant derive a lsk transaction from buffer inequivocably'); }; return BaseLiskCodec; }()); exports.BaseLiskCodec = BaseLiskCodec;