dpos-offline
Version:
Offline Signing Transactions for DPOS Blockchains
103 lines (102 loc) • 3.6 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 empty = require("is-empty");
var toTransportable_1 = require("../../../utils/toTransportable");
function isAddressV2(addr) {
return !/[0-9]+R/.test(addr);
}
var BaseRiseCodec = /** @class */ (function () {
function BaseRiseCodec(type, identifier) {
this.type = type;
this.identifier = identifier;
}
BaseRiseCodec.prototype.calcFullBytes = function (tx) {
return Buffer.concat(__spread([
this.calcBytes(tx)
], tx.signatures));
};
BaseRiseCodec.prototype.fromPostable = function (ptx) {
return __assign({}, ptx, { senderPubData: (ptx.senderPubData ? Buffer.from(ptx.senderPubData, 'hex') : null), signatures: (ptx.signatures ? ptx.signatures.map(function (s) { return Buffer.from(s, 'hex'); }) : null) });
};
BaseRiseCodec.prototype.toPostable = function (tx) {
return toTransportable_1.toTransportable(tx);
};
BaseRiseCodec.prototype.transform = function (from) {
var _a;
var toRet = {
amount: '0',
asset: null,
fee: null,
id: null,
recipientId: null,
senderId: null,
senderPubData: null,
signatures: [],
timestamp: null,
type: this.type,
version: 0,
};
if (empty(from.fee)) {
toRet.fee = "" + this.calcFees(from);
}
else {
toRet.fee = from.fee;
}
if (empty(from.sender.publicKey)) {
throw new Error('Please set sender publicKey');
}
toRet.senderPubData = isAddressV2(from.sender.address)
? Buffer.concat([new Buffer([1]), from.sender.publicKey])
: 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 (from.signature) {
toRet.signatures.push(from.signature);
}
if (from.extraSignatures) {
(_a = toRet.signatures).push.apply(_a, __spread(from.extraSignatures));
}
return toRet;
};
BaseRiseCodec.prototype.createNonce = function () {
return "" + Math.floor((Date.now() - Date.UTC(2016, 4, 24, 17, 0, 0, 0)) / 1000);
};
return BaseRiseCodec;
}());
exports.BaseRiseCodec = BaseRiseCodec;