@ocap/mcrypto 1.28.8 → 1.29.0

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/README.md CHANGED
@@ -1,6 +1,5 @@
1
1
  ![mcrypto:js](https://www.arcblock.io/.netlify/functions/badge/?text=mcrypto:js)
2
2
 
3
- [![styled with prettier](https://img.shields.io/badge/styled_with-prettier-ff69b4.svg)](https://github.com/prettier/prettier)
4
3
  [![docs](https://img.shields.io/badge/powered%20by-arcblock-green.svg)](https://docs.arcblock.io)
5
4
  [![Gitter](https://badges.gitter.im/ArcBlock/community.svg)](https://gitter.im/ArcBlock/community?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge)
6
5
 
@@ -7,18 +7,20 @@ import crypto from "node:crypto";
7
7
  //#region src/crypter/aes.ts
8
8
  var AesCrypter = class extends crypter_default {
9
9
  encrypt(message, secret, encoding = "hex") {
10
- const key = sha3_default.hash256(secret, 1, "buffer");
11
- const cipher = crypto.createCipheriv("aes-256-ecb", key, "");
10
+ const keyBuffer = sha3_default.hash256(secret, 1, "buffer");
11
+ const key = Uint8Array.from(keyBuffer);
12
+ const cipher = crypto.createCipheriv("aes-256-ecb", key, null);
12
13
  cipher.setAutoPadding(true);
13
- const output = cipher.update(toBuffer(message));
14
- return encode(Buffer.concat([output, cipher.final()]), encoding);
14
+ const output = cipher.update(Uint8Array.from(toBuffer(message)));
15
+ return encode(Uint8Array.from(Buffer.concat([Uint8Array.from(output), Uint8Array.from(cipher.final())])), encoding);
15
16
  }
16
17
  decrypt(message, secret, encoding = "hex") {
17
- const key = sha3_default.hash256(secret, 1, "buffer");
18
- const decipher = crypto.createDecipheriv("aes-256-ecb", key, "");
18
+ const keyBuffer = sha3_default.hash256(secret, 1, "buffer");
19
+ const key = Uint8Array.from(keyBuffer);
20
+ const decipher = crypto.createDecipheriv("aes-256-ecb", key, null);
19
21
  decipher.setAutoPadding(true);
20
- const output = decipher.update(toBuffer(message));
21
- return encode(Buffer.concat([output, decipher.final()]), encoding);
22
+ const output = decipher.update(Uint8Array.from(toBuffer(message)));
23
+ return encode(Uint8Array.from(Buffer.concat([Uint8Array.from(output), Uint8Array.from(decipher.final())])), encoding);
22
24
  }
23
25
  };
24
26
  var aes_default = new AesCrypter();
@@ -26,7 +26,9 @@ var RSABrowserCrypter = class {
26
26
  return toBase58(new Uint8Array(encrypted));
27
27
  }
28
28
  async decrypt(message, key) {
29
- const decrypted = await crypto.decrypt({ name: RSA_ALGORITHM }, key, fromBase58(message));
29
+ const messageBuffer = fromBase58(message);
30
+ const data = Uint8Array.from(messageBuffer);
31
+ const decrypted = await crypto.decrypt({ name: RSA_ALGORITHM }, key, data);
30
32
  return Buffer.from(new Uint8Array(decrypted)).toString("utf8");
31
33
  }
32
34
  };
@@ -19,10 +19,10 @@ var RSACrypter = class extends crypter_default {
19
19
  });
20
20
  }
21
21
  encrypt(message, key, encoding = "hex") {
22
- return encode(crypto.publicEncrypt(key, toBuffer(message)), encoding);
22
+ return encode(crypto.publicEncrypt(key, Uint8Array.from(toBuffer(message))), encoding);
23
23
  }
24
24
  decrypt(message, key, encoding = "hex") {
25
- return encode(crypto.privateDecrypt(key, toBuffer(message)), encoding);
25
+ return encode(crypto.privateDecrypt(key, Uint8Array.from(toBuffer(message))), encoding);
26
26
  }
27
27
  };
28
28
  var rsa_default = new RSACrypter();
package/esm/index.d.mts CHANGED
@@ -38,7 +38,8 @@ interface SignerType {
38
38
  sign(data: BytesType, sk: BytesType, encoding?: 'buffer'): Buffer;
39
39
  sign(data: BytesType, sk: BytesType, encoding?: 'Uint8Array'): Uint8Array;
40
40
  sign(data: BytesType, sk: BytesType, encoding?: EncodingType): BytesType;
41
- verify(data: BytesType, pk: BytesType, signature: BytesType, extra?: any): boolean;
41
+ /** extra is JSON string for passkey signer containing {authenticatorData, clientDataJSON} */
42
+ verify(data: BytesType, pk: BytesType, signature: BytesType, extra?: string): boolean;
42
43
  ethHash?(data: string): string;
43
44
  ethSign?(data: string, sk: string): string;
44
45
  ethVerify?(data: string, pk: string, signature: BytesType): boolean;
@@ -18,8 +18,8 @@ var EthereumSigner = class extends Secp256k1Signer {
18
18
  const messageBytes = hexToBytes(isHexStrict(data) ? data : utf8ToHex(data));
19
19
  const messageBuffer = Buffer.from(messageBytes);
20
20
  const preamble = `\x19Ethereum Signed Message:\n${messageBytes.length}`;
21
- const preambleBuffer = Buffer.from(preamble);
22
- const ethMessage = Buffer.concat([preambleBuffer, messageBuffer]);
21
+ const preambleBuffer = Uint8Array.from(Buffer.from(preamble));
22
+ const ethMessage = Buffer.concat([preambleBuffer, Uint8Array.from(messageBuffer)]);
23
23
  return Hash.keccak256s(ethMessage);
24
24
  }
25
25
  ethSign(data, privateKey) {
@@ -1,5 +1,5 @@
1
1
  import signer_default from "../protocols/signer.mjs";
2
- import { fromBase64, toBase64, toBuffer } from "@ocap/util";
2
+ import { fromBase64, toBase64, toBuffer, toUint8Array } from "@ocap/util";
3
3
  import { decodeClientDataJSON, isoBase64URL, isoUint8Array, toHash, verifySignature } from "@simplewebauthn/server/helpers";
4
4
 
5
5
  //#region src/signer/passkey.ts
@@ -36,8 +36,8 @@ var PasskeySigner = class extends signer_default {
36
36
  if (decodeClientDataJSON(parsed.clientDataJSON).challenge !== toBase64(challenge)) throw new Error("challenge mismatch for passkey signature");
37
37
  return verifySignature({
38
38
  signature: isoBase64URL.toBuffer(typeof signature === "string" ? signature : toBase64(signature)),
39
- data: isoUint8Array.concat([authDataBuffer, clientDataHash]),
40
- credentialPublicKey: pk
39
+ data: isoUint8Array.concat([Uint8Array.from(authDataBuffer), clientDataHash]),
40
+ credentialPublicKey: toUint8Array(pk)
41
41
  });
42
42
  }
43
43
  };
@@ -72,7 +72,7 @@ var Secp256k1Signer = class extends signer_default {
72
72
  msg = toUint8Array(message);
73
73
  } catch (_err) {}
74
74
  let pkBuffer = toBuffer(pk);
75
- if (this.pkHasFormatPrefix === false && pkBuffer[0] !== 4 && pkBuffer.byteLength === 64) pkBuffer = Buffer.concat([Buffer.from([4]), pkBuffer]);
75
+ if (this.pkHasFormatPrefix === false && pkBuffer[0] !== 4 && pkBuffer.byteLength === 64) pkBuffer = Buffer.concat([Uint8Array.from([4]), Uint8Array.from(pkBuffer)]);
76
76
  return secp256k1.keyFromPublic(pkBuffer).verify(stripHexPrefix(msg), stripHexPrefix(toHex(signature)));
77
77
  }
78
78
  };
@@ -10,18 +10,20 @@ node_crypto = require_rolldown_runtime.__toESM(node_crypto);
10
10
  //#region src/crypter/aes.ts
11
11
  var AesCrypter = class extends require_protocols_crypter.default {
12
12
  encrypt(message, secret, encoding = "hex") {
13
- const key = require_hasher_sha3.default.hash256(secret, 1, "buffer");
14
- const cipher = node_crypto.default.createCipheriv("aes-256-ecb", key, "");
13
+ const keyBuffer = require_hasher_sha3.default.hash256(secret, 1, "buffer");
14
+ const key = Uint8Array.from(keyBuffer);
15
+ const cipher = node_crypto.default.createCipheriv("aes-256-ecb", key, null);
15
16
  cipher.setAutoPadding(true);
16
- const output = cipher.update((0, _ocap_util.toBuffer)(message));
17
- return require_encode.encode(Buffer.concat([output, cipher.final()]), encoding);
17
+ const output = cipher.update(Uint8Array.from((0, _ocap_util.toBuffer)(message)));
18
+ return require_encode.encode(Uint8Array.from(Buffer.concat([Uint8Array.from(output), Uint8Array.from(cipher.final())])), encoding);
18
19
  }
19
20
  decrypt(message, secret, encoding = "hex") {
20
- const key = require_hasher_sha3.default.hash256(secret, 1, "buffer");
21
- const decipher = node_crypto.default.createDecipheriv("aes-256-ecb", key, "");
21
+ const keyBuffer = require_hasher_sha3.default.hash256(secret, 1, "buffer");
22
+ const key = Uint8Array.from(keyBuffer);
23
+ const decipher = node_crypto.default.createDecipheriv("aes-256-ecb", key, null);
22
24
  decipher.setAutoPadding(true);
23
- const output = decipher.update((0, _ocap_util.toBuffer)(message));
24
- return require_encode.encode(Buffer.concat([output, decipher.final()]), encoding);
25
+ const output = decipher.update(Uint8Array.from((0, _ocap_util.toBuffer)(message)));
26
+ return require_encode.encode(Uint8Array.from(Buffer.concat([Uint8Array.from(output), Uint8Array.from(decipher.final())])), encoding);
25
27
  }
26
28
  };
27
29
  var aes_default = new AesCrypter();
@@ -28,7 +28,9 @@ var RSABrowserCrypter = class {
28
28
  return (0, _ocap_util.toBase58)(new Uint8Array(encrypted));
29
29
  }
30
30
  async decrypt(message, key) {
31
- const decrypted = await crypto.decrypt({ name: RSA_ALGORITHM }, key, (0, _ocap_util.fromBase58)(message));
31
+ const messageBuffer = (0, _ocap_util.fromBase58)(message);
32
+ const data = Uint8Array.from(messageBuffer);
33
+ const decrypted = await crypto.decrypt({ name: RSA_ALGORITHM }, key, data);
32
34
  return Buffer.from(new Uint8Array(decrypted)).toString("utf8");
33
35
  }
34
36
  };
@@ -22,10 +22,10 @@ var RSACrypter = class extends require_protocols_crypter.default {
22
22
  });
