dpos-offline
Version:
Offline Signing Transactions for DPOS Blockchains
86 lines (85 loc) • 2.9 kB
JavaScript
;
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 Long = require("long");
var sha256_1 = require("../../../utils/sha256");
var RiseIdsHandler = /** @class */ (function () {
function RiseIdsHandler() {
}
RiseIdsHandler.addressFromBytes = function (bytes) {
if (bytes.length === 0) {
return null;
}
if (bytes.length === 8) {
return Long.fromBytes(__spread(bytes), true, false).toString(10) + "R";
}
else {
return bech32.encode(bytes.slice(0, 4).toString('ascii'), bytes.slice(4));
}
};
RiseIdsHandler.addressFromPubData = function (pubKey) {
if (pubKey[0] === 1 && pubKey.length === 33) {
return bech32.encode('rise', pubKey);
}
var hash = sha256_1.toSha256(pubKey);
var temp = [];
for (var i = 0; i < 8; i++) {
temp.push(hash[7 - i]);
}
return Long.fromBytesBE(temp, true).toString() + "L";
};
RiseIdsHandler.addressToBytes = function (address) {
if (!address) {
return Buffer.alloc(0);
}
if (/^[0-9]+R$/i.test(address)) {
if (address.substring(-1) === 'r') {
throw new Error('Invalid address');
}
return new Buffer(Long.fromString(address.slice(0, -1)).toBytesBE());
}
else {
var res = bech32.decode(address);
return Buffer.concat([
Buffer.from(res.prefix, 'ascii'),
new Buffer(res.data),
]);
}
};
// public static calcBlockIdFromBytes(bytes: Buffer): string {
// return this.toBigInt(bytes).toString();
// }
RiseIdsHandler.calcTxIdFromBytes = function (bytes) {
return "" + this.toBigInt(bytes);
};
RiseIdsHandler.toBigInt = function (bytes) {
var hash = sha256_1.toSha256(bytes);
var tmp = Buffer.alloc(8);
for (var i = 0; i < 8; i++) {
tmp[i] = hash[7 - i];
}
return Long.fromBytes(__spread(tmp), true, false).toString(10);
};
return RiseIdsHandler;
}());
exports.RiseIdsHandler = RiseIdsHandler;