From 2bb1cc92a97b61d92708e3672f8636485330d2c5 Mon Sep 17 00:00:00 2001 From: "Hong Jing (Jingles)" Date: Sat, 24 Aug 2024 07:53:18 +0800 Subject: [PATCH] revert strica --- package-lock.json | 232 ++++++--- packages/mesh-core-cst/package.json | 9 +- .../stricahq/bip32ed25519/bip32-public-key.ts | 65 --- .../stricahq/bip32ed25519/ed25519e/index.ts | 5 - .../src/stricahq/bip32ed25519/index.ts | 29 +- .../src/stricahq/bip32ed25519/private-key.ts | 45 -- .../src/stricahq/bip32ed25519/public-key.ts | 27 -- .../src/stricahq/bip32ed25519/utils.ts | 23 - .../src/stricahq/cbors/buffer-list.ts | 32 -- .../src/stricahq/cbors/cbor-array.ts | 11 - .../src/stricahq/cbors/cbor-map.ts | 11 - .../src/stricahq/cbors/cbor-tag.ts | 20 - .../src/stricahq/cbors/decoder/index.ts | 449 ------------------ .../src/stricahq/cbors/encoder/index.ts | 214 --------- .../src/stricahq/cbors/helpers.ts | 4 - .../mesh-core-cst/src/stricahq/cbors/index.ts | 11 +- .../src/stricahq/cbors/simple-value.ts | 7 - .../mesh-core-cst/src/stricahq/cbors/utils.ts | 32 -- .../src/stricahq/cbors/wrapper.ts | 9 + 19 files changed, 209 insertions(+), 1026 deletions(-) delete mode 100644 packages/mesh-core-cst/src/stricahq/bip32ed25519/bip32-public-key.ts delete mode 100644 packages/mesh-core-cst/src/stricahq/bip32ed25519/ed25519e/index.ts delete mode 100644 packages/mesh-core-cst/src/stricahq/bip32ed25519/private-key.ts delete mode 100644 packages/mesh-core-cst/src/stricahq/bip32ed25519/public-key.ts delete mode 100644 packages/mesh-core-cst/src/stricahq/bip32ed25519/utils.ts delete mode 100644 packages/mesh-core-cst/src/stricahq/cbors/buffer-list.ts delete mode 100644 packages/mesh-core-cst/src/stricahq/cbors/cbor-array.ts delete mode 100644 packages/mesh-core-cst/src/stricahq/cbors/cbor-map.ts delete mode 100644 packages/mesh-core-cst/src/stricahq/cbors/cbor-tag.ts delete mode 100644 packages/mesh-core-cst/src/stricahq/cbors/decoder/index.ts delete mode 100644 packages/mesh-core-cst/src/stricahq/cbors/encoder/index.ts delete mode 100644 packages/mesh-core-cst/src/stricahq/cbors/helpers.ts delete mode 100644 packages/mesh-core-cst/src/stricahq/cbors/simple-value.ts delete mode 100644 packages/mesh-core-cst/src/stricahq/cbors/utils.ts create mode 100644 packages/mesh-core-cst/src/stricahq/cbors/wrapper.ts diff --git a/package-lock.json b/package-lock.json index d078a7fa..c6844de8 100644 --- a/package-lock.json +++ b/package-lock.json @@ -4204,11 +4204,11 @@ "license": "ISC" }, "node_modules/@cardano-sdk/dapp-connector": { - "version": "0.12.32", - "resolved": "https://registry.npmjs.org/@cardano-sdk/dapp-connector/-/dapp-connector-0.12.32.tgz", - "integrity": "sha512-qS4Xr3cjpw/fZtVBgDk4Prd7soWPHXz4EhZXCIs7SaVvO1Mkl3qTrNg5rVtvKrkDdXaKNhjBA1BZc+JAW1HWVA==", + "version": "0.12.33", + "resolved": "https://registry.npmjs.org/@cardano-sdk/dapp-connector/-/dapp-connector-0.12.33.tgz", + "integrity": "sha512-KMwh6LDcBdorA6X+HDtVOoyDcNSjxGebFxjXuSV3nDrRS5ueWCbZz2Txc0acT4ucXhCc3itl/5WdfNP11JJ3+Q==", "dependencies": { - "@cardano-sdk/core": "~0.38.3", + "@cardano-sdk/core": "~0.39.0", "@cardano-sdk/crypto": "~0.1.30", "@cardano-sdk/util": "~0.15.5", "ts-custom-error": "^3.2.0", @@ -4248,9 +4248,9 @@ } }, "node_modules/@cardano-sdk/dapp-connector/node_modules/@cardano-sdk/core": { - "version": "0.38.3", - "resolved": "https://registry.npmjs.org/@cardano-sdk/core/-/core-0.38.3.tgz", - "integrity": "sha512-M9skfTK+GHCJQMHaYNKG339fb3FEQaUUi/Gzk6+uMjkVV4fFzAyEhMnkCvH8zO1hQvpOK4yqrfkxapU7JRkutA==", + "version": "0.39.0", + "resolved": "https://registry.npmjs.org/@cardano-sdk/core/-/core-0.39.0.tgz", + "integrity": "sha512-7nrRrFK8hM1XKzTsRjyGMZY46oK0KcQtIR0e44xqlxHo2sG4iy3abL60DhguDqcJkagmap8TPTT36QVtrgD4iQ==", "dependencies": { "@cardano-ogmios/client": "6.5.0", "@cardano-ogmios/schema": "6.5.0", @@ -4368,13 +4368,13 @@ } }, "node_modules/@cardano-sdk/key-management/node_modules/@cardano-sdk/key-management": { - "version": "0.23.3", - "resolved": "https://registry.npmjs.org/@cardano-sdk/key-management/-/key-management-0.23.3.tgz", - "integrity": "sha512-qqXQWpC8udVWwsQjoZg7UvhjY6BhFTvtjYB4hpUppmfoLEhY0kwN3X3Vk+GpA3iZ0XMcZJYLfKdFhKYbmR/rOw==", + "version": "0.23.4", + "resolved": "https://registry.npmjs.org/@cardano-sdk/key-management/-/key-management-0.23.4.tgz", + "integrity": "sha512-KvfiKoyo8Tq/PTgxfg7nwZnZSa4GB+i0JbLRa8EneMWxEuB726BOW2Ra8gvdsW8QAYxLw/uD9FDeF8iiM3jY2A==", "dependencies": { - "@cardano-sdk/core": "~0.38.3", + "@cardano-sdk/core": "~0.39.0", "@cardano-sdk/crypto": "~0.1.30", - "@cardano-sdk/dapp-connector": "~0.12.32", + "@cardano-sdk/dapp-connector": "~0.12.33", "@cardano-sdk/util": "~0.15.5", "@emurgo/cardano-message-signing-nodejs": "^1.0.1", "bip39": "^3.0.4", @@ -4391,9 +4391,9 @@ } }, "node_modules/@cardano-sdk/key-management/node_modules/@cardano-sdk/key-management/node_modules/@cardano-sdk/core": { - "version": "0.38.3", - "resolved": "https://registry.npmjs.org/@cardano-sdk/core/-/core-0.38.3.tgz", - "integrity": "sha512-M9skfTK+GHCJQMHaYNKG339fb3FEQaUUi/Gzk6+uMjkVV4fFzAyEhMnkCvH8zO1hQvpOK4yqrfkxapU7JRkutA==", + "version": "0.39.0", + "resolved": "https://registry.npmjs.org/@cardano-sdk/core/-/core-0.39.0.tgz", + "integrity": "sha512-7nrRrFK8hM1XKzTsRjyGMZY46oK0KcQtIR0e44xqlxHo2sG4iy3abL60DhguDqcJkagmap8TPTT36QVtrgD4iQ==", "dependencies": { "@cardano-ogmios/client": "6.5.0", "@cardano-ogmios/schema": "6.5.0", @@ -4421,13 +4421,13 @@ } }, "node_modules/@cardano-sdk/key-management/node_modules/@cardano-sdk/util-dev": { - "version": "0.22.5", - "resolved": "https://registry.npmjs.org/@cardano-sdk/util-dev/-/util-dev-0.22.5.tgz", - "integrity": "sha512-9Nw8PxPmIGQ7e50QAjbMcWAFzOdQsTcSOV3kJ+O/ITDcXcB312u6G4YnfdPEvg40Wm21OuFaJpOZG3UKvJeX0A==", + "version": "0.22.6", + "resolved": "https://registry.npmjs.org/@cardano-sdk/util-dev/-/util-dev-0.22.6.tgz", + "integrity": "sha512-qSMDcihdfXRneUKXTUozjgp18kt1y75PNwgy0PpAXaWBKFX/j/zoTI117F3XVKg6IgIiKFy60iuYbil/1Kl0qw==", "dependencies": { - "@cardano-sdk/core": "~0.38.3", + "@cardano-sdk/core": "~0.39.0", "@cardano-sdk/crypto": "~0.1.30", - "@cardano-sdk/key-management": "~0.23.3", + "@cardano-sdk/key-management": "~0.23.4", "@cardano-sdk/util": "~0.15.5", "@types/dockerode": "^3.3.8", "axios": "^1.7.4", @@ -4446,9 +4446,9 @@ } }, "node_modules/@cardano-sdk/key-management/node_modules/@cardano-sdk/util-dev/node_modules/@cardano-sdk/core": { - "version": "0.38.3", - "resolved": "https://registry.npmjs.org/@cardano-sdk/core/-/core-0.38.3.tgz", - "integrity": "sha512-M9skfTK+GHCJQMHaYNKG339fb3FEQaUUi/Gzk6+uMjkVV4fFzAyEhMnkCvH8zO1hQvpOK4yqrfkxapU7JRkutA==", + "version": "0.39.0", + "resolved": "https://registry.npmjs.org/@cardano-sdk/core/-/core-0.39.0.tgz", + "integrity": "sha512-7nrRrFK8hM1XKzTsRjyGMZY46oK0KcQtIR0e44xqlxHo2sG4iy3abL60DhguDqcJkagmap8TPTT36QVtrgD4iQ==", "dependencies": { "@cardano-ogmios/client": "6.5.0", "@cardano-ogmios/schema": "6.5.0", @@ -6665,6 +6665,29 @@ "@sinonjs/commons": "^3.0.0" } }, + "node_modules/@stricahq/bip32ed25519": { + "version": "1.1.0", + "resolved": "https://registry.npmjs.org/@stricahq/bip32ed25519/-/bip32ed25519-1.1.0.tgz", + "integrity": "sha512-G3OEjxaqvTCzEGppmI9arMrKzl44poVXWTVlQgLILI39EtG/mDWQguN+wJ9m+9TOIxPBlz/10TLKrV+WVH91uw==", + "dependencies": { + "blakejs": "^1.1.1", + "bn.js": "^5.2.0", + "buffer": "^6.0.3", + "chai": "^4.3.4", + "elliptic": "6.5.4", + "hash.js": "^1.1.7", + "pbkdf2": "^3.1.2" + } + }, + "node_modules/@stricahq/cbors": { + "version": "1.0.3", + "resolved": "https://registry.npmjs.org/@stricahq/cbors/-/cbors-1.0.3.tgz", + "integrity": "sha512-FlV5DaQtTeSSgkC9S4CiO167+x1t6uSNixQ32QMWIMG7+/LhOtzv4GPf4qJR6z7C31mzx+mRlf86dJt9lfBeVw==", + "dependencies": { + "bignumber.js": "^9.0.2", + "buffer": "^6.0.3" + } + }, "node_modules/@swc/counter": { "version": "0.1.3", "resolved": "https://registry.npmjs.org/@swc/counter/-/counter-0.1.3.tgz", @@ -6798,15 +6821,6 @@ "integrity": "sha512-MFCdX0MNxFBP/xEILO5Td0kv6nI7+Q2iRWZbTL/yzH2/eDVZS5Wd1LHdsmXClvsCyzqaZfHFzZaN6BUeUCfSDA==", "dev": true }, - "node_modules/@types/bn.js": { - "version": "5.1.5", - "resolved": "https://registry.npmjs.org/@types/bn.js/-/bn.js-5.1.5.tgz", - "integrity": "sha512-V46N0zwKRF5Q00AZ6hWtN0T8gGmDUaUzLWQvHFo5yThtVwK/VCenFY3wXVbOvNfajEpsTfQM4IN9k/d6gUVX3A==", - "dev": true, - "dependencies": { - "@types/node": "*" - } - }, "node_modules/@types/debug": { "version": "4.1.12", "resolved": "https://registry.npmjs.org/@types/debug/-/debug-4.1.12.tgz", @@ -8060,6 +8074,14 @@ "safer-buffer": "~2.1.0" } }, + "node_modules/assertion-error": { + "version": "1.1.0", + "resolved": "https://registry.npmjs.org/assertion-error/-/assertion-error-1.1.0.tgz", + "integrity": "sha512-jgsaNduz+ndvGyFt3uSuWqvy4lCnIJiovtouQN5JZHOKCS2QuhEdbcQHFhVksz2N2U9hXJo8odG7ETyWlEeuDw==", + "engines": { + "node": "*" + } + }, "node_modules/ast-types-flow": { "version": "0.0.8", "resolved": "https://registry.npmjs.org/ast-types-flow/-/ast-types-flow-0.0.8.tgz", @@ -8144,9 +8166,9 @@ } }, "node_modules/axios": { - "version": "1.7.4", - "resolved": "https://registry.npmjs.org/axios/-/axios-1.7.4.tgz", - "integrity": "sha512-DukmaFRnY6AzAALSH4J2M3k6PkaC+MfaAGdEERRWcC9q3/TWQwLpHR8ZRLKTdQ3aBDL64EdluRDjJqKw+BPZEw==", + "version": "1.7.5", + "resolved": "https://registry.npmjs.org/axios/-/axios-1.7.5.tgz", + "integrity": "sha512-fZu86yCo+svH3uqJ/yTdQ0QHpQu5oL+/QE+QPSv6BZSkDAoky9vytxp7u5qk83OJFS3kEBcesWni9WTZAv3tSw==", "dependencies": { "follow-redirects": "^1.15.6", "form-data": "^4.0.0", @@ -8376,6 +8398,29 @@ "readable-stream": "^3.4.0" } }, + "node_modules/bl/node_modules/buffer": { + "version": "5.7.1", + "resolved": "https://registry.npmjs.org/buffer/-/buffer-5.7.1.tgz", + "integrity": "sha512-EHcyIPBQ4BSGlvjB16k5KgAJ27CIsHY/2JBmCRReo48y9rQ3MaUzWX3KVlBa4U7MyX02HdVj0K7C3WaB3ju7FQ==", + "funding": [ + { + "type": "github", + "url": "https://github.com/sponsors/feross" + }, + { + "type": "patreon", + "url": "https://www.patreon.com/feross" + }, + { + "type": "consulting", + "url": "https://feross.org/support" + } + ], + "dependencies": { + "base64-js": "^1.3.1", + "ieee754": "^1.1.13" + } + }, "node_modules/bl/node_modules/readable-stream": { "version": "3.6.2", "resolved": "https://registry.npmjs.org/readable-stream/-/readable-stream-3.6.2.tgz", @@ -8503,9 +8548,9 @@ } }, "node_modules/buffer": { - "version": "5.7.1", - "resolved": "https://registry.npmjs.org/buffer/-/buffer-5.7.1.tgz", - "integrity": "sha512-EHcyIPBQ4BSGlvjB16k5KgAJ27CIsHY/2JBmCRReo48y9rQ3MaUzWX3KVlBa4U7MyX02HdVj0K7C3WaB3ju7FQ==", + "version": "6.0.3", + "resolved": "https://registry.npmjs.org/buffer/-/buffer-6.0.3.tgz", + "integrity": "sha512-FTiCpNxtwiZZHEZbcbTIcZjERVICn9yq/pDFkTl95/AxzD1naBctN7YO68riM/gLSDY7sdrMby8hofADYuuqOA==", "funding": [ { "type": "github", @@ -8522,7 +8567,7 @@ ], "dependencies": { "base64-js": "^1.3.1", - "ieee754": "^1.1.13" + "ieee754": "^1.2.1" } }, "node_modules/buffer-from": { @@ -8680,6 +8725,31 @@ "nan": "^2.4.0" } }, + "node_modules/chai": { + "version": "4.5.0", + "resolved": "https://registry.npmjs.org/chai/-/chai-4.5.0.tgz", + "integrity": "sha512-RITGBfijLkBddZvnn8jdqoTypxvqbOLYQkGGxXzeFjVHvudaPw0HNFD9x928/eUwYWd2dPCugVqspGALTZZQKw==", + "dependencies": { + "assertion-error": "^1.1.0", + "check-error": "^1.0.3", + "deep-eql": "^4.1.3", + "get-func-name": "^2.0.2", + "loupe": "^2.3.6", + "pathval": "^1.1.1", + "type-detect": "^4.1.0" + }, + "engines": { + "node": ">=4" + } + }, + "node_modules/chai/node_modules/type-detect": { + "version": "4.1.0", + "resolved": "https://registry.npmjs.org/type-detect/-/type-detect-4.1.0.tgz", + "integrity": "sha512-Acylog8/luQ8L7il+geoSxhEkazvkslg7PSNKOX59mbB9cOveP5aq9h74Y7YU8yDpJwetzQQrfIwtf4Wp4LKcw==", + "engines": { + "node": ">=4" + } + }, "node_modules/chalk": { "version": "4.1.2", "resolved": "https://registry.npmjs.org/chalk/-/chalk-4.1.2.tgz", @@ -8740,6 +8810,17 @@ "url": "https://github.com/sponsors/wooorm" } }, + "node_modules/check-error": { + "version": "1.0.3", + "resolved": "https://registry.npmjs.org/check-error/-/check-error-1.0.3.tgz", + "integrity": "sha512-iKEoDYaRmd1mxM90a2OEfWhjsjPpYPuQ+lMYsoxB126+t8fw7ySEO48nmDg5COTjxDI65/Y2OWpeEHk3ZOe8zg==", + "dependencies": { + "get-func-name": "^2.0.2" + }, + "engines": { + "node": "*" + } + }, "node_modules/chokidar": { "version": "3.6.0", "resolved": "https://registry.npmjs.org/chokidar/-/chokidar-3.6.0.tgz", @@ -9218,6 +9299,17 @@ } } }, + "node_modules/deep-eql": { + "version": "4.1.4", + "resolved": "https://registry.npmjs.org/deep-eql/-/deep-eql-4.1.4.tgz", + "integrity": "sha512-SUwdGfqdKOwxCPeVYjwSyRpJ7Z+fhpwIAtmCUdZIWZ/YP5R9WAsyuSgpLVDi9bjWoN2LXHNss/dk3urXtdQxGg==", + "dependencies": { + "type-detect": "^4.0.0" + }, + "engines": { + "node": ">=6" + } + }, "node_modules/deep-equal": { "version": "2.2.3", "resolved": "https://registry.npmjs.org/deep-equal/-/deep-equal-2.2.3.tgz", @@ -11366,6 +11458,14 @@ "node": "6.* || 8.* || >= 10.*" } }, + "node_modules/get-func-name": { + "version": "2.0.2", + "resolved": "https://registry.npmjs.org/get-func-name/-/get-func-name-2.0.2.tgz", + "integrity": "sha512-8vXOvuE167CtIc3OyItco7N/dpRtBbYOsPsXCz7X/PMnlGjYjSGuZJgM1Y7mmew7BKf9BqvLX2tnOVy1BBUsxQ==", + "engines": { + "node": "*" + } + }, "node_modules/get-intrinsic": { "version": "1.2.4", "resolved": "https://registry.npmjs.org/get-intrinsic/-/get-intrinsic-1.2.4.tgz", @@ -13524,6 +13624,14 @@ "loose-envify": "cli.js" } }, + "node_modules/loupe": { + "version": "2.3.7", + "resolved": "https://registry.npmjs.org/loupe/-/loupe-2.3.7.tgz", + "integrity": "sha512-zSMINGVYkdpYSOBmLi0D1Uo7JU9nVdQKrHxC8eYlV+9YKK9WePqAlL7lSlorG/U2Fw1w0hTBmaa/jrQ3UbPHtA==", + "dependencies": { + "get-func-name": "^2.0.1" + } + }, "node_modules/lru-cache": { "version": "5.1.1", "resolved": "https://registry.npmjs.org/lru-cache/-/lru-cache-5.1.1.tgz", @@ -14373,9 +14481,9 @@ ] }, "node_modules/micromatch": { - "version": "4.0.7", - "resolved": "https://registry.npmjs.org/micromatch/-/micromatch-4.0.7.tgz", - "integrity": "sha512-LPP/3KorzCwBxfeUuZmaR6bG2kdeHSbe0P2tY3FLRU4vYrjYz5hI4QZwV0njUx3jeuKe67YukQ1LSPZBKDqO/Q==", + "version": "4.0.8", + "resolved": "https://registry.npmjs.org/micromatch/-/micromatch-4.0.8.tgz", + "integrity": "sha512-PXwfBhYu0hBCPw8Dn0E+WDYb7af3dSLVWKi3HGv84IdF4TyFoC0ysxFd0Goxw7nSv4T/PzEJQxsYsEiFCKo2BA==", "dependencies": { "braces": "^3.0.3", "picomatch": "^2.3.1" @@ -17365,6 +17473,14 @@ "node": ">=8" } }, + "node_modules/pathval": { + "version": "1.1.1", + "resolved": "https://registry.npmjs.org/pathval/-/pathval-1.1.1.tgz", + "integrity": "sha512-Dp6zGqpTdETdR63lehJYPeIOqpiNBNtc7BpWSLrOje7UaIsE5aY92r/AunQA7rsXvet3lrJ3JnZX29UPTKXyKQ==", + "engines": { + "node": "*" + } + }, "node_modules/pbkdf2": { "version": "3.1.2", "resolved": "https://registry.npmjs.org/pbkdf2/-/pbkdf2-3.1.2.tgz", @@ -19679,20 +19795,20 @@ "integrity": "sha512-Y/arvbn+rrz3JCKl9C4kVNfTfSm2/mEp5FSz5EsZSANGPSlQrpRI5M4PKF+mJnE52jOO90PnPSc3Ur3bTQw0gA==" }, "node_modules/ts-jest": { - "version": "29.2.4", - "resolved": "https://registry.npmjs.org/ts-jest/-/ts-jest-29.2.4.tgz", - "integrity": "sha512-3d6tgDyhCI29HlpwIq87sNuI+3Q6GLTTCeYRHCs7vDz+/3GCMwEtV9jezLyl4ZtnBgx00I7hm8PCP8cTksMGrw==", + "version": "29.2.5", + "resolved": "https://registry.npmjs.org/ts-jest/-/ts-jest-29.2.5.tgz", + "integrity": "sha512-KD8zB2aAZrcKIdGk4OwpJggeLcH1FgrICqDSROWqlnJXGCXK4Mn6FcdK2B6670Xr73lHMG1kHw8R87A0ecZ+vA==", "dev": true, "dependencies": { - "bs-logger": "0.x", + "bs-logger": "^0.2.6", "ejs": "^3.1.10", - "fast-json-stable-stringify": "2.x", + "fast-json-stable-stringify": "^2.1.0", "jest-util": "^29.0.0", "json5": "^2.2.3", - "lodash.memoize": "4.x", - "make-error": "1.x", - "semver": "^7.5.3", - "yargs-parser": "^21.0.1" + "lodash.memoize": "^4.1.2", + "make-error": "^1.3.6", + "semver": "^7.6.3", + "yargs-parser": "^21.1.1" }, "bin": { "ts-jest": "cli.js" @@ -19823,9 +19939,9 @@ } }, "node_modules/tslib": { - "version": "2.6.3", - "resolved": "https://registry.npmjs.org/tslib/-/tslib-2.6.3.tgz", - "integrity": "sha512-xNvxJEOUiWPGhUuUdQgAJPKOOJfGnIyKySOc09XkKsgdUV/3E2zvwZYdejjmRgPCgcym1juLH3226yA7sEFJKQ==" + "version": "2.7.0", + "resolved": "https://registry.npmjs.org/tslib/-/tslib-2.7.0.tgz", + "integrity": "sha512-gLXCKdN1/j47AiHiOkJN69hJmcbGTHI0ImLmbYLHykhgeN0jVGola9yVjFgzCUklsZQMW55o+dW7IXv3RCXDzA==" }, "node_modules/tsup": { "version": "8.2.4", @@ -20115,7 +20231,6 @@ "version": "4.0.8", "resolved": "https://registry.npmjs.org/type-detect/-/type-detect-4.0.8.tgz", "integrity": "sha512-0fr/mIH1dlO+x7TlcMy+bIDqKPsw/70tVyeHW787goQjhmqaZe10uwLujubK9q9Lg6Fiho1KUKDYz0Z7k7g5/g==", - "dev": true, "engines": { "node": ">=4" } @@ -21117,15 +21232,12 @@ "@harmoniclabs/plutus-data": "1.2.4", "@harmoniclabs/uplc": "1.2.4", "@meshsdk/common": "*", - "blakejs": "^1.1.1", - "bn.js": "^5.2.0", - "elliptic": "6.5.4", - "hash.js": "^1.1.7", + "@stricahq/bip32ed25519": "^1.1.0", + "@stricahq/cbors": "^1.0.0", "pbkdf2": "^3.1.2" }, "devDependencies": { "@meshsdk/configs": "*", - "@types/bn.js": "^5.1.5", "@types/pbkdf2": "^3.1.2", "eslint": "^8.57.0", "ts-jest": "^29.1.4", diff --git a/packages/mesh-core-cst/package.json b/packages/mesh-core-cst/package.json index db353f9f..dac9c5c3 100644 --- a/packages/mesh-core-cst/package.json +++ b/packages/mesh-core-cst/package.json @@ -27,7 +27,6 @@ }, "devDependencies": { "@meshsdk/configs": "*", - "@types/bn.js": "^5.1.5", "@types/pbkdf2": "^3.1.2", "eslint": "^8.57.0", "ts-jest": "^29.1.4", @@ -43,10 +42,8 @@ "@harmoniclabs/plutus-data": "1.2.4", "@harmoniclabs/uplc": "1.2.4", "@meshsdk/common": "*", - "blakejs": "^1.1.1", - "bn.js": "^5.2.0", - "elliptic": "6.5.4", - "hash.js": "^1.1.7", + "@stricahq/bip32ed25519": "^1.1.0", + "@stricahq/cbors": "^1.0.0", "pbkdf2": "^3.1.2" }, "prettier": "@meshsdk/configs/prettier", @@ -61,4 +58,4 @@ "blockchain", "sdk" ] -} +} \ No newline at end of file diff --git a/packages/mesh-core-cst/src/stricahq/bip32ed25519/bip32-public-key.ts b/packages/mesh-core-cst/src/stricahq/bip32ed25519/bip32-public-key.ts deleted file mode 100644 index 7c3dab9d..00000000 --- a/packages/mesh-core-cst/src/stricahq/bip32ed25519/bip32-public-key.ts +++ /dev/null @@ -1,65 +0,0 @@ -/* eslint-disable no-bitwise */ -import { Buffer } from "buffer"; -import BN from "bn.js"; - -import { EDDSA } from "./ed25519e/eddsa"; -import { PublicKey } from "./public-key"; -import { HARDENED_OFFSET, hmac512 } from "./utils"; - -const eddsa = new EDDSA(); - -class Bip32PublicKey { - protected xpub: Buffer; - - constructor(xpub: Buffer) { - this.xpub = xpub; - } - - derive(index: number) { - const pk = this.xpub.subarray(0, 32); - const cc = this.xpub.subarray(32, 64); - - const data = Buffer.allocUnsafe(1 + 32 + 4); - data.writeUInt32LE(index, 1 + 32); - - let z; - let i; - if (index < HARDENED_OFFSET) { - pk.copy(data, 1); - data[0] = 0x02; - z = hmac512(cc, data); - data[0] = 0x03; - i = hmac512(cc, data); - } else { - throw new Error("can not derive hardened public key"); - } - - const chainCode = i.subarray(32, 64); - const zl = z.subarray(0, 32); - - const left = new BN(zl.subarray(0, 28), 16, "le").mul(new BN(8)); - - const p = eddsa.g.mul(left); - const pp = eddsa.decodePoint(pk.toString("hex")); - const point = pp.add(p); - - return new Bip32PublicKey( - Buffer.concat([Buffer.from(eddsa.encodePoint(point)), chainCode]), - ); - } - - toPublicKey(): PublicKey { - const key = eddsa.keyFromPublic(this.xpub.slice(0, 32)); - return new PublicKey(Buffer.from(key.pubBytes())); - } - - toBytes(): Buffer { - return this.xpub; - } - - static fromBytes(xpub: Buffer): Bip32PublicKey { - return new Bip32PublicKey(xpub); - } -} - -export { Bip32PublicKey }; diff --git a/packages/mesh-core-cst/src/stricahq/bip32ed25519/ed25519e/index.ts b/packages/mesh-core-cst/src/stricahq/bip32ed25519/ed25519e/index.ts deleted file mode 100644 index 220b0719..00000000 --- a/packages/mesh-core-cst/src/stricahq/bip32ed25519/ed25519e/index.ts +++ /dev/null @@ -1,5 +0,0 @@ -import { EDDSA } from "./eddsa"; -import { KeyPair } from "./key"; -import { Signature } from "./signature"; - -export { EDDSA, KeyPair, Signature }; diff --git a/packages/mesh-core-cst/src/stricahq/bip32ed25519/index.ts b/packages/mesh-core-cst/src/stricahq/bip32ed25519/index.ts index 317e35c8..6734869d 100644 --- a/packages/mesh-core-cst/src/stricahq/bip32ed25519/index.ts +++ b/packages/mesh-core-cst/src/stricahq/bip32ed25519/index.ts @@ -1,12 +1,23 @@ -import { PrivateKey } from "./private-key"; -import { PublicKey } from "./public-key"; -import { Bip32PrivateKey } from "./bip32-private-key"; -import { Bip32PublicKey } from "./bip32-public-key"; - +import { + bip32ed25519, + StricaBip32PrivateKey, + StricaBip32PrivateKey as StricaBip32PrivateKeyType, + StricaBip32PublicKey, + StricaBip32PublicKey as StricaBip32PublicKeyType, + StricaPrivateKey, + StricaPrivateKey as StricaPrivateKeyType, + StricaPublicKey, + StricaPublicKey as StricaPublicKeyType, +} from "./wrapper"; +export default bip32ed25519; export { - PrivateKey as StricaPrivateKey, - PublicKey as StricaPublicKey, - Bip32PrivateKey as StricaBip32PrivateKey, - Bip32PublicKey as StricaBip32PublicKey, + StricaPrivateKey, + StricaPublicKey, + StricaBip32PrivateKey, + StricaBip32PublicKey, + StricaPrivateKeyType, + StricaPublicKeyType, + StricaBip32PrivateKeyType, + StricaBip32PublicKeyType, }; diff --git a/packages/mesh-core-cst/src/stricahq/bip32ed25519/private-key.ts b/packages/mesh-core-cst/src/stricahq/bip32ed25519/private-key.ts deleted file mode 100644 index 45dd2c79..00000000 --- a/packages/mesh-core-cst/src/stricahq/bip32ed25519/private-key.ts +++ /dev/null @@ -1,45 +0,0 @@ -import { Buffer } from "buffer"; - -import { EDDSA } from "./ed25519e/eddsa"; -import { PublicKey } from "./public-key"; -import { sha512 } from "./utils"; - -const eddsa = new EDDSA(); - -class PrivateKey { - private privKey: Buffer; - - constructor(privKey: Buffer) { - this.privKey = privKey; - } - - static fromSecretKey(secretKey: Buffer): PrivateKey { - let extendedSecret = sha512(secretKey); - extendedSecret[0]! &= 0b1111_1000; - extendedSecret[31]! &= 0b0011_1111; - extendedSecret[31]! |= 0b0100_0000; - return new PrivateKey(extendedSecret); - } - - toBytes(): Buffer { - return this.privKey; - } - - toPublicKey(): PublicKey { - const keyPair = eddsa.keyFromSecret(this.privKey); - return new PublicKey(Buffer.from(keyPair.pubBytes())); - } - - sign(data: Buffer): Buffer { - const keyPair = eddsa.keyFromSecret(this.privKey); - const signature = keyPair.sign(data.toString("hex")); - return Buffer.from(signature.toBytes()); - } - - verify(signature: Buffer, message: Buffer) { - const keyPair = eddsa.keyFromSecret(this.privKey); - return keyPair.verify(message.toString("hex"), signature.toString("hex")); - } -} - -export { PrivateKey }; diff --git a/packages/mesh-core-cst/src/stricahq/bip32ed25519/public-key.ts b/packages/mesh-core-cst/src/stricahq/bip32ed25519/public-key.ts deleted file mode 100644 index 04c94575..00000000 --- a/packages/mesh-core-cst/src/stricahq/bip32ed25519/public-key.ts +++ /dev/null @@ -1,27 +0,0 @@ -import { EDDSA } from "./ed25519e/eddsa"; -import { hash28 } from "./utils"; - -const eddsa = new EDDSA(); - -class PublicKey { - pubKey: Buffer; - - constructor(pubKey: Buffer) { - this.pubKey = pubKey; - } - - toBytes(): Buffer { - return this.pubKey; - } - - hash(): Buffer { - return hash28(this.pubKey); - } - - verify(signature: Buffer, data: Buffer) { - const keyPair = eddsa.keyFromPublic(this.pubKey.toString("hex")); - return keyPair.verify(data.toString("hex"), signature.toString("hex")); - } -} - -export { PublicKey }; diff --git a/packages/mesh-core-cst/src/stricahq/bip32ed25519/utils.ts b/packages/mesh-core-cst/src/stricahq/bip32ed25519/utils.ts deleted file mode 100644 index 4be23f84..00000000 --- a/packages/mesh-core-cst/src/stricahq/bip32ed25519/utils.ts +++ /dev/null @@ -1,23 +0,0 @@ -import { Buffer } from "buffer"; -import { blake2b } from "blakejs"; -import * as hashJs from "hash.js"; - -export const hash28 = function (data: Buffer): Buffer { - const hash = blake2b(data, undefined, 28); - return Buffer.from(hash); -}; - -export const hmac512 = function (key: Buffer, data: Buffer): Buffer { - const digest = hashJs - .hmac(hashJs.sha512 as any, key) - .update(data) - .digest(); - return Buffer.from(digest); -}; - -export const sha512 = function (data: Buffer): Buffer { - const digest = hashJs.sha512().update(data).digest(); - return Buffer.from(digest); -}; - -export const HARDENED_OFFSET = 0x80000000; \ No newline at end of file diff --git a/packages/mesh-core-cst/src/stricahq/cbors/buffer-list.ts b/packages/mesh-core-cst/src/stricahq/cbors/buffer-list.ts deleted file mode 100644 index 29d9be4c..00000000 --- a/packages/mesh-core-cst/src/stricahq/cbors/buffer-list.ts +++ /dev/null @@ -1,32 +0,0 @@ -import { Buffer } from 'buffer'; - -export default class BufferList { - private buf: Buffer = Buffer.alloc(0); - - get length(): number { - return this.buf.length; - } - - read(n: number): Buffer { - if (n === 0) { - return Buffer.alloc(0); - } - if (n < 0) { - throw new Error('invalid length'); - } - - const chunk = this.buf.slice(0, n); - this.buf = this.buf.slice(n); - - if (chunk.length < n) { - throw new Error('Not enough buffer'); - } - - return chunk; - } - - push(chunk: Buffer): void { - if (!chunk.length) return; - this.buf = Buffer.concat([this.buf, chunk]); - } -} \ No newline at end of file diff --git a/packages/mesh-core-cst/src/stricahq/cbors/cbor-array.ts b/packages/mesh-core-cst/src/stricahq/cbors/cbor-array.ts deleted file mode 100644 index 0f73efaf..00000000 --- a/packages/mesh-core-cst/src/stricahq/cbors/cbor-array.ts +++ /dev/null @@ -1,11 +0,0 @@ -export default class CborArray extends Array { - private byteSpan: [number, number] = [0, 0]; - - setByteSpan(byteSpan: [number, number]) { - this.byteSpan = byteSpan; - } - - getByteSpan(): [number, number] { - return this.byteSpan; - } -} diff --git a/packages/mesh-core-cst/src/stricahq/cbors/cbor-map.ts b/packages/mesh-core-cst/src/stricahq/cbors/cbor-map.ts deleted file mode 100644 index 5c213cd5..00000000 --- a/packages/mesh-core-cst/src/stricahq/cbors/cbor-map.ts +++ /dev/null @@ -1,11 +0,0 @@ -export default class CborMap extends Map { - private byteSpan: [number, number] = [0, 0]; - - setByteSpan(byteSpan: [number, number]) { - this.byteSpan = byteSpan; - } - - getByteSpan(): [number, number] { - return this.byteSpan; - } -} diff --git a/packages/mesh-core-cst/src/stricahq/cbors/cbor-tag.ts b/packages/mesh-core-cst/src/stricahq/cbors/cbor-tag.ts deleted file mode 100644 index 7f11228b..00000000 --- a/packages/mesh-core-cst/src/stricahq/cbors/cbor-tag.ts +++ /dev/null @@ -1,20 +0,0 @@ -export default class CborTag { - value: any; - - tag: number; - - constructor(value: any, tag: number) { - this.value = value; - this.tag = tag; - } - - private byteSpan: [number, number] = [0, 0]; - - setByteSpan(byteSpan: [number, number]) { - this.byteSpan = byteSpan; - } - - getByteSpan(): [number, number] { - return this.byteSpan; - } - } \ No newline at end of file diff --git a/packages/mesh-core-cst/src/stricahq/cbors/decoder/index.ts b/packages/mesh-core-cst/src/stricahq/cbors/decoder/index.ts deleted file mode 100644 index 852bf210..00000000 --- a/packages/mesh-core-cst/src/stricahq/cbors/decoder/index.ts +++ /dev/null @@ -1,449 +0,0 @@ -/* eslint-disable no-underscore-dangle */ -/* eslint-disable no-bitwise */ - -import { Buffer } from "buffer"; -import * as stream from "stream"; -import BigNumber from "bignumber.js"; - -import BufferList from "../buffer-list"; -import CborArray from "../cbor-array"; -import CborMap from "../cbor-map"; -import CborTag from "../cbor-tag"; -import SimpleValue from "../simple-value"; -import { - addSpanBytesToObject, - getBigNum, - POW_2_24, - utf8Decoder, -} from "../utils"; - -const readFloat16 = (value: number): number => { - const sign = value & 0x8000; - let exponent = value & 0x7c00; - const fraction = value & 0x03ff; - - if (exponent === 0x7c00) exponent = 0xff << 10; - else if (exponent !== 0) exponent += (127 - 15) << 10; - else if (fraction !== 0) return (sign ? -1 : 1) * fraction * POW_2_24; - - const buf = Buffer.alloc(4); - buf.writeUInt32BE((sign << 16) | (exponent << 13) | (fraction << 13)); - return buf.readFloatBE(0); -}; - -const isBreakPoint = (value: number): boolean => { - if (value !== 0xff) return false; - return true; -}; - -class StricaDecoder extends stream.Transform { - private bl: any; - - private needed: number | null = null; - - private fresh: boolean = true; - - private _parser = this.parse(); - - private offset: number = 0; - - private usedBytes: Array = []; - - constructor() { - super({ - writableObjectMode: false, - readableObjectMode: true, - }); - this.bl = new BufferList(); - this.restart(); - } - - static decode(inputBytes: Buffer): { bytes: Buffer; value: any } { - const decoder = new StricaDecoder(); - const bs = new BufferList(); - bs.push(inputBytes); - const parser = decoder.parse(); - let state = parser.next(); - - while (!state.done) { - const b = bs.read(state.value); - if (b == null || b.length !== state.value) { - throw new Error("Insufficient data"); - } - state = parser.next(b); - } - - if (bs.length > 0) { - throw new Error("Remaining Bytes"); - } - return { - bytes: inputBytes, - value: state.value, - }; - } - - private readUInt64( - f: number, - g: number, - startByte: number, - ): number | BigNumber { - const bigNum = getBigNum(f, g); - if (BigNumber.isBigNumber(bigNum)) { - return addSpanBytesToObject(bigNum, [startByte, this.offset]); - } - return bigNum; - } - - private updateTracker(bytes: Buffer) { - this.usedBytes.push(bytes); - this.offset += bytes.length; - } - - private *readIndefiniteStringLength( - majorType: number, - startByte: number, - ): Generator { - let bytes = yield 1; - let length; - const number = bytes.readUInt8(0); - - if (number === 0xff) { - length = -1; - } else { - const ai = number & 0x1f; - // read length - const lengthReader = this.readLength(ai, startByte); - let lengthStatus = lengthReader.next(); - while (!lengthStatus.done) { - bytes = yield lengthStatus.value; - lengthStatus = lengthReader.next(bytes); - } - length = lengthStatus.value; - // - if (length < 0 || number >> 5 !== majorType) { - throw new Error("Invalid indefinite length encoding"); - } - } - return length; - } - - private *readLength( - additionalInformation: number, - startByte: number, - ): Generator { - if (additionalInformation < 24) { - return additionalInformation; - } - if (additionalInformation === 24) { - const bytes = yield 1; - return bytes.readUInt8(0); - } - if (additionalInformation === 25) { - const bytes = yield 2; - return bytes.readUInt16BE(0); - } - if (additionalInformation === 26) { - const bytes = yield 4; - return bytes.readUInt32BE(0); - } - if (additionalInformation === 27) { - const fBytes = yield 4; - const f = fBytes.readUInt32BE(0); - const gBytes = yield 4; - const g = gBytes.readUInt32BE(0); - - return this.readUInt64(f, g, startByte); - } - if (additionalInformation === 31) { - return -1; - } - throw new Error("Invalid length encoding"); - } - - _transform(fresh: any, encoding: any, cb: any) { - this.bl.push(fresh); - - while (this.bl.length >= (this.needed as number)) { - let ret = null; - let chunk; - - if (this.needed === null) { - chunk = undefined; - } else { - chunk = this.bl.read(this.needed); - } - - try { - ret = this._parser.next(chunk); - } catch (e) { - return cb(e); - } - - if (this.needed) { - this.fresh = false; - } - - if (ret.done) { - this.push({ - bytes: this.usedBytes, - value: ret.value, - }); - this.restart(); - } else { - this.needed = ret.value || Infinity; - } - } - - return cb(); - } - - private *parse(suppliedBytes?: Buffer): Generator { - let startByte = this.offset; - let bytes; - if (suppliedBytes) { - bytes = suppliedBytes; - startByte -= suppliedBytes.length; - } else { - bytes = yield 1; - this.updateTracker(bytes); - } - - const value = bytes.readUInt8(0); - const majorType = value >> 5; - const additionalInformation = value & 0x1f; - let length; - if (majorType === 7) { - if (additionalInformation === 25) { - bytes = yield 2; - this.updateTracker(bytes); - const number = bytes.readUInt16BE(0); - return readFloat16(number); - } - if (additionalInformation === 26) { - bytes = yield 4; - this.updateTracker(bytes); - return bytes.readFloatBE(0); - } - if (additionalInformation === 27) { - bytes = yield 8; - this.updateTracker(bytes); - return bytes.readDoubleBE(0); - } - } - - // read length - const lengthReader = this.readLength(additionalInformation, startByte); - let lengthStatus = lengthReader.next(); - while (!lengthStatus.done) { - bytes = yield lengthStatus.value; - this.updateTracker(bytes); - lengthStatus = lengthReader.next(bytes); - } - length = lengthStatus.value; - // - - if (length < 0 && (majorType < 2 || majorType > 6)) - throw new Error("Invalid length"); - - switch (majorType) { - case 0: - return length; - case 1: { - if (length === Number.MAX_SAFE_INTEGER) { - const bigNum = new BigNumber(-1).minus( - new BigNumber(Number.MAX_SAFE_INTEGER.toString(16), 16), - ); - return addSpanBytesToObject(bigNum, [startByte, this.offset]); - } - if (BigNumber.isBigNumber(length)) { - const bigNum = new BigNumber(-1).minus(length); - return addSpanBytesToObject(bigNum, [startByte, this.offset]); - } - return -1 - length; - } - case 2: { - if (length < 0) { - const chunks = []; - { - // read indefinite length - const inDefLengthReader = this.readIndefiniteStringLength( - majorType, - startByte, - ); - let inDefLengthStatus = inDefLengthReader.next(); - while (!inDefLengthStatus.done) { - bytes = yield inDefLengthStatus.value; - this.updateTracker(bytes); - inDefLengthStatus = inDefLengthReader.next(bytes); - } - length = inDefLengthStatus.value; - // - } - while (length >= 0) { - bytes = yield length as number; - this.updateTracker(bytes); - chunks.push(bytes); - { - // read indefinite length - const inDefLengthReader = this.readIndefiniteStringLength( - majorType, - startByte, - ); - let inDefLengthStatus = inDefLengthReader.next(); - while (!inDefLengthStatus.done) { - bytes = yield inDefLengthStatus.value; - this.updateTracker(bytes); - inDefLengthStatus = inDefLengthReader.next(bytes); - } - length = inDefLengthStatus.value; - // - } - } - const buf = Buffer.concat(chunks); - return addSpanBytesToObject(buf, [startByte, this.offset]); - } - bytes = yield length as number; - this.updateTracker(bytes); - return addSpanBytesToObject(bytes, [startByte, this.offset]); - } - case 3: { - const stringBuf: Array = []; - if (length < 0) { - { - // read indefinite length - const inDefLengthReader = this.readIndefiniteStringLength( - majorType, - startByte, - ); - let inDefLengthStatus = inDefLengthReader.next(); - while (!inDefLengthStatus.done) { - bytes = yield inDefLengthStatus.value; - this.updateTracker(bytes); - inDefLengthStatus = inDefLengthReader.next(bytes); - } - length = inDefLengthStatus.value; - // - } - - while (length >= 0) { - bytes = yield length as number; - this.updateTracker(bytes); - stringBuf.push(bytes); - // - - { - // read indefinite length - const inDefLengthReader = this.readIndefiniteStringLength( - majorType, - startByte, - ); - let inDefLengthStatus = inDefLengthReader.next(); - while (!inDefLengthStatus.done) { - bytes = yield inDefLengthStatus.value; - this.updateTracker(bytes); - inDefLengthStatus = inDefLengthReader.next(bytes); - } - length = inDefLengthStatus.value; - // - } - } - - const string = utf8Decoder(Buffer.concat(stringBuf)); - return string; - } - bytes = yield length as number; - this.updateTracker(bytes); - const string = utf8Decoder(bytes); - return string; - } - case 4: { - if (length < 0) { - const ary = new CborArray(); - bytes = yield 1; - this.updateTracker(bytes); - let bp = bytes.readUInt8(0); - while (!isBreakPoint(bp)) { - ary.push(yield* this.parse(bytes)); - - bytes = yield 1; - this.updateTracker(bytes); - bp = bytes.readUInt8(0); - } - ary.setByteSpan([startByte, this.offset]); - return ary; - } - const ary = new CborArray(); - for (let i = 0; i < length; i += 1) { - ary.push(yield* this.parse()); - } - ary.setByteSpan([startByte, this.offset]); - return ary; - } - case 5: { - if (length < 0) { - const obj = new CborMap(); - bytes = yield 1; - this.updateTracker(bytes); - let bp = bytes.readUInt8(0); - while (!isBreakPoint(bp)) { - const key = yield* this.parse(bytes); - const val = yield* this.parse(); - obj.set(key, val); - - bytes = yield 1; - this.updateTracker(bytes); - bp = bytes.readUInt8(0); - } - obj.setByteSpan([startByte, this.offset]); - return obj; - } - const obj = new CborMap(); - - for (let i = 0; i < length; i += 1) { - const key = yield* this.parse(); - const val = yield* this.parse(); - obj.set(key, val); - } - - obj.setByteSpan([startByte, this.offset]); - return obj; - } - case 6: { - const tag = new CborTag(yield* this.parse(), length as number); - tag.setByteSpan([startByte, this.offset]); - return tag; - } - case 7: { - switch (length) { - case 20: - return false; - case 21: - return true; - case 22: - return null; - case 23: - return undefined; - default: - return new SimpleValue(length as number); - } - } - default: { - throw new Error("Invalid CBOR encoding"); - } - } - } - - private restart() { - this.needed = null; - this._parser = this.parse(); - this.fresh = true; - this.offset = 0; - this.usedBytes = []; - } - - _flush(cb: any) { - cb(this.fresh ? null : new Error("unexpected end of input")); - } -} - -export { StricaDecoder }; diff --git a/packages/mesh-core-cst/src/stricahq/cbors/encoder/index.ts b/packages/mesh-core-cst/src/stricahq/cbors/encoder/index.ts deleted file mode 100644 index b83fec46..00000000 --- a/packages/mesh-core-cst/src/stricahq/cbors/encoder/index.ts +++ /dev/null @@ -1,214 +0,0 @@ -/* eslint-disable no-bitwise */ -import { Buffer } from "buffer"; -import BigNumber from "bignumber.js"; - -import CborTag from "../cbor-tag"; -import { IndefiniteArray, IndefiniteMap } from "../helpers"; -import { - MAX_BIG_NUM_INT, - MAX_BIG_NUM_INT32, - MAX_BIG_NUM_INT64, - POW_2_32, - POW_2_53, - SHIFT32, -} from "../utils"; - -const NAN_BUF = Buffer.from("f97e00", "hex"); -const BREAK = Buffer.from("ff", "hex"); - -export default ( - input: any, - options: { collapseBigNumber: Boolean } = { collapseBigNumber: true }, -) => { - const outBufAry: Array = []; - - function pushFloat64(value: number) { - const buf = Buffer.allocUnsafe(8); - buf.writeDoubleBE(value); - outBufAry.push(buf); - } - function pushUInt8(value: number) { - const buf = Buffer.allocUnsafe(1); - buf.writeUInt8(value, 0); - outBufAry.push(buf); - } - function pushBuffer(value: Buffer) { - outBufAry.push(value); - } - function pushUInt16(value: number) { - const buf = Buffer.allocUnsafe(2); - buf.writeUInt16BE(value, 0); - outBufAry.push(buf); - } - function pushUInt32(value: number) { - const buf = Buffer.allocUnsafe(4); - buf.writeUInt32BE(value, 0); - outBufAry.push(buf); - } - function pushUInt64(value: number) { - const low = value % POW_2_32; - const high = (value - low) / POW_2_32; - const buf = Buffer.allocUnsafe(8); - buf.writeUInt32BE(high, 0); - buf.writeUInt32BE(low, 4); - outBufAry.push(buf); - } - function pushTypeAndLength(type: number, length: number) { - if (length < 24) { - pushUInt8((type << 5) | length); - } else if (length < 0x100) { - pushUInt8((type << 5) | 24); - pushUInt8(length); - } else if (length < 0x10000) { - pushUInt8((type << 5) | 25); - pushUInt16(length); - } else if (length < 0x100000000) { - pushUInt8((type << 5) | 26); - pushUInt32(length); - } else { - pushUInt8((type << 5) | 27); - pushUInt64(length); - } - } - function pushIntNum(value: number) { - if (Object.is(value, -0)) { - return pushBuffer(Buffer.from("f98000", "hex")); - } - if (value >= 0 && value <= POW_2_53) { - return pushTypeAndLength(0, value); - } - if (-POW_2_53 <= value && value < 0) { - return pushTypeAndLength(1, -(value + 1)); - } - } - function pushBigInt(value: BigNumber) { - let valueM = value; - let type = 0; - let tag = 2; - - if (valueM.isNegative()) { - valueM = valueM.negated().minus(1); - type = 1; - tag = 3; - } - - if (options.collapseBigNumber && valueM.lte(MAX_BIG_NUM_INT64)) { - if (valueM.lte(MAX_BIG_NUM_INT32)) { - return pushTypeAndLength(type, valueM.toNumber()); - } - pushUInt8((type << 5) | 27); - pushUInt32(valueM.dividedToIntegerBy(SHIFT32).toNumber()); - pushUInt32(valueM.mod(SHIFT32).toNumber()); - } else { - let str = valueM.toString(16); - if (str.length % 2) { - str = `0${str}`; - } - // push tag - pushTypeAndLength(6, tag); - - // push buffer - const buf = Buffer.from(str, "hex"); - pushTypeAndLength(2, buf.length); - pushBuffer(buf); - } - } - function pushBigNumber(value: BigNumber) { - if (value.isNaN()) { - pushBuffer(NAN_BUF); - } else if (value.isInteger()) { - pushBigInt(value); - } else { - // push decimal - pushTypeAndLength(6, 4); - pushTypeAndLength(4, 2); - const dec = value.decimalPlaces()!; - const slide = value.shiftedBy(dec); - pushIntNum(-dec); - if (slide.abs().isLessThan(MAX_BIG_NUM_INT)) { - pushIntNum(slide.toNumber()); - } else { - pushBigInt(slide); - } - } - } - - function encodeItem(value: any) { - if (value === false) return pushUInt8(0xf4); - if (value === true) return pushUInt8(0xf5); - if (value === null) return pushUInt8(0xf6); - if (value === undefined) return pushUInt8(0xf7); - - switch (typeof value) { - case "number": { - if (Math.round(value) === value) { - return pushIntNum(value); - } - pushUInt8(0xfb); - return pushFloat64(value); - } - case "string": { - const strBuff = Buffer.from(value, "utf8"); - pushTypeAndLength(3, strBuff.length); - return pushBuffer(strBuff); - } - default: { - if (Array.isArray(value)) { - if (value instanceof IndefiniteArray) { - pushUInt8((4 << 5) | 31); - } else { - pushTypeAndLength(4, value.length); - } - for (const v of value) { - encodeItem(v); - } - if (value instanceof IndefiniteArray) { - pushBuffer(BREAK); - } - } else if (value instanceof Buffer) { - pushTypeAndLength(2, value.length); - pushBuffer(value); - } else if (value instanceof ArrayBuffer) { - const buf = Buffer.from(value); - pushTypeAndLength(2, buf.length); - pushBuffer(buf); - } else if (value instanceof Uint8ClampedArray) { - const buf = Buffer.from(value); - pushTypeAndLength(2, buf.length); - pushBuffer(buf); - } else if (value instanceof Uint8Array) { - const buf = Buffer.from(value); - pushTypeAndLength(2, buf.length); - pushBuffer(buf); - } else if (BigNumber.isBigNumber(value)) { - pushBigNumber(value); - } else if (value instanceof CborTag) { - pushTypeAndLength(6, value.tag); - encodeItem(value.value); - } else { - let entries; - if (value instanceof Map) { - entries = [...value.entries()]; - } else { - entries = [...Object.entries(value)]; - } - if (value instanceof IndefiniteMap) { - pushUInt8((5 << 5) | 31); - } else { - pushTypeAndLength(5, entries.length); - } - for (const [key, v] of entries) { - encodeItem(key); - encodeItem(v); - } - if (value instanceof IndefiniteMap) { - pushBuffer(BREAK); - } - } - } - } - } - - encodeItem(input); - return Buffer.concat(outBufAry); -}; diff --git a/packages/mesh-core-cst/src/stricahq/cbors/helpers.ts b/packages/mesh-core-cst/src/stricahq/cbors/helpers.ts deleted file mode 100644 index 4fc759d9..00000000 --- a/packages/mesh-core-cst/src/stricahq/cbors/helpers.ts +++ /dev/null @@ -1,4 +0,0 @@ -/* eslint-disable max-classes-per-file */ -export class IndefiniteMap extends Map {} -export class IndefiniteArray extends Array {} -export { default as CborTag } from './cbor-tag'; \ No newline at end of file diff --git a/packages/mesh-core-cst/src/stricahq/cbors/index.ts b/packages/mesh-core-cst/src/stricahq/cbors/index.ts index d7e51de1..892776a0 100644 --- a/packages/mesh-core-cst/src/stricahq/cbors/index.ts +++ b/packages/mesh-core-cst/src/stricahq/cbors/index.ts @@ -1,6 +1,5 @@ -import encode from "./encoder"; - -export * from "./decoder"; -export const StricaEncoder = { - encode, -}; +import { cbors, StricaDecoder, StricaEncoder } from "./wrapper"; + +export default cbors; + +export { StricaEncoder, StricaDecoder }; diff --git a/packages/mesh-core-cst/src/stricahq/cbors/simple-value.ts b/packages/mesh-core-cst/src/stricahq/cbors/simple-value.ts deleted file mode 100644 index 80bda7d4..00000000 --- a/packages/mesh-core-cst/src/stricahq/cbors/simple-value.ts +++ /dev/null @@ -1,7 +0,0 @@ -export default class SimpleValue { - value: number; - - constructor(value: number) { - this.value = value; - } - } \ No newline at end of file diff --git a/packages/mesh-core-cst/src/stricahq/cbors/utils.ts b/packages/mesh-core-cst/src/stricahq/cbors/utils.ts deleted file mode 100644 index 7ce3b224..00000000 --- a/packages/mesh-core-cst/src/stricahq/cbors/utils.ts +++ /dev/null @@ -1,32 +0,0 @@ -/* eslint-disable max-classes-per-file */ -import { Buffer } from 'buffer'; -import BigNumber from 'bignumber.js'; - -const MAX_SAFE_HIGH = 0x1fffff; -export const SHIFT32 = 0x100000000; -export const POW_2_24 = 5.960464477539063e-8; -export const POW_2_32 = 4294967296; -export const POW_2_53 = 9007199254740992; -export const MAX_BIG_NUM_INT = new BigNumber('0x20000000000000'); -export const MAX_BIG_NUM_INT32 = new BigNumber('0xffffffff'); -export const MAX_BIG_NUM_INT64 = new BigNumber('0xffffffffffffffff'); - -export const getBigNum = (f: number, g: number): number | BigNumber => { - if (f > MAX_SAFE_HIGH) { - return new BigNumber(f).times(SHIFT32).plus(g); - } - return f * SHIFT32 + g; -}; - -export const addSpanBytesToObject = (obj: any, span: [number, number]): any => { - const spanObj = obj; - spanObj.byteSpan = span; - spanObj.getByteSpan = function (): [number, number] { - return this.byteSpan; - }; - - return spanObj; -}; - -const td = new TextDecoder('utf8', { fatal: true, ignoreBOM: true }); -export const utf8Decoder = (buf: Buffer) => td.decode(buf); \ No newline at end of file diff --git a/packages/mesh-core-cst/src/stricahq/cbors/wrapper.ts b/packages/mesh-core-cst/src/stricahq/cbors/wrapper.ts new file mode 100644 index 00000000..8d7335ab --- /dev/null +++ b/packages/mesh-core-cst/src/stricahq/cbors/wrapper.ts @@ -0,0 +1,9 @@ +import * as cjsCbors from "@stricahq/cbors"; + +export const cbors: typeof cjsCbors & { default?: typeof cjsCbors } = cjsCbors; +const exportedCbors = cbors?.default || cbors; + +export const StricaEncoder = exportedCbors.Encoder; + +export const StricaDecoder = exportedCbors.Decoder; +export type StricaDecoder = cjsCbors.Decoder;