23
23
  }
24
24
  encrypt(message, key, encoding = "hex") {
25
- return require_encode.encode(node_crypto.default.publicEncrypt(key, (0, _ocap_util.toBuffer)(message)), encoding);
25
+ return require_encode.encode(node_crypto.default.publicEncrypt(key, Uint8Array.from((0, _ocap_util.toBuffer)(message))), encoding);
26
26
  }
27
27
  decrypt(message, key, encoding = "hex") {
28
- return require_encode.encode(node_crypto.default.privateDecrypt(key, (0, _ocap_util.toBuffer)(message)), encoding);
28
+ return require_encode.encode(node_crypto.default.privateDecrypt(key, Uint8Array.from((0, _ocap_util.toBuffer)(message))), encoding);
29
29
  }
30
30
  };
31
31
  var rsa_default = new RSACrypter();
package/lib/index.d.cts CHANGED
@@ -38,7 +38,8 @@ interface SignerType {
38
38
  sign(data: BytesType, sk: BytesType, encoding?: 'buffer'): Buffer;
39
39
  sign(data: BytesType, sk: BytesType, encoding?: 'Uint8Array'): Uint8Array;
40
40
  sign(data: BytesType, sk: BytesType, encoding?: EncodingType): BytesType;
41
- verify(data: BytesType, pk: BytesType, signature: BytesType, extra?: any): boolean;
41
+ /** extra is JSON string for passkey signer containing {authenticatorData, clientDataJSON} */
42
+ verify(data: BytesType, pk: BytesType, signature: BytesType, extra?: string): boolean;
42
43
  ethHash?(data: string): string;
43
44
  ethSign?(data: string, sk: string): string;
44
45
  ethVerify?(data: string, pk: string, signature: BytesType): boolean;
@@ -22,8 +22,8 @@ var EthereumSigner = class extends require_signer_secp256k1.Secp256k1Signer {
22
22
  const messageBytes = (0, _ocap_util.hexToBytes)((0, _ocap_util.isHexStrict)(data) ? data : (0, _ocap_util.utf8ToHex)(data));
23
23
  const messageBuffer = Buffer.from(messageBytes);
24
24
  const preamble = `\x19Ethereum Signed Message:\n${messageBytes.length}`;
25
- const preambleBuffer = Buffer.from(preamble);
26
- const ethMessage = Buffer.concat([preambleBuffer, messageBuffer]);
25
+ const preambleBuffer = Uint8Array.from(Buffer.from(preamble));
26
+ const ethMessage = Buffer.concat([preambleBuffer, Uint8Array.from(messageBuffer)]);
27
27
  return eth_lib_lib_hash.default.keccak256s(ethMessage);
28
28
  }
29
29
  ethSign(data, privateKey) {
@@ -38,8 +38,8 @@ var PasskeySigner = class extends require_protocols_signer.default {
38
38
  if ((0, _simplewebauthn_server_helpers.decodeClientDataJSON)(parsed.clientDataJSON).challenge !== (0, _ocap_util.toBase64)(challenge)) throw new Error("challenge mismatch for passkey signature");
39
39
  return (0, _simplewebauthn_server_helpers.verifySignature)({
40
40
  signature: _simplewebauthn_server_helpers.isoBase64URL.toBuffer(typeof signature === "string" ? signature : (0, _ocap_util.toBase64)(signature)),
41
- data: _simplewebauthn_server_helpers.isoUint8Array.concat([authDataBuffer, clientDataHash]),
42
- credentialPublicKey: pk
41
+ data: _simplewebauthn_server_helpers.isoUint8Array.concat([Uint8Array.from(authDataBuffer), clientDataHash]),
42
+ credentialPublicKey: (0, _ocap_util.toUint8Array)(pk)
43
43
  });
44
44
  }
45
45
  };
@@ -76,7 +76,7 @@ var Secp256k1Signer = class extends require_protocols_signer.default {
76
76
  msg = (0, _ocap_util.toUint8Array)(message);
77
77
  } catch (_err) {}
78
78
  let pkBuffer = (0, _ocap_util.toBuffer)(pk);
79
- if (this.pkHasFormatPrefix === false && pkBuffer[0] !== 4 && pkBuffer.byteLength === 64) pkBuffer = Buffer.concat([Buffer.from([4]), pkBuffer]);
79
+ if (this.pkHasFormatPrefix === false && pkBuffer[0] !== 4 && pkBuffer.byteLength === 64) pkBuffer = Buffer.concat([Uint8Array.from([4]), Uint8Array.from(pkBuffer)]);
80
80
  return secp256k1.keyFromPublic(pkBuffer).verify((0, _ocap_util.stripHexPrefix)(msg), (0, _ocap_util.stripHexPrefix)((0, _ocap_util.toHex)(signature)));
81
81
  }
82
82
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ocap/mcrypto",
3
- "version": "1.28.8",
3
+ "version": "1.29.0",
4
4
  "type": "module",
5
5
  "description": "Crypto lib that provides signer,crypter,hasher interface",
6
6
  "keywords": [
@@ -48,14 +48,13 @@
48
48
  "esm"
49
49
  ],
50
50
  "devDependencies": {
51
- "@ocap/e2e-test": "1.28.8",
51
+ "@ocap/e2e-test": "1.29.0",
52
52
  "@types/crypto-js": "^4.2.2",
53
53
  "@types/elliptic": "^6.4.18",
54
54
  "@types/node": "^22.7.5",
55
55
  "@types/randombytes": "^2.0.3",
56
56
  "tsdown": "^0.18.4",
57
- "type-fest": "^3.1.0",
58
- "typescript": "^5.6.2"
57
+ "type-fest": "^3.1.0"
59
58
  },
60
59
  "repository": {
61
60
  "type": "git",
@@ -74,7 +73,7 @@
74
73
  "url": "https://github.com/ArcBlock/blockchain/issues"
75
74
  },
76
75
  "dependencies": {
77
- "@ocap/util": "1.28.8",
76
+ "@ocap/util": "1.29.0",
78
77
  "@simplewebauthn/server": "^13.0.0",
79
78
  "bn.js": "5.2.2",
80
79
  "crypto-js": "^4.2.0",