UNPKG

5.12 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.Signature = void 0;
4var BN = require("bn.js");
5var eosjs_numeric_1 = require("./eosjs-numeric");
6var eosjs_key_conversions_1 = require("./eosjs-key-conversions");
7/** Represents/stores a Signature and provides easy conversion for use with `elliptic` lib */
8var Signature = /** @class */ (function () {
9 function Signature(signature, ec) {
10 this.signature = signature;
11 this.ec = ec;
12 }
13 /** Instantiate Signature from an EOSIO-format Signature */
14 Signature.fromString = function (sig, ec) {
15 var signature = eosjs_numeric_1.stringToSignature(sig);
16 if (!ec) {
17 ec = eosjs_key_conversions_1.constructElliptic(signature.type);
18 }
19 return new Signature(signature, ec);
20 };
21 /** Instantiate Signature from an `elliptic`-format Signature */
22 Signature.fromElliptic = function (ellipticSig, keyType, ec) {
23 var r = ellipticSig.r.toArray('be', 32);
24 var s = ellipticSig.s.toArray('be', 32);
25 var eosioRecoveryParam;
26 if (keyType === eosjs_numeric_1.KeyType.k1 || keyType === eosjs_numeric_1.KeyType.r1) {
27 eosioRecoveryParam = ellipticSig.recoveryParam + 27;
28 if (ellipticSig.recoveryParam <= 3) {
29 eosioRecoveryParam += 4;
30 }
31 }
32 else if (keyType === eosjs_numeric_1.KeyType.wa) {
33 eosioRecoveryParam = ellipticSig.recoveryParam;
34 }
35 var sigData = new Uint8Array([eosioRecoveryParam].concat(r, s));
36 if (!ec) {
37 ec = eosjs_key_conversions_1.constructElliptic(keyType);
38 }
39 return new Signature({
40 type: keyType,
41 data: sigData,
42 }, ec);
43 };
44 /** Export Signature as `elliptic`-format Signature
45 * NOTE: This isn't an actual elliptic-format Signature, as ec.Signature is not exported by the library.
46 * That's also why the return type is `any`. We're *actually* returning an object with the 3 params
47 * not an ec.Signature.
48 * Further NOTE: @types/elliptic shows ec.Signature as exported; it is *not*. Hence the `any`.
49 */
50 Signature.prototype.toElliptic = function () {
51 var lengthOfR = 32;
52 var lengthOfS = 32;
53 var r = new BN(this.signature.data.slice(1, lengthOfR + 1));
54 var s = new BN(this.signature.data.slice(lengthOfR + 1, lengthOfR + lengthOfS + 1));
55 var ellipticRecoveryBitField;
56 if (this.signature.type === eosjs_numeric_1.KeyType.k1 || this.signature.type === eosjs_numeric_1.KeyType.r1) {
57 ellipticRecoveryBitField = this.signature.data[0] - 27;
58 if (ellipticRecoveryBitField > 3) {
59 ellipticRecoveryBitField -= 4;
60 }
61 }
62 else if (this.signature.type === eosjs_numeric_1.KeyType.wa) {
63 ellipticRecoveryBitField = this.signature.data[0];
64 }
65 var recoveryParam = ellipticRecoveryBitField & 3;
66 return { r: r, s: s, recoveryParam: recoveryParam };
67 };
68 /** Export Signature as EOSIO-format Signature */
69 Signature.prototype.toString = function () {
70 return eosjs_numeric_1.signatureToString(this.signature);
71 };
72 /** Export Signature in binary format */
73 Signature.prototype.toBinary = function () {
74 return this.signature.data;
75 };
76 /** Get key type from signature */
77 Signature.prototype.getType = function () {
78 return this.signature.type;
79 };
80 /** Verify a signature with a message or hashed message digest and public key */
81 Signature.prototype.verify = function (data, publicKey, shouldHash, encoding) {
82 if (shouldHash === void 0) { shouldHash = true; }
83 if (encoding === void 0) { encoding = 'utf8'; }
84 if (shouldHash) {
85 if (typeof data === 'string') {
86 data = Buffer.from(data, encoding);
87 }
88 data = this.ec.hash().update(data).digest();
89 }
90 var ellipticSignature = this.toElliptic();
91 var ellipticPublicKey = publicKey.toElliptic();
92 return this.ec.verify(data, ellipticSignature, ellipticPublicKey, encoding);
93 };
94 /** Recover a public key from a message or hashed message digest and signature */
95 Signature.prototype.recover = function (data, shouldHash, encoding) {
96 if (shouldHash === void 0) { shouldHash = true; }
97 if (encoding === void 0) { encoding = 'utf8'; }
98 if (shouldHash) {
99 if (typeof data === 'string') {
100 data = Buffer.from(data, encoding);
101 }
102 data = this.ec.hash().update(data).digest();
103 }
104 var ellipticSignature = this.toElliptic();
105 var recoveredPublicKey = this.ec.recoverPubKey(data, ellipticSignature, ellipticSignature.recoveryParam, encoding);
106 var ellipticKPub = this.ec.keyFromPublic(recoveredPublicKey);
107 return eosjs_key_conversions_1.PublicKey.fromElliptic(ellipticKPub, this.getType(), this.ec);
108 };
109 return Signature;
110}());
111exports.Signature = Signature;
112//# sourceMappingURL=Signature.js.map
\No newline at end of file