evtjs
Version:
Javascript API Bindings for the everiToken blockchain.
209 lines (168 loc) • 6.35 kB
JavaScript
;
var _regenerator = require("babel-runtime/regenerator");
var _regenerator2 = _interopRequireDefault(_regenerator);
var _asyncToGenerator2 = require("babel-runtime/helpers/asyncToGenerator");
var _asyncToGenerator3 = _interopRequireDefault(_asyncToGenerator2);
var _classCallCheck2 = require("babel-runtime/helpers/classCallCheck");
var _classCallCheck3 = _interopRequireDefault(_classCallCheck2);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
var ecc = require("./ecc/index");
var EvtKey = function EvtKey() {
(0, _classCallCheck3.default)(this, EvtKey);
};
/**
* Convert from private key to public key
* @param {*} privateKey privateKey in WIF format
*/
EvtKey.privateToPublic = function (privateKeyInWif) {
return ecc.privateToPublic(privateKeyInWif);
};
/**
* Generates a private key for evt and returns a Promise, the return value is a WIF
*/
EvtKey.randomPrivateKey = (0, _asyncToGenerator3.default)( /*#__PURE__*/_regenerator2.default.mark(function _callee() {
return _regenerator2.default.wrap(function _callee$(_context) {
while (1) {
switch (_context.prev = _context.next) {
case 0:
ecc.initialize();
_context.next = 3;
return ecc.randomKey();
case 3:
return _context.abrupt("return", _context.sent);
case 4:
case "end":
return _context.stop();
}
}
}, _callee, this);
}));
/**
* Generates a private key for evt from a Buffer
*/
EvtKey.privateKeyFromBuffer = function (bufferHex) {
return ecc.PrivateKey.fromBuffer(new Buffer(bufferHex, "hex")).toString();
};
/**
* Generates a buffer from private key
*/
EvtKey.privateKeyToBufferHex = function (privateKey) {
return ecc.PrivateKey.fromString(privateKey).toBuffer().toString("hex");
};
/**
* Generates a public key for evt from a compressed Buffer
*/
EvtKey.publicKeyFromCompressedBuffer = function (bufferHex) {
return ecc.PublicKey.fromBuffer(new Buffer(bufferHex, "hex")).toString();
};
/**
* Generates a private key for evt in specific seed. Note: The same seed produces the same private key every time. At least 128 random bits should be used to produce a good private key.
* @param {string} seed The seed string
*/
EvtKey.seedPrivateKey = function (seed) {
return ecc.seedPrivate(seed);
};
/**
* Check if a public key is valid.
* @param {*} key public key
*/
EvtKey.isValidPublicKey = function (key) {
if (typeof key !== "string" || key.length < 8) return false;
if (!key.startsWith("EVT")) return false;
return ecc.isValidPublic("EVT" + key.substr(3)) || ecc.isValidPublic("EOS" + key.substr(3));
};
EvtKey.sign = function (buf, privateKey, encoding) {
return ecc.sign(buf, privateKey, encoding);
};
EvtKey.signHash = function (buf, privateKey, encoding) {
return ecc.signHash(buf, privateKey, encoding);
};
/**
* Check if a address is valid.
* @param {*} key address
*/
EvtKey.isValidAddress = function (address) {
if (typeof address !== "string" || address.length < 8) return false;
if (!address.startsWith("EVT")) return false;
if (address === "EVT00000000000000000000000000000000000000000000000000") return true;
if (address.length == 53 && address[3] == "0") return true;
return ecc.isValidPublic("EVT" + address.substr(3));
};
/**
* return safe random bytes as hex.
*/
EvtKey.random32BytesAsHex = (0, _asyncToGenerator3.default)( /*#__PURE__*/_regenerator2.default.mark(function _callee2() {
return _regenerator2.default.wrap(function _callee2$(_context2) {
while (1) {
switch (_context2.prev = _context2.next) {
case 0:
_context2.next = 2;
return ecc.initialize();
case 2:
return _context2.abrupt("return", ecc.key_utils.random32ByteBuffer({ safe: true }).toString("hex"));
case 3:
case "end":
return _context2.stop();
}
}
}, _callee2, this);
}));
/**
* return safe random bytes.
*/
EvtKey.random32Bytes = (0, _asyncToGenerator3.default)( /*#__PURE__*/_regenerator2.default.mark(function _callee3() {
return _regenerator2.default.wrap(function _callee3$(_context3) {
while (1) {
switch (_context3.prev = _context3.next) {
case 0:
_context3.next = 2;
return ecc.initialize();
case 2:
return _context3.abrupt("return", ecc.key_utils.random32ByteBuffer({ safe: true }));
case 3:
case "end":
return _context3.stop();
}
}
}, _callee3, this);
}));
/**
* return a promise that resolves a safe random string to be used in name128 format.
*/
EvtKey.randomName128 = (0, _asyncToGenerator3.default)( /*#__PURE__*/_regenerator2.default.mark(function _callee4() {
var buffer, range, ret, i;
return _regenerator2.default.wrap(function _callee4$(_context4) {
while (1) {
switch (_context4.prev = _context4.next) {
case 0:
_context4.next = 2;
return ecc.initialize();
case 2:
buffer = ecc.key_utils.random32ByteBuffer({ safe: true });
range = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz.-";
ret = "";
for (i = 0; i < 21; ++i) {
ret += range[buffer.readUInt8(i) % range.length];
}
return _context4.abrupt("return", ret);
case 7:
case "end":
return _context4.stop();
}
}
}, _callee4, this);
}));
/**
* Check if a private key is valid.
* @param {*} key wif format of a private key
*/
EvtKey.isValidPrivateKey = function (key) {
return ecc.isValidPrivate(key);
};
/**
* return the address representing a null address.
*/
EvtKey.getNullAddress = function () {
return "EVT00000000000000000000000000000000000000000000000000";
};
module.exports = EvtKey;