@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 CHANGED
@@ -5657,7 +5657,7 @@ function weierstrass(curveDef) {
5657
5657
  return drbg(seed, k2sig);
5658
5658
  }
5659
5659
  Point3.BASE._setWindowSize(8);
5660
- function verify2(signature, msgHash, publicKey, opts = defaultVerOpts) {
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: verify2,
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 = await privateKey.sign(Buffer.from(message).toString("hex"));
27958
+ const signature = new SigningKey(`0x${privateKey.privateKey}`).sign(hashMessage(message));
27996
27959
  return _Message.new({
27997
27960
  publicKey,
27998
- signature: signature.r + signature.s + Number(signature.v).toString(16),
27961
+ signature: signature.serialized,
27999
27962
  message
28000
27963
  });
28001
27964
  }
28002
27965
  verify() {
28003
- const message = Buffer.from(keccak256(Buffer.from(this.message)).slice(2), "hex");
28004
- const signature = Buffer.from(this.signature.slice(0, 128), "hex");
28005
- const publicKey = Buffer.from(this.publicKey, "hex");
28006
- return verify(signature, message, publicKey);
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":"AAEA,OAAO,EAAE,aAAa,EAAE,MAAM,SAAS,CAAC;AAIxC,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;IAaxE,MAAM,IAAI,OAAO;IAQjB,QAAQ,IAAI,MAAM;IAIlB,QAAQ,IAAI,MAAM,EAAE;IAIpB,MAAM,IAAI,aAAa;CAOvB"}
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"}
@@ -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 { keccak256 } from "ethers";
13
- import { verify } from "@noble/secp256k1";
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
- const signature = yield privateKey.sign(Buffer.from(message).toString("hex"));
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.r + signature.s + Number(signature.v).toString(16),
32
+ signature: signature.serialized,
31
33
  message,
32
34
  });
33
35
  });
34
36
  }
35
37
  verify() {
36
- const message = Buffer.from(keccak256(Buffer.from(this.message)).slice(2), "hex");
37
- const signature = Buffer.from(this.signature.slice(0, 128), "hex");
38
- const publicKey = Buffer.from(this.publicKey, "hex");
39
- return verify(signature, message, publicKey);
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
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@arkecosystem/typescript-crypto",
3
- "version": "0.0.11",
3
+ "version": "0.0.13",
4
4
  "engines": {
5
5
  "node": ">=20.12.2"
6
6
  },
@@ -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 { keccak256 } from "ethers";
5
- import { verify } from "@noble/secp256k1";
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
- const signature = await privateKey.sign(Buffer.from(message).toString("hex"));
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.r + signature.s + Number(signature.v).toString(16),
32
+ signature: signature.serialized,
31
33
  message,
32
34
  });
33
35
  }
34
36
 
35
37
  verify(): boolean {
36
- const message = Buffer.from(keccak256(Buffer.from(this.message)).slice(2), "hex");
37
- const signature = Buffer.from(this.signature.slice(0, 128), "hex");
38
- const publicKey = Buffer.from(this.publicKey, "hex");
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 verify(signature, message, publicKey);
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": "0e2e53409be748834cac44052817ecef569b429a0492aa6bbc0d934eb71a09547e77aeef33d45669bbcba0498149f0e2b637fe8905186e08a5410c6f2b013bb40"
4
+ "signature": "0x2bdd0c58ff8a25f456065fb731c73308a25d0a09f351f23e3c7dd3882776d33d626b0cafc0b99dd7504b24f6ecd2e036a267c8e5e005f36dcbc03b2e33fa7fc31c"
5
5
  }