dpos-offline
Version:
Offline Signing Transactions for DPOS Blockchains
98 lines (97 loc) • 3.92 kB
JavaScript
;
var __extends = (this && this.__extends) || (function () {
var extendStatics = function (d, b) {
extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
return extendStatics(d, b);
};
return function (d, b) {
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
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 varuint = require("varuint-bitcoin");
var base_v2_1 = require("./base_v2");
function encodeVote(username) {
var usernameBuf = Buffer.from(username, 'utf8');
return Buffer.concat([varuint.encode(usernameBuf.length), usernameBuf]);
}
var RiseVoteV2TxCodec = /** @class */ (function (_super) {
__extends(RiseVoteV2TxCodec, _super);
function RiseVoteV2TxCodec() {
return _super.call(this, 13, 'vote-v2') || this;
}
RiseVoteV2TxCodec.prototype.transform = function (from) {
var added = from.preferences
.filter(function (a) { return a.action === '+'; })
.map(function (a) { return a.delegateIdentifier; });
var removed = from.preferences
.filter(function (a) { return a.action === '-'; })
.map(function (a) { return a.delegateIdentifier; });
return __assign({}, _super.prototype.transform.call(this, from), { asset: { added: added, removed: removed } });
};
RiseVoteV2TxCodec.prototype.calcFees = function (tx) {
return 100000000;
};
RiseVoteV2TxCodec.prototype.assetBytes = function (tx) {
return Buffer.concat(__spread([
varuint.encode(tx.asset.added.length)
], tx.asset.added.map(encodeVote), [
varuint.encode(tx.asset.removed.length)
], tx.asset.removed.map(encodeVote)));
};
RiseVoteV2TxCodec.prototype.assetFromBytes = function (bytes) {
var offset = 0;
function decodeVotesArr() {
var totalEntries = varuint.decode(bytes, offset);
offset += varuint.decode.bytes;
var toRet = [];
for (var i = 0; i < totalEntries; i++) {
var usernameLength = varuint.decode(bytes, offset);
offset += varuint.decode.bytes;
toRet.push(bytes.slice(offset, offset + usernameLength).toString('utf8'));
offset += usernameLength;
}
return toRet;
}
var added = decodeVotesArr();
var removed = decodeVotesArr();
return { added: added, removed: removed };
};
return RiseVoteV2TxCodec;
}(base_v2_1.BaseRiseV2Codec));
exports.RiseVoteV2TxCodec = RiseVoteV2TxCodec;