@arkecosystem/typescript-crypto 0.0.11 → 0.0.13
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.js +8 -45
- package/dist/utils/Message.d.ts.map +1 -1
- package/dist/utils/Message.js +12 -9
- package/package.json +1 -1
- package/src/utils/Message.ts +12 -9
- package/tests/fixtures/message-sign.json +1 -1
package/dist/index.js
CHANGED
|
@@ -5657,7 +5657,7 @@ function weierstrass(curveDef) {
|
|
|
5657
5657
|
return drbg(seed, k2sig);
|
|
5658
5658
|
}
|
|
5659
5659
|
Point3.BASE._setWindowSize(8);
|
|
5660
|
-
function
|
|
5660
|
+
function verify(signature, msgHash, publicKey, opts = defaultVerOpts) {
|
|
5661
5661
|
const sg = signature;
|
|
5662
5662
|
msgHash = ensureBytes("msgHash", msgHash);
|
|
5663
5663
|
publicKey = ensureBytes("publicKey", publicKey);
|
|
@@ -5707,7 +5707,7 @@ function weierstrass(curveDef) {
|
|
|
5707
5707
|
getPublicKey,
|
|
5708
5708
|
getSharedSecret,
|
|
5709
5709
|
sign,
|
|
5710
|
-
verify
|
|
5710
|
+
verify,
|
|
5711
5711
|
ProjectivePoint: Point3,
|
|
5712
5712
|
Signature: Signature3,
|
|
5713
5713
|
utils
|
|
@@ -23260,7 +23260,6 @@ var cr = () => (
|
|
|
23260
23260
|
);
|
|
23261
23261
|
var _hmacSync;
|
|
23262
23262
|
var optS = { lowS: true };
|
|
23263
|
-
var optV = { lowS: true };
|
|
23264
23263
|
var prepSig = (msgh, priv, opts = optS) => {
|
|
23265
23264
|
if (["der", "recovered", "canonical"].some((k) => k in opts))
|
|
23266
23265
|
err("option not supported");
|
|
@@ -23368,42 +23367,6 @@ var signAsync = async (msgh, priv, opts = optS) => {
|
|
|
23368
23367
|
const { seed, k2sig } = prepSig(msgh, priv, opts);
|
|
23369
23368
|
return hmacDrbg(true)(seed, k2sig);
|
|
23370
23369
|
};
|
|
23371
|
-
var verify = (sig, msgh, pub, opts = optV) => {
|
|
23372
|
-
let { lowS } = opts;
|
|
23373
|
-
if (lowS == null)
|
|
23374
|
-
lowS = true;
|
|
23375
|
-
if ("strict" in opts)
|
|
23376
|
-
err("option not supported");
|
|
23377
|
-
let sig_, h, P2;
|
|
23378
|
-
const rs = sig && typeof sig === "object" && "r" in sig;
|
|
23379
|
-
if (!rs && toU8(sig).length !== 2 * fLen)
|
|
23380
|
-
err("signature must be 64 bytes");
|
|
23381
|
-
try {
|
|
23382
|
-
sig_ = rs ? new Signature2(sig.r, sig.s).assertValidity() : Signature2.fromCompact(sig);
|
|
23383
|
-
h = bits2int_modN(toU8(msgh));
|
|
23384
|
-
P2 = pub instanceof Point2 ? pub.ok() : Point2.fromHex(pub);
|
|
23385
|
-
} catch (e) {
|
|
23386
|
-
return false;
|
|
23387
|
-
}
|
|
23388
|
-
if (!sig_)
|
|
23389
|
-
return false;
|
|
23390
|
-
const { r, s } = sig_;
|
|
23391
|
-
if (lowS && high(s))
|
|
23392
|
-
return false;
|
|
23393
|
-
let R;
|
|
23394
|
-
try {
|
|
23395
|
-
const is = inv(s, N3);
|
|
23396
|
-
const u1 = M(h * is, N3);
|
|
23397
|
-
const u2 = M(r * is, N3);
|
|
23398
|
-
R = G.mulAddQUns(P2, u1, u2).aff();
|
|
23399
|
-
} catch (error) {
|
|
23400
|
-
return false;
|
|
23401
|
-
}
|
|
23402
|
-
if (!R)
|
|
23403
|
-
return false;
|
|
23404
|
-
const v = M(R.x, N3);
|
|
23405
|
-
return v === r;
|
|
23406
|
-
};
|
|
23407
23370
|
var hashToPrivateKey = (hash2) => {
|
|
23408
23371
|
hash2 = toU8(hash2);
|
|
23409
23372
|
if (hash2.length < fLen + 8 || hash2.length > 1024)
|
|
@@ -27992,18 +27955,18 @@ var Message = class _Message {
|
|
|
27992
27955
|
static async sign(message, passphrase) {
|
|
27993
27956
|
const privateKey = PrivateKey.fromPassphrase(passphrase);
|
|
27994
27957
|
const publicKey = PublicKey.fromPassphrase(passphrase).publicKey;
|
|
27995
|
-
const signature =
|
|
27958
|
+
const signature = new SigningKey(`0x${privateKey.privateKey}`).sign(hashMessage(message));
|
|
27996
27959
|
return _Message.new({
|
|
27997
27960
|
publicKey,
|
|
27998
|
-
signature: signature.
|
|
27961
|
+
signature: signature.serialized,
|
|
27999
27962
|
message
|
|
28000
27963
|
});
|
|
28001
27964
|
}
|
|
28002
27965
|
verify() {
|
|
28003
|
-
const message =
|
|
28004
|
-
const
|
|
28005
|
-
const
|
|
28006
|
-
return
|
|
27966
|
+
const message = new Uint8Array(new TextEncoder().encode(this.message));
|
|
27967
|
+
const address = Address.fromPublicKey(this.publicKey);
|
|
27968
|
+
const signerAddress = verifyMessage(message, this.signature);
|
|
27969
|
+
return signerAddress === address;
|
|
28007
27970
|
}
|
|
28008
27971
|
toString() {
|
|
28009
27972
|
return JSON.stringify(this.toJson());
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Message.d.ts","sourceRoot":"","sources":["../../src/utils/Message.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"Message.d.ts","sourceRoot":"","sources":["../../src/utils/Message.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,aAAa,EAAE,MAAM,SAAS,CAAC;AAKxC,qBAAa,OAAO;IACZ,SAAS,EAAE,MAAM,CAAC;IAClB,SAAS,EAAE,MAAM,CAAC;IAClB,OAAO,EAAE,MAAM,CAAC;gBAEX,OAAO,EAAE,aAAa;IAMlC,MAAM,CAAC,GAAG,CAAC,OAAO,EAAE,aAAa,GAAG,OAAO;WAI9B,IAAI,CAAC,OAAO,EAAE,MAAM,EAAE,UAAU,EAAE,MAAM,GAAG,OAAO,CAAC,OAAO,CAAC;IAexE,MAAM,IAAI,OAAO;IASjB,QAAQ,IAAI,MAAM;IAIlB,QAAQ,IAAI,MAAM,EAAE;IAIpB,MAAM,IAAI,aAAa;CAOvB"}
|
package/dist/utils/Message.js
CHANGED
|
@@ -7,10 +7,10 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
7
7
|
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
8
8
|
});
|
|
9
9
|
};
|
|
10
|
-
import { PrivateKey } from "@/identities/PrivateKey";
|
|
11
10
|
import { PublicKey } from "@/identities/PublicKey";
|
|
12
|
-
import {
|
|
13
|
-
import {
|
|
11
|
+
import { hashMessage, verifyMessage } from "ethers";
|
|
12
|
+
import { Address, PrivateKey } from "@/identities";
|
|
13
|
+
import { SigningKey } from "ethers";
|
|
14
14
|
export class Message {
|
|
15
15
|
constructor(message) {
|
|
16
16
|
this.publicKey = message.publicKey;
|
|
@@ -24,19 +24,22 @@ export class Message {
|
|
|
24
24
|
return __awaiter(this, void 0, void 0, function* () {
|
|
25
25
|
const privateKey = PrivateKey.fromPassphrase(passphrase);
|
|
26
26
|
const publicKey = PublicKey.fromPassphrase(passphrase).publicKey;
|
|
27
|
-
|
|
27
|
+
// Sign messages using eip-191 signed data specification, to be aligned with ledger signing workflow.
|
|
28
|
+
// @see https://developers.ledger.com/docs/ledger-live/discover/integration/wallet-api/server/handlers/message
|
|
29
|
+
const signature = new SigningKey(`0x${privateKey.privateKey}`).sign(hashMessage(message));
|
|
28
30
|
return Message.new({
|
|
29
31
|
publicKey,
|
|
30
|
-
signature: signature.
|
|
32
|
+
signature: signature.serialized,
|
|
31
33
|
message,
|
|
32
34
|
});
|
|
33
35
|
});
|
|
34
36
|
}
|
|
35
37
|
verify() {
|
|
36
|
-
const message =
|
|
37
|
-
const
|
|
38
|
-
|
|
39
|
-
|
|
38
|
+
const message = new Uint8Array(new TextEncoder().encode(this.message));
|
|
39
|
+
const address = Address.fromPublicKey(this.publicKey);
|
|
40
|
+
// Messages need to be signed using eip-191 signed data specification
|
|
41
|
+
const signerAddress = verifyMessage(message, this.signature);
|
|
42
|
+
return signerAddress === address;
|
|
40
43
|
}
|
|
41
44
|
toString() {
|
|
42
45
|
return JSON.stringify(this.toJson());
|
package/package.json
CHANGED
package/src/utils/Message.ts
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import { PrivateKey } from "@/identities/PrivateKey";
|
|
2
1
|
import { PublicKey } from "@/identities/PublicKey";
|
|
3
2
|
import { SignedMessage } from "@/types";
|
|
4
|
-
import {
|
|
5
|
-
import {
|
|
3
|
+
import { hashMessage, verifyMessage } from "ethers";
|
|
4
|
+
import { Address, PrivateKey } from "@/identities";
|
|
5
|
+
import { SigningKey } from "ethers";
|
|
6
6
|
|
|
7
7
|
export class Message {
|
|
8
8
|
public publicKey: string;
|
|
@@ -23,21 +23,24 @@ export class Message {
|
|
|
23
23
|
const privateKey = PrivateKey.fromPassphrase(passphrase);
|
|
24
24
|
const publicKey = PublicKey.fromPassphrase(passphrase).publicKey;
|
|
25
25
|
|
|
26
|
-
|
|
26
|
+
// Sign messages using eip-191 signed data specification, to be aligned with ledger signing workflow.
|
|
27
|
+
// @see https://developers.ledger.com/docs/ledger-live/discover/integration/wallet-api/server/handlers/message
|
|
28
|
+
const signature = new SigningKey(`0x${privateKey.privateKey}`).sign(hashMessage(message));
|
|
27
29
|
|
|
28
30
|
return Message.new({
|
|
29
31
|
publicKey,
|
|
30
|
-
signature: signature.
|
|
32
|
+
signature: signature.serialized,
|
|
31
33
|
message,
|
|
32
34
|
});
|
|
33
35
|
}
|
|
34
36
|
|
|
35
37
|
verify(): boolean {
|
|
36
|
-
const message =
|
|
37
|
-
const
|
|
38
|
-
|
|
38
|
+
const message = new Uint8Array(new TextEncoder().encode(this.message));
|
|
39
|
+
const address = Address.fromPublicKey(this.publicKey);
|
|
40
|
+
// Messages need to be signed using eip-191 signed data specification
|
|
41
|
+
const signerAddress = verifyMessage(message, this.signature);
|
|
39
42
|
|
|
40
|
-
return
|
|
43
|
+
return signerAddress === address;
|
|
41
44
|
}
|
|
42
45
|
|
|
43
46
|
toString(): string {
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
{
|
|
2
2
|
"message": "Hello, world!",
|
|
3
3
|
"publicKey": "0243333347c8cbf4e3cbc7a96964181d02a2b0c854faa2fef86b4b8d92afcf473d",
|
|
4
|
-
"signature": "
|
|
4
|
+
"signature": "0x2bdd0c58ff8a25f456065fb731c73308a25d0a09f351f23e3c7dd3882776d33d626b0cafc0b99dd7504b24f6ecd2e036a267c8e5e005f36dcbc03b2e33fa7fc31c"
|
|
5
5
|
}
|