UNPKG

2.4 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.PublicKey = void 0;
4var eosjs_numeric_1 = require("./eosjs-numeric");
5var eosjs_key_conversions_1 = require("./eosjs-key-conversions");
6/** Represents/stores a public key and provides easy conversion for use with `elliptic` lib */
7var PublicKey = /** @class */ (function () {
8 function PublicKey(key, ec) {
9 this.key = key;
10 this.ec = ec;
11 }
12 /** Instantiate public key from an EOSIO-format public key */
13 PublicKey.fromString = function (publicKeyStr, ec) {
14 var key = eosjs_numeric_1.stringToPublicKey(publicKeyStr);
15 if (!ec) {
16 ec = eosjs_key_conversions_1.constructElliptic(key.type);
17 }
18 return new PublicKey(key, ec);
19 };
20 /** Instantiate public key from an `elliptic`-format public key */
21 PublicKey.fromElliptic = function (publicKey, keyType, ec) {
22 var x = publicKey.getPublic().getX().toArray('be', 32);
23 var y = publicKey.getPublic().getY().toArray('be', 32);
24 if (!ec) {
25 ec = eosjs_key_conversions_1.constructElliptic(keyType);
26 }
27 return new PublicKey({
28 type: keyType,
29 data: new Uint8Array([(y[31] & 1) ? 3 : 2].concat(x)),
30 }, ec);
31 };
32 /** Export public key as EOSIO-format public key */
33 PublicKey.prototype.toString = function () {
34 return eosjs_numeric_1.publicKeyToString(this.key);
35 };
36 /** Export public key as Legacy EOSIO-format public key */
37 PublicKey.prototype.toLegacyString = function () {
38 return eosjs_numeric_1.publicKeyToLegacyString(this.key);
39 };
40 /** Export public key as `elliptic`-format public key */
41 PublicKey.prototype.toElliptic = function () {
42 return this.ec.keyPair({
43 pub: Buffer.from(this.key.data),
44 });
45 };
46 /** Get key type from key */
47 PublicKey.prototype.getType = function () {
48 return this.key.type;
49 };
50 /** Validate a public key */
51 PublicKey.prototype.isValid = function () {
52 try {
53 var ellipticPublicKey = this.toElliptic();
54 var validationObj = ellipticPublicKey.validate();
55 return validationObj.result;
56 }
57 catch (_a) {
58 return false;
59 }
60 };
61 return PublicKey;
62}());
63exports.PublicKey = PublicKey;
64//# sourceMappingURL=PublicKey.js.map
\No newline at end of file