From 2b28e5dd3e6d464e258a3f5c44021a31f374404d Mon Sep 17 00:00:00 2001 From: arnold Date: Tue, 18 Jul 2023 19:57:28 -0400 Subject: [PATCH 1/7] Build both as ESM and CommonJS. Fixes #113 --- .gitignore | 2 +- package.json | 13 ++++++++++--- tsconfig.cjs.json | 7 +++++++ tsconfig.esm.json | 8 ++++++++ tsconfig.json | 4 +--- tsconfig.types.json | 8 ++++++++ 6 files changed, 35 insertions(+), 7 deletions(-) create mode 100644 tsconfig.cjs.json create mode 100644 tsconfig.esm.json create mode 100644 tsconfig.types.json diff --git a/.gitignore b/.gitignore index 1872f94..3a79bb6 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,6 @@ dist/ +build/ node_modules/ -lib/ tmp-browser/ tmp-node/ test.ts diff --git a/package.json b/package.json index e2475c9..502c843 100644 --- a/package.json +++ b/package.json @@ -8,7 +8,7 @@ "scripts": { "clean": "rm -rf ./lib ./dist", "build": "webpack --mode production", - "compile": "tsc -p ./tsconfig.json", + "compile": "tsc -p ./tsconfig.types.json && tsc -p ./tsconfig.cjs.json && tsc -p ./tsconfig.esm.json", "test": "mocha --require ts-node/register 'test/**/*.spec.ts'", "lint": "eslint src --ext .ts,.tsx", "lint-fix": "eslint src --ext .ts,.tsx --fix", @@ -16,12 +16,19 @@ }, "author": "LTO Network", "license": "MIT", - "main": "lib/index.js", "files": [ - "lib", + "build", "src", "interfaces.d.ts" ], + "exports": { + ".": { + "types": "./build/index.d.ts", + "require": "./build/cjs/index.js", + "import": "./build/esm/index.js", + "default": "./build/esm/index.js" + } + }, "dependencies": { "@noble/curves": "^1.0.0", "@noble/hashes": "^1.3.0", diff --git a/tsconfig.cjs.json b/tsconfig.cjs.json new file mode 100644 index 0000000..75c4f89 --- /dev/null +++ b/tsconfig.cjs.json @@ -0,0 +1,7 @@ +{ + "extends": "./tsconfig.json", + "compilerOptions": { + "outDir": "build/cjs", + "module": "commonjs", + } +} diff --git a/tsconfig.esm.json b/tsconfig.esm.json new file mode 100644 index 0000000..58dfa28 --- /dev/null +++ b/tsconfig.esm.json @@ -0,0 +1,8 @@ +{ + "extends": "./tsconfig.json", + "compilerOptions": { + "outDir": "build/esm", + "module": "ES2020", + "moduleResolution": "node16" + } +} diff --git a/tsconfig.json b/tsconfig.json index b981192..87bbd55 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -8,12 +8,10 @@ ], "module": "commonjs", "sourceMap": true, - "declaration": true, + "declaration": false, "target": "es2020", "paths": {}, "rootDir": "src", - "outDir": "lib", - "allowJs": true, "esModuleInterop": true }, "include": ["src"] diff --git a/tsconfig.types.json b/tsconfig.types.json new file mode 100644 index 0000000..2db6234 --- /dev/null +++ b/tsconfig.types.json @@ -0,0 +1,8 @@ +{ + "extends": "./tsconfig.json", + "compilerOptions": { + "outDir": "build/types", + "declaration": true, + "emitDeclarationOnly": true, + } +} From b8a56e948e306ed9620938a1b9f464a48c501002 Mon Sep 17 00:00:00 2001 From: arnold Date: Tue, 18 Jul 2023 21:02:26 -0400 Subject: [PATCH 2/7] Fix building ESM module. Add package.json to esm build dir. --- package.json | 7 +++++-- src/index.ts | 2 +- tsconfig.esm.json | 3 +-- tsconfig.json | 5 ++--- tsconfig.types.json | 2 +- 5 files changed, 10 insertions(+), 9 deletions(-) diff --git a/package.json b/package.json index 502c843..b46874b 100644 --- a/package.json +++ b/package.json @@ -9,6 +9,7 @@ "clean": "rm -rf ./lib ./dist", "build": "webpack --mode production", "compile": "tsc -p ./tsconfig.types.json && tsc -p ./tsconfig.cjs.json && tsc -p ./tsconfig.esm.json", + "postcompile": "echo '{\"type\": \"module\"}' > ./build/esm/package.json", "test": "mocha --require ts-node/register 'test/**/*.spec.ts'", "lint": "eslint src --ext .ts,.tsx", "lint-fix": "eslint src --ext .ts,.tsx --fix", @@ -23,11 +24,13 @@ ], "exports": { ".": { - "types": "./build/index.d.ts", + "types": "./build/types/index.d.ts", "require": "./build/cjs/index.js", "import": "./build/esm/index.js", "default": "./build/esm/index.js" - } + }, + "./interfaces": "./interfaces.d.ts", + "./package.json": "./package.json" }, "dependencies": { "@noble/curves": "^1.0.0", diff --git a/src/index.ts b/src/index.ts index d2d75b1..3a1e2a3 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,6 +1,6 @@ import LTO from './LTO'; export default LTO; -export { LTO }; // Deprecated +export { LTO }; export { default as Binary } from './Binary'; diff --git a/tsconfig.esm.json b/tsconfig.esm.json index 58dfa28..0dda739 100644 --- a/tsconfig.esm.json +++ b/tsconfig.esm.json @@ -2,7 +2,6 @@ "extends": "./tsconfig.json", "compilerOptions": { "outDir": "build/esm", - "module": "ES2020", - "moduleResolution": "node16" + "module": "ESNext" } } diff --git a/tsconfig.json b/tsconfig.json index 87bbd55..cd04c71 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -6,13 +6,12 @@ "dom", "es2020" ], - "module": "commonjs", "sourceMap": true, "declaration": false, "target": "es2020", - "paths": {}, "rootDir": "src", - "esModuleInterop": true + "esModuleInterop": true, + "moduleResolution": "node16" }, "include": ["src"] } diff --git a/tsconfig.types.json b/tsconfig.types.json index 2db6234..e44516e 100644 --- a/tsconfig.types.json +++ b/tsconfig.types.json @@ -3,6 +3,6 @@ "compilerOptions": { "outDir": "build/types", "declaration": true, - "emitDeclarationOnly": true, + "emitDeclarationOnly": true } } From 2bf20cfaf89dd9e49d91d1249b6f801bd65a401a Mon Sep 17 00:00:00 2001 From: arnold Date: Tue, 18 Jul 2023 21:07:05 -0400 Subject: [PATCH 3/7] Export as individual submodules. Allows for tree-shaking. Fixes #112 [bump:minor] --- package.json | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ src/index.ts | 9 --------- 2 files changed, 48 insertions(+), 9 deletions(-) diff --git a/package.json b/package.json index b46874b..7086fba 100644 --- a/package.json +++ b/package.json @@ -29,6 +29,54 @@ "import": "./build/esm/index.js", "default": "./build/esm/index.js" }, + "./accounts": { + "types": "./build/types/accounts.d.ts", + "require": "./build/cjs/accounts.js", + "import": "./build/esm/accounts.js", + "default": "./build/esm/accounts.js" + }, + "./errors": { + "types": "./build/types/errors.d.ts", + "require": "./build/cjs/errors.js", + "import": "./build/esm/errors.js", + "default": "./build/esm/errors.js" + }, + "./events": { + "types": "./build/types/events.d.ts", + "require": "./build/cjs/events.js", + "import": "./build/esm/events.js", + "default": "./build/esm/events.js" + }, + "./identities": { + "types": "./build/types/identities.d.ts", + "require": "./build/cjs/identities.js", + "import": "./build/esm/identities.js", + "default": "./build/esm/identities.js" + }, + "./messages": { + "types": "./build/types/messages.d.ts", + "require": "./build/cjs/messages.js", + "import": "./build/esm/messages.js", + "default": "./build/esm/messages.js" + }, + "./node": { + "types": "./build/types/node.d.ts", + "require": "./build/cjs/node.js", + "import": "./build/esm/node.js", + "default": "./build/esm/node.js" + }, + "./transactions": { + "types": "./build/types/transactions.d.ts", + "require": "./build/cjs/transactions.js", + "import": "./build/esm/transactions.js", + "default": "./build/esm/transactions.js" + }, + "./utils": { + "types": "./build/types/utils.d.ts", + "require": "./build/cjs/utils.js", + "import": "./build/esm/utils.js", + "default": "./build/esm/utils.js" + }, "./interfaces": "./interfaces.d.ts", "./package.json": "./package.json" }, diff --git a/src/index.ts b/src/index.ts index 3a1e2a3..fb2d8cc 100644 --- a/src/index.ts +++ b/src/index.ts @@ -3,12 +3,3 @@ export default LTO; export { LTO }; export { default as Binary } from './Binary'; - -export * from './accounts'; -export * from './errors'; -export * from './events'; -export * from './identities'; -export * from './messages'; -export * from './node'; -export * from './transactions'; -export * from './utils'; From 5daaf141fee5c53a3d3c8edebc92c7d7a769109c Mon Sep 17 00:00:00 2001 From: arnold Date: Wed, 19 Jul 2023 09:26:04 -0400 Subject: [PATCH 4/7] Use `lib` instead of `build` for output dir of tsc. No need to change this. All our packages use `lib`. --- .gitignore | 2 +- package.json | 78 ++++++++++++++++++++++----------------------- tsconfig.cjs.json | 2 +- tsconfig.esm.json | 2 +- tsconfig.types.json | 2 +- 5 files changed, 43 insertions(+), 43 deletions(-) diff --git a/.gitignore b/.gitignore index 3a79bb6..f99f19d 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,5 @@ dist/ -build/ +lib/ node_modules/ tmp-browser/ tmp-node/ diff --git a/package.json b/package.json index 7086fba..6a90571 100644 --- a/package.json +++ b/package.json @@ -7,9 +7,9 @@ "bugs": "https://github.com/ltonetwork/lto-api.js/issues", "scripts": { "clean": "rm -rf ./lib ./dist", - "build": "webpack --mode production", + "lib": "webpack --mode production", "compile": "tsc -p ./tsconfig.types.json && tsc -p ./tsconfig.cjs.json && tsc -p ./tsconfig.esm.json", - "postcompile": "echo '{\"type\": \"module\"}' > ./build/esm/package.json", + "postcompile": "echo '{\"type\": \"module\"}' > ./lib/esm/package.json", "test": "mocha --require ts-node/register 'test/**/*.spec.ts'", "lint": "eslint src --ext .ts,.tsx", "lint-fix": "eslint src --ext .ts,.tsx --fix", @@ -18,64 +18,64 @@ "author": "LTO Network", "license": "MIT", "files": [ - "build", + "lib", "src", "interfaces.d.ts" ], "exports": { ".": { - "types": "./build/types/index.d.ts", - "require": "./build/cjs/index.js", - "import": "./build/esm/index.js", - "default": "./build/esm/index.js" + "types": "./lib/types/index.d.ts", + "require": "./lib/cjs/index.js", + "import": "./lib/esm/index.js", + "default": "./lib/esm/index.js" }, "./accounts": { - "types": "./build/types/accounts.d.ts", - "require": "./build/cjs/accounts.js", - "import": "./build/esm/accounts.js", - "default": "./build/esm/accounts.js" + "types": "./lib/types/accounts.d.ts", + "require": "./lib/cjs/accounts.js", + "import": "./lib/esm/accounts.js", + "default": "./lib/esm/accounts.js" }, "./errors": { - "types": "./build/types/errors.d.ts", - "require": "./build/cjs/errors.js", - "import": "./build/esm/errors.js", - "default": "./build/esm/errors.js" + "types": "./lib/types/errors.d.ts", + "require": "./lib/cjs/errors.js", + "import": "./lib/esm/errors.js", + "default": "./lib/esm/errors.js" }, "./events": { - "types": "./build/types/events.d.ts", - "require": "./build/cjs/events.js", - "import": "./build/esm/events.js", - "default": "./build/esm/events.js" + "types": "./lib/types/events.d.ts", + "require": "./lib/cjs/events.js", + "import": "./lib/esm/events.js", + "default": "./lib/esm/events.js" }, "./identities": { - "types": "./build/types/identities.d.ts", - "require": "./build/cjs/identities.js", - "import": "./build/esm/identities.js", - "default": "./build/esm/identities.js" + "types": "./lib/types/identities.d.ts", + "require": "./lib/cjs/identities.js", + "import": "./lib/esm/identities.js", + "default": "./lib/esm/identities.js" }, "./messages": { - "types": "./build/types/messages.d.ts", - "require": "./build/cjs/messages.js", - "import": "./build/esm/messages.js", - "default": "./build/esm/messages.js" + "types": "./lib/types/messages.d.ts", + "require": "./lib/cjs/messages.js", + "import": "./lib/esm/messages.js", + "default": "./lib/esm/messages.js" }, "./node": { - "types": "./build/types/node.d.ts", - "require": "./build/cjs/node.js", - "import": "./build/esm/node.js", - "default": "./build/esm/node.js" + "types": "./lib/types/node.d.ts", + "require": "./lib/cjs/node.js", + "import": "./lib/esm/node.js", + "default": "./lib/esm/node.js" }, "./transactions": { - "types": "./build/types/transactions.d.ts", - "require": "./build/cjs/transactions.js", - "import": "./build/esm/transactions.js", - "default": "./build/esm/transactions.js" + "types": "./lib/types/transactions.d.ts", + "require": "./lib/cjs/transactions.js", + "import": "./lib/esm/transactions.js", + "default": "./lib/esm/transactions.js" }, "./utils": { - "types": "./build/types/utils.d.ts", - "require": "./build/cjs/utils.js", - "import": "./build/esm/utils.js", - "default": "./build/esm/utils.js" + "types": "./lib/types/utils.d.ts", + "require": "./lib/cjs/utils.js", + "import": "./lib/esm/utils.js", + "default": "./lib/esm/utils.js" }, "./interfaces": "./interfaces.d.ts", "./package.json": "./package.json" diff --git a/tsconfig.cjs.json b/tsconfig.cjs.json index 75c4f89..1a419b9 100644 --- a/tsconfig.cjs.json +++ b/tsconfig.cjs.json @@ -1,7 +1,7 @@ { "extends": "./tsconfig.json", "compilerOptions": { - "outDir": "build/cjs", + "outDir": "lib/cjs", "module": "commonjs", } } diff --git a/tsconfig.esm.json b/tsconfig.esm.json index 0dda739..0b9ecd6 100644 --- a/tsconfig.esm.json +++ b/tsconfig.esm.json @@ -1,7 +1,7 @@ { "extends": "./tsconfig.json", "compilerOptions": { - "outDir": "build/esm", + "outDir": "lib/esm", "module": "ESNext" } } diff --git a/tsconfig.types.json b/tsconfig.types.json index e44516e..b458ee1 100644 --- a/tsconfig.types.json +++ b/tsconfig.types.json @@ -1,7 +1,7 @@ { "extends": "./tsconfig.json", "compilerOptions": { - "outDir": "build/types", + "outDir": "lib/types", "declaration": true, "emitDeclarationOnly": true } From 6a73bbf9403e3767fa8869578aed4cc7cebc9b09 Mon Sep 17 00:00:00 2001 From: arnold Date: Fri, 21 Jul 2023 09:47:45 -0400 Subject: [PATCH 5/7] Fix ESM build. Thanks to @Starush-1 Added post compile script that modifies the js files in lib/esm. It appends `.js` for imported files and `/index.js` for imported directories. Fix `exports` in package.json. Fix imports that break in esm. Fix tests. Closes #112 Closes #113 --- bin/test-lto-identity-builder.js | 86 ------------------- package.json | 66 +++++++------- scripts/esm-postcompile.js | 45 ++++++++++ src/accounts/ecdsa/AccountFactoryECDSA.ts | 2 +- src/accounts/ecdsa/ECDSA.ts | 2 +- src/accounts/ed25519/AccountFactoryED25519.ts | 2 +- src/accounts/ed25519/ED25519.ts | 2 +- src/utils/encrypt-seed.ts | 2 +- test/LTO.spec.ts | 3 +- test/accounts/ed25519.spec.ts | 3 +- test/identities/IdentityBuilder.spec.ts | 3 +- 11 files changed, 88 insertions(+), 128 deletions(-) delete mode 100644 bin/test-lto-identity-builder.js create mode 100644 scripts/esm-postcompile.js diff --git a/bin/test-lto-identity-builder.js b/bin/test-lto-identity-builder.js deleted file mode 100644 index 4ada650..0000000 --- a/bin/test-lto-identity-builder.js +++ /dev/null @@ -1,86 +0,0 @@ -// test-lto-identity-builder.js -// ---------------------------- -// This file is only for testing the `LTO.IdentityBuilder` class against TestNet -// -// For this, make sure you have run `npm run build` to create a `dist` folder, -// and then run this file by using `node ./bin/test-lto-identity-builder.js` -// -// This will send the transactions necessary for testing the builder: -// > One transfer tx from `sender` to `recipient` of 0.35 LTO -// > One anchor tx from `recipient` -// > One association from `sender` with `recipient` as the `party` -// -// Note: the inital transfer might take a few seconds to process, -// so if your `recipient` wallet is empty, the anchor tx will fail. -// You can either choose a wallet that you know has funds or make the code -// wait before sending the anchor tx. - -const path = require("path"); -const https = require("https"); - -const API = require(path.join(__dirname, "..", "dist", "lto-api")); - -const execute = async () => { - const lto = new API.LTO("T"); - - // If you want to specify an account/wallet to use, just uncomment the below - // lines and add your seed to `senderSeed` or `recipientSeed` (or both) - // - // const sender = new API.Account(senderSeed, "T"); - // const recipient = new API.Account(recipientSeed, "T"); - - const sender = lto.createAccount(); - const recipient = lto.createAccount(); - - const identity = new API.IdentityBuilder(sender); - - identity.addVerificationMethod(recipient, 1); - - for (const transaction of identity.transactions) { - delete transaction.id; // id is not necessary when broadcasting tx - - const res = await httpPost({ - hostname: "testnet.lto.network", - path: "/transactions/broadcast", - headers: { "Content-Type": "application/json" }, - body: JSON.stringify(transaction), - }); - - console.log("=================================="); - console.log("Response: ", res); - } -}; - -const httpPost = ({ body, ...options }) => { - return new Promise((resolve, reject) => { - const req = https.request( - { - method: "POST", - ...options, - }, - (res) => { - const chunks = []; - res.on("data", (data) => chunks.push(data)); - res.on("end", () => { - let body = Buffer.concat(chunks); - - switch (res.headers["content-type"]) { - case "application/json": - body = JSON.parse(body); - break; - } - - resolve(body); - }); - } - ); - - req.on("error", reject); - - if (body) req.write(body); - - req.end(); - }); -}; - -execute(); \ No newline at end of file diff --git a/package.json b/package.json index 6a90571..c028efe 100644 --- a/package.json +++ b/package.json @@ -9,7 +9,7 @@ "clean": "rm -rf ./lib ./dist", "lib": "webpack --mode production", "compile": "tsc -p ./tsconfig.types.json && tsc -p ./tsconfig.cjs.json && tsc -p ./tsconfig.esm.json", - "postcompile": "echo '{\"type\": \"module\"}' > ./lib/esm/package.json", + "postcompile": "node ./scripts/esm-postcompile.js ./lib/esm", "test": "mocha --require ts-node/register 'test/**/*.spec.ts'", "lint": "eslint src --ext .ts,.tsx", "lint-fix": "eslint src --ext .ts,.tsx --fix", @@ -30,52 +30,52 @@ "default": "./lib/esm/index.js" }, "./accounts": { - "types": "./lib/types/accounts.d.ts", - "require": "./lib/cjs/accounts.js", - "import": "./lib/esm/accounts.js", - "default": "./lib/esm/accounts.js" + "types": "./lib/types/accounts/index.d.ts", + "require": "./lib/cjs/accounts/index.js", + "import": "./lib/esm/accounts/index.js", + "default": "./lib/esm/accounts/index.js" }, "./errors": { - "types": "./lib/types/errors.d.ts", - "require": "./lib/cjs/errors.js", - "import": "./lib/esm/errors.js", - "default": "./lib/esm/errors.js" + "types": "./lib/types/errors/index.d.ts", + "require": "./lib/cjs/errors/index.js", + "import": "./lib/esm/errors/index.js", + "default": "./lib/esm/errors/index.js" }, "./events": { - "types": "./lib/types/events.d.ts", - "require": "./lib/cjs/events.js", - "import": "./lib/esm/events.js", - "default": "./lib/esm/events.js" + "types": "./lib/types/events/index.d.ts", + "require": "./lib/cjs/events/index.js", + "import": "./lib/esm/events/index.js", + "default": "./lib/esm/events/index.js" }, "./identities": { - "types": "./lib/types/identities.d.ts", - "require": "./lib/cjs/identities.js", - "import": "./lib/esm/identities.js", - "default": "./lib/esm/identities.js" + "types": "./lib/types/identities/index.d.ts", + "require": "./lib/cjs/identities/index.js", + "import": "./lib/esm/identities/index.js", + "default": "./lib/esm/identities/index.js" }, "./messages": { - "types": "./lib/types/messages.d.ts", - "require": "./lib/cjs/messages.js", - "import": "./lib/esm/messages.js", - "default": "./lib/esm/messages.js" + "types": "./lib/types/messages/index.d.ts", + "require": "./lib/cjs/messages/index.js", + "import": "./lib/esm/messages/index.js", + "default": "./lib/esm/messages/index.js" }, "./node": { - "types": "./lib/types/node.d.ts", - "require": "./lib/cjs/node.js", - "import": "./lib/esm/node.js", - "default": "./lib/esm/node.js" + "types": "./lib/types/node/index.d.ts", + "require": "./lib/cjs/node/index.js", + "import": "./lib/esm/node/index.js", + "default": "./lib/esm/node/index.js" }, "./transactions": { - "types": "./lib/types/transactions.d.ts", - "require": "./lib/cjs/transactions.js", - "import": "./lib/esm/transactions.js", - "default": "./lib/esm/transactions.js" + "types": "./lib/types/transactions/index.d.ts", + "require": "./lib/cjs/transactions/index.js", + "import": "./lib/esm/transactions/index.js", + "default": "./lib/esm/transactions/index.js" }, "./utils": { - "types": "./lib/types/utils.d.ts", - "require": "./lib/cjs/utils.js", - "import": "./lib/esm/utils.js", - "default": "./lib/esm/utils.js" + "types": "./lib/types/utils/index.d.ts", + "require": "./lib/cjs/utils/index.js", + "import": "./lib/esm/utils/index.js", + "default": "./lib/esm/utils/index.js" }, "./interfaces": "./interfaces.d.ts", "./package.json": "./package.json" diff --git a/scripts/esm-postcompile.js b/scripts/esm-postcompile.js new file mode 100644 index 0000000..f22db09 --- /dev/null +++ b/scripts/esm-postcompile.js @@ -0,0 +1,45 @@ +const fs = require('fs'); +const path = require('path'); + +// Define the directory containing your compiled JavaScript files +const OUTPUT_DIR = process.argv[2]; + +// Recursive function to process all JavaScript files in the directory +function processDirectory(dir) { + const promises = []; + const files = fs.readdirSync(dir); + + for (const file of files) { + const fullPath = path.join(dir, file); + const stat = fs.statSync(fullPath); + + if (stat.isDirectory()) { + processDirectory(fullPath); + continue; + } + + if (!file.endsWith('.js')) continue; + + const promise = fs.promises.readFile(fullPath, 'utf-8').then((content) => { + const updatedContent = content.replace(/\bfrom\s+(["'])(\..+)\1/g, (original, quote, file) => { + const includePath = path.resolve(dir, file); + if (file.endsWith('.js')) return original; + if (fs.existsSync(`${includePath}.js`)) return `from ${quote}${file}.js${quote}`; + if (fs.statSync(includePath).isDirectory()) return `from ${quote}${file}/index.js${quote}`; + return original; + }); + + fs.writeFileSync(fullPath, updatedContent); + }); + + promises.push(promise); + } + + return Promise.all(promises); +} + +fs.writeFileSync(OUTPUT_DIR + '/package.json', JSON.stringify({ type: 'module' })); + +processDirectory(OUTPUT_DIR).then(() => { + console.log('Post-compile script executed successfully.'); +}); diff --git a/src/accounts/ecdsa/AccountFactoryECDSA.ts b/src/accounts/ecdsa/AccountFactoryECDSA.ts index e41e7ff..952b626 100644 --- a/src/accounts/ecdsa/AccountFactoryECDSA.ts +++ b/src/accounts/ecdsa/AccountFactoryECDSA.ts @@ -9,7 +9,7 @@ import { secp256k1 } from '@noble/curves/secp256k1'; import { secp256r1 } from '@noble/curves/p256'; import { mnemonicToSeedSync, generateMnemonic } from '@scure/bip39'; import { HDKey } from '@scure/bip32'; -import { wordlist } from '@scure/bip39/wordlists/english'; +import { wordlist } from '@scure/bip39/wordlists/english.js'; import { DEFAULT_DERIVATION_PATH } from '../../constants'; export default class AccountFactoryECDSA extends AccountFactory { diff --git a/src/accounts/ecdsa/ECDSA.ts b/src/accounts/ecdsa/ECDSA.ts index 1edb2e5..ec8ae32 100644 --- a/src/accounts/ecdsa/ECDSA.ts +++ b/src/accounts/ecdsa/ECDSA.ts @@ -5,7 +5,7 @@ import { sha512 } from '@noble/hashes/sha512'; import { secp256k1 } from '@noble/curves/secp256k1'; import { secp256r1 } from '@noble/curves/p256'; import { hmac } from '@noble/hashes/hmac'; -import * as AES from 'crypto-js/aes'; +import * as AES from 'crypto-js/aes.js'; import Binary from '../../Binary'; import { compareBytes } from '../../utils/bytes'; import { hexToBytes } from '@noble/hashes/utils'; diff --git a/src/accounts/ed25519/AccountFactoryED25519.ts b/src/accounts/ed25519/AccountFactoryED25519.ts index 5a9a9c8..fd3e49b 100644 --- a/src/accounts/ed25519/AccountFactoryED25519.ts +++ b/src/accounts/ed25519/AccountFactoryED25519.ts @@ -1,7 +1,7 @@ import AccountFactory from '../AccountFactory'; import Account from '../Account'; import { IKeyPairBytes } from '../../../interfaces'; -import * as nacl from 'tweetnacl'; +import nacl from 'tweetnacl'; import { base58 } from '@scure/base'; import { ED25519 } from './ED25519'; import ed2curve from 'ed2curve'; diff --git a/src/accounts/ed25519/ED25519.ts b/src/accounts/ed25519/ED25519.ts index 2dfcf90..218bb7a 100644 --- a/src/accounts/ed25519/ED25519.ts +++ b/src/accounts/ed25519/ED25519.ts @@ -1,6 +1,6 @@ import { Cypher } from '../Cypher'; import { IKeyPairBytes } from '../../../interfaces'; -import * as nacl from 'tweetnacl'; +import nacl from 'tweetnacl'; import { blake2b } from '@noble/hashes/blake2b'; import { DecryptError } from '../../errors'; diff --git a/src/utils/encrypt-seed.ts b/src/utils/encrypt-seed.ts index 225127e..0e04cd2 100644 --- a/src/utils/encrypt-seed.ts +++ b/src/utils/encrypt-seed.ts @@ -1,4 +1,4 @@ -import * as AES from 'crypto-js/aes'; +import * as AES from 'crypto-js/aes.js'; import { sha256 } from '@noble/hashes/sha256'; import { bytesToHex, hexToBytes } from '@noble/hashes/utils'; import { SEED_ENCRYPTION_ROUNDS } from '../constants'; diff --git a/test/LTO.spec.ts b/test/LTO.spec.ts index 03485f4..d8d72ee 100644 --- a/test/LTO.spec.ts +++ b/test/LTO.spec.ts @@ -1,10 +1,11 @@ import { expect } from 'chai'; import * as sinon from 'sinon'; -import LTO, { Binary, encryptSeed } from '../src'; +import LTO, { Binary } from '../src'; import { PublicNode } from '../src/node'; import { Relay } from '../src/messages'; import { Account, AccountFactory, AccountFactoryECDSA, AccountFactoryED25519 } from '../src/accounts'; +import { encryptSeed } from '../src/utils'; describe('LTO', () => { let lto: LTO; diff --git a/test/accounts/ed25519.spec.ts b/test/accounts/ed25519.spec.ts index 62e97d7..c3846bc 100644 --- a/test/accounts/ed25519.spec.ts +++ b/test/accounts/ed25519.spec.ts @@ -2,8 +2,9 @@ import { assert, expect } from 'chai'; import { AccountFactoryED25519 } from '../../src/accounts'; -import { decryptSeed, Binary } from '../../src'; +import { Binary } from '../../src'; import { ED25519 } from '../../src/accounts/ed25519/ED25519'; +import { decryptSeed } from '../../src/utils'; describe('ed25519 account', () => { const seed = 'satisfy sustain shiver skill betray mother appear pupil coconut weasel firm top puzzle monkey seek'; diff --git a/test/identities/IdentityBuilder.spec.ts b/test/identities/IdentityBuilder.spec.ts index 9301425..be21cbd 100644 --- a/test/identities/IdentityBuilder.spec.ts +++ b/test/identities/IdentityBuilder.spec.ts @@ -3,8 +3,7 @@ import { assert, expect } from 'chai'; import { IdentityBuilder } from '../../src/identities'; import { AccountFactoryED25519 as AccountFactory } from '../../src/accounts'; -import { Register, Association, Anchor, Statement } from '../../src/transactions'; -import { Data, RevokeAssociation } from '../../src'; +import { Register, Association, Anchor, Statement, Data, RevokeAssociation } from '../../src/transactions'; import DataEntry from '../../src/transactions/DataEntry'; import { ASSOCIATION_TYPE_DID_DISABLE_CAPABILITY, From 02ea9795068496c1485e2ce584c87a8f2a0c771f Mon Sep 17 00:00:00 2001 From: arnold Date: Fri, 21 Jul 2023 10:40:46 -0400 Subject: [PATCH 6/7] Split up interfaces.d.ts and move it to src/types. Also export constants in package.json. --- README.md | 21 ++- interfaces.d.ts | 140 ------------------ package.json | 8 +- src/Binary.ts | 2 +- src/LTO.ts | 2 +- src/accounts/Account.ts | 2 +- src/accounts/Cypher.ts | 2 +- src/accounts/ecdsa/AccountFactoryECDSA.ts | 2 +- src/accounts/ecdsa/ECDSA.ts | 2 +- src/accounts/ed25519/AccountFactoryED25519.ts | 2 +- src/accounts/ed25519/ED25519.ts | 2 +- src/accounts/index.ts | 2 +- src/events/Event.ts | 2 +- src/events/EventChain.ts | 2 +- src/identities/IdentityBuilder.ts | 2 +- src/messages/Message.ts | 2 +- src/node/PublicNode.ts | 2 +- src/transactions/Anchor.ts | 2 +- src/transactions/Association.ts | 2 +- src/transactions/Burn.ts | 2 +- src/transactions/CancelLease.ts | 2 +- src/transactions/CancelSponsorship.ts | 2 +- src/transactions/Data.ts | 2 +- src/transactions/Lease.ts | 2 +- src/transactions/MappedAnchor.ts | 2 +- src/transactions/MassTransfer.ts | 2 +- src/transactions/Register.ts | 2 +- src/transactions/RevokeAssociation.ts | 2 +- src/transactions/Sponsorship.ts | 2 +- src/transactions/Statement.ts | 2 +- src/transactions/Transaction.ts | 2 +- src/transactions/Transfer.ts | 2 +- src/transactions/index.ts | 2 +- src/types/accounts.d.ts | 34 +++++ src/types/binary.d.ts | 17 +++ src/types/events.d.ts | 17 +++ src/types/identities.d.ts | 14 ++ src/types/index.d.ts | 27 ++++ src/types/messages.d.ts | 21 +++ src/types/transactions.d.ts | 13 ++ src/utils/base64.ts | 2 +- src/utils/convert.ts | 2 +- src/utils/crypto.ts | 2 +- test/events/Event.spec.ts | 2 +- test/events/EventChain.spec.ts | 2 +- test/message/Message.spec.ts | 2 +- test/node/PublicNode.spec.ts | 2 +- tsconfig.json | 3 +- 48 files changed, 196 insertions(+), 193 deletions(-) delete mode 100644 interfaces.d.ts create mode 100644 src/types/accounts.d.ts create mode 100644 src/types/binary.d.ts create mode 100644 src/types/events.d.ts create mode 100644 src/types/identities.d.ts create mode 100644 src/types/index.d.ts create mode 100644 src/types/messages.d.ts create mode 100644 src/types/transactions.d.ts diff --git a/README.md b/README.md index 22caf54..28334c5 100644 --- a/README.md +++ b/README.md @@ -30,7 +30,7 @@ const lto = new LTO('T'); const account = lto.account(); const seed = 'satisfy sustain shiver skill betray mother appear pupil coconut weasel firm top puzzle monkey seek'; -const accountFromSeed = lto.account({seed: seed}); +const accountFromSeed = lto.account({ seed }); lto.transfer(account, recipient, 100_00000000); lto.massTransfer(account, [{recipient: recipient1, amount: 100_00000000}, {recipient: recipient2, amount: 50_00000000}]); @@ -60,7 +60,7 @@ useful if you want to use the library in a browser environment. You can download the bundle from the [GitHub releases page](https://github.com/ltonetwork/lto-api.js/releases). -The library is bundles as a UMD module. This means you can use it in the browser as a global variable, or you can +The library is bundles as a UMD module. This means you can use it in the browser as global variable `LTO`, or you can import it as a module in your JavaScript code. ### Browser @@ -68,23 +68,20 @@ import it as a module in your JavaScript code. ```html ``` ### Troubleshooting -Global variable `LTO` is an object with all exported classes and functions. It contains the `LTO` class, -which is the main class of the library. If you try to do `new LTO()` you will get the error: +Global variable `LTO` is an object with all exported classes and functions. If you try to do `new LTO()` you will get +the error: TypeError: LTO is not a constructor -Make sure you do - -```js -const { LTO } = window.LTO; -``` +Use `LTO.connect()` instead. diff --git a/interfaces.d.ts b/interfaces.d.ts deleted file mode 100644 index 5f64813..0000000 --- a/interfaces.d.ts +++ /dev/null @@ -1,140 +0,0 @@ -export type TBinary = Uint8Array | number[]; -export type TKeyType = 'ed25519' | 'secp256k1' | 'secp256r1'; - -export interface IAccountIn { - address?: string; - publicKey?: string | Uint8Array; - privateKey?: string | Uint8Array; - keyType?: string; - seed?: string; - seedPassword?: string; - nonce?: number | string | Uint8Array; - derivationPath?: string; - parent?: { - seed: string; - keyType?: string; - }; -} - -export interface ISigner { - sign(message: string | Uint8Array): IBinary; - address: string; - publicKey: string; - keyType: TKeyType; -} - -export interface ISignable { - signWith(account: ISigner): this; -} - -export interface IPublicAccount { - keyType: TKeyType; - publicKey: string; -} - -export interface IKeyPair { - privateKey?: string; - publicKey: string; -} - -export interface IBinary extends Uint8Array { - readonly base58: string; - readonly base64: string; - readonly hex: string; - toString(): string; - hash(): IBinary; - slice(start?: number, end?: number): IBinary; - reverse(): IBinary; - toReversed(): IBinary; -} - -export interface IKeyPairBytes { - privateKey?: IBinary; - publicKey: IBinary; -} - -export type IPair = { - key: T; - value: T; -}; - -export interface ITxJSON extends Record { - type: number; -} - -export interface IEventChainJSON extends Record { - id: string; - events: Array; -} - -export interface IEventJSON { - timestamp?: number; - previous?: string; - signKey?: IPublicAccount; - signature?: string; - hash?: string; - mediaType: string; - data: string; - attachments?: Array<{ name: string; mediaType: string; data: string }>; -} - -interface IMessageJSONBase { - type: string; - sender: IPublicAccount; - recipient: string; - timestamp: Date | string; - signature?: string; - hash?: string; -} - -interface IMessageJSONEncrypted extends IMessageJSONBase { - encryptedData: string; -} - -interface IMessageJSONUnencrypted extends IMessageJSONBase { - mediaType: string; - data: string; -} - -export type IMessageJSON = IMessageJSONEncrypted | IMessageJSONUnencrypted; - -export interface ITransfer { - recipient: string; - amount: number; -} - -export interface IDIDService { - id?: string; - type: string; - serviceEndpoint: string | Record | Array>; - description?: string; - [key: string]: any; -} - -export type TDIDRelationship = - | 'authentication' - | 'assertionMethod' - | 'keyAgreement' - | 'capabilityInvocation' - | 'capabilityDelegation'; - -// Missing interfaces -declare global { - interface Window { - msCrypto?: any; - Response?: any; - Promise: PromiseConstructor; - } - - interface ErrorConstructor { - captureStackTrace(thisArg: any, func: any): void; - } -} - -// Replacement for --allowJs -declare module '*.js' { - const content: { - [key: string]: any; - }; - export = content; -} diff --git a/package.json b/package.json index c028efe..49ce415 100644 --- a/package.json +++ b/package.json @@ -7,7 +7,8 @@ "bugs": "https://github.com/ltonetwork/lto-api.js/issues", "scripts": { "clean": "rm -rf ./lib ./dist", - "lib": "webpack --mode production", + "build": "webpack --mode production", + "precompile": "rm -rf ./lib", "compile": "tsc -p ./tsconfig.types.json && tsc -p ./tsconfig.cjs.json && tsc -p ./tsconfig.esm.json", "postcompile": "node ./scripts/esm-postcompile.js ./lib/esm", "test": "mocha --require ts-node/register 'test/**/*.spec.ts'", @@ -20,7 +21,7 @@ "files": [ "lib", "src", - "interfaces.d.ts" + "src/types/interfaces.d.ts" ], "exports": { ".": { @@ -77,7 +78,8 @@ "import": "./lib/esm/utils/index.js", "default": "./lib/esm/utils/index.js" }, - "./interfaces": "./interfaces.d.ts", + "./types": "./lib/types/index.d.ts", + "./constants": "./lib/constants.js", "./package.json": "./package.json" }, "dependencies": { diff --git a/src/Binary.ts b/src/Binary.ts index e8508e3..a00109b 100644 --- a/src/Binary.ts +++ b/src/Binary.ts @@ -1,4 +1,4 @@ -import { IBinary } from '../interfaces'; +import { IBinary } from './types'; import { sha256 } from '@noble/hashes/sha256'; import { int16ToBytes, int32ToBytes } from './utils/bytes'; import { bytesToHex, hexToBytes } from '@noble/hashes/utils'; diff --git a/src/LTO.ts b/src/LTO.ts index bdfb141..f64c10c 100644 --- a/src/LTO.ts +++ b/src/LTO.ts @@ -2,7 +2,7 @@ import { Account, AccountFactoryED25519, AccountFactoryECDSA, AccountFactory } f import { PublicNode } from './node'; import { isValidAddress } from './utils'; import { DEFAULT_MAINNET_NODE, DEFAULT_TESTNET_NODE, DEFAULT_RELAY_SERVICE } from './constants'; -import { IAccountIn, IPair, ITransfer, ISigner, IPublicAccount } from '../interfaces'; +import { IAccountIn, IPair, ITransfer, ISigner, IPublicAccount } from './types'; import { Anchor, Association, diff --git a/src/accounts/Account.ts b/src/accounts/Account.ts index fc4d9a3..053f332 100644 --- a/src/accounts/Account.ts +++ b/src/accounts/Account.ts @@ -1,4 +1,4 @@ -import { IKeyPairBytes, ISignable, ISigner, TKeyType } from '../../interfaces'; +import { IKeyPairBytes, ISignable, ISigner, TKeyType } from '../types'; import { Cypher } from './Cypher'; import Binary from '../Binary'; import { SEED_ENCRYPTION_ROUNDS } from '../constants'; diff --git a/src/accounts/Cypher.ts b/src/accounts/Cypher.ts index f21152f..4fac692 100644 --- a/src/accounts/Cypher.ts +++ b/src/accounts/Cypher.ts @@ -1,4 +1,4 @@ -import { TKeyType } from '../../interfaces'; +import { TKeyType } from '../types'; export abstract class Cypher { readonly keyType: TKeyType; diff --git a/src/accounts/ecdsa/AccountFactoryECDSA.ts b/src/accounts/ecdsa/AccountFactoryECDSA.ts index 952b626..e68c4fa 100644 --- a/src/accounts/ecdsa/AccountFactoryECDSA.ts +++ b/src/accounts/ecdsa/AccountFactoryECDSA.ts @@ -1,6 +1,6 @@ import AccountFactory from '../AccountFactory'; import Account from '../Account'; -import { IKeyPairBytes } from '../../../interfaces'; +import { IKeyPairBytes } from '../../types'; import { buildAddress } from '../../utils'; import { compressPublicKey, decompressPublicKey } from '../../utils/ecdsa'; import Binary from '../../Binary'; diff --git a/src/accounts/ecdsa/ECDSA.ts b/src/accounts/ecdsa/ECDSA.ts index ec8ae32..0ffcccf 100644 --- a/src/accounts/ecdsa/ECDSA.ts +++ b/src/accounts/ecdsa/ECDSA.ts @@ -1,5 +1,5 @@ import { Cypher } from '../Cypher'; -import { IKeyPairBytes } from '../../../interfaces'; +import { IKeyPairBytes } from '../../types'; import { sha256 } from '@noble/hashes/sha256'; import { sha512 } from '@noble/hashes/sha512'; import { secp256k1 } from '@noble/curves/secp256k1'; diff --git a/src/accounts/ed25519/AccountFactoryED25519.ts b/src/accounts/ed25519/AccountFactoryED25519.ts index fd3e49b..16bbb20 100644 --- a/src/accounts/ed25519/AccountFactoryED25519.ts +++ b/src/accounts/ed25519/AccountFactoryED25519.ts @@ -1,6 +1,6 @@ import AccountFactory from '../AccountFactory'; import Account from '../Account'; -import { IKeyPairBytes } from '../../../interfaces'; +import { IKeyPairBytes } from '../../types'; import nacl from 'tweetnacl'; import { base58 } from '@scure/base'; import { ED25519 } from './ED25519'; diff --git a/src/accounts/ed25519/ED25519.ts b/src/accounts/ed25519/ED25519.ts index 218bb7a..6a85fe6 100644 --- a/src/accounts/ed25519/ED25519.ts +++ b/src/accounts/ed25519/ED25519.ts @@ -1,5 +1,5 @@ import { Cypher } from '../Cypher'; -import { IKeyPairBytes } from '../../../interfaces'; +import { IKeyPairBytes } from '../../types'; import nacl from 'tweetnacl'; import { blake2b } from '@noble/hashes/blake2b'; import { DecryptError } from '../../errors'; diff --git a/src/accounts/index.ts b/src/accounts/index.ts index 164d124..0a6ae09 100644 --- a/src/accounts/index.ts +++ b/src/accounts/index.ts @@ -1,7 +1,7 @@ import { Cypher } from './Cypher'; import { ED25519 } from './ed25519/ED25519'; import { ECDSA } from './ecdsa/ECDSA'; -import { IBinary } from '../../interfaces'; +import { IBinary } from '../types'; export { default as Account } from './Account'; export { default as AccountFactory } from './AccountFactory'; diff --git a/src/events/Event.ts b/src/events/Event.ts index f273e6e..e9fca68 100644 --- a/src/events/Event.ts +++ b/src/events/Event.ts @@ -1,6 +1,6 @@ import * as convert from '../utils/convert'; -import { IBinary, IEventJSON, ISigner, TKeyType } from '../../interfaces'; +import { IBinary, IEventJSON, ISigner, TKeyType } from '../types'; import EventChain, { EVENT_CHAIN_V1, EVENT_CHAIN_V2 } from './EventChain'; import Binary from '../Binary'; import { concatBytes } from '@noble/hashes/utils'; diff --git a/src/events/EventChain.ts b/src/events/EventChain.ts index a045843..ddba140 100644 --- a/src/events/EventChain.ts +++ b/src/events/EventChain.ts @@ -1,4 +1,4 @@ -import { IBinary, IEventChainJSON, IEventJSON, ISigner } from '../../interfaces'; +import { IBinary, IEventChainJSON, IEventJSON, ISigner } from '../types'; import Event from './Event'; import Binary from '../Binary'; import MergeConflict from './MergeConflict'; diff --git a/src/identities/IdentityBuilder.ts b/src/identities/IdentityBuilder.ts index 892ac70..74a5c62 100644 --- a/src/identities/IdentityBuilder.ts +++ b/src/identities/IdentityBuilder.ts @@ -1,7 +1,7 @@ import { Account } from '../accounts'; import { Anchor, Association, Data, Register, RevokeAssociation, Statement } from '../transactions'; import Transaction from '../transactions/Transaction'; -import { IDIDService, TDIDRelationship } from '../../interfaces'; +import { IDIDService, TDIDRelationship } from '../types'; import { ASSOCIATION_TYPE_DID_DISABLE_CAPABILITY, ASSOCIATION_TYPE_DID_VERIFICATION_METHOD, diff --git a/src/messages/Message.ts b/src/messages/Message.ts index 9e3726f..a8b7b80 100644 --- a/src/messages/Message.ts +++ b/src/messages/Message.ts @@ -1,4 +1,4 @@ -import { IBinary, IMessageJSON, TKeyType } from '../../interfaces'; +import { IBinary, IMessageJSON, TKeyType } from '../types'; import Binary from '../Binary'; import { Account, cypher } from '../accounts'; import { concatBytes } from '@noble/hashes/utils'; diff --git a/src/node/PublicNode.ts b/src/node/PublicNode.ts index c785eb8..029545d 100644 --- a/src/node/PublicNode.ts +++ b/src/node/PublicNode.ts @@ -1,6 +1,6 @@ import Transaction from '../transactions/Transaction'; import { txFromData } from '../transactions'; -import { ITxJSON } from '../../interfaces'; +import { ITxJSON } from '../types'; import { RequestError } from '../errors'; export default class PublicNode { diff --git a/src/transactions/Anchor.ts b/src/transactions/Anchor.ts index b59134d..9c98db4 100644 --- a/src/transactions/Anchor.ts +++ b/src/transactions/Anchor.ts @@ -3,7 +3,7 @@ import { concatBytes } from '@noble/hashes/utils'; import { base58 } from '@scure/base'; import * as convert from '../utils/convert'; import { keyTypeId } from '../utils/crypto'; -import { ITxJSON } from '../../interfaces'; +import { ITxJSON } from '../types'; import Binary from '../Binary'; const BASE_FEE = 25000000; diff --git a/src/transactions/Association.ts b/src/transactions/Association.ts index 0f29ec5..eba1deb 100644 --- a/src/transactions/Association.ts +++ b/src/transactions/Association.ts @@ -3,7 +3,7 @@ import { concatBytes } from '@noble/hashes/utils'; import { base58 } from '@scure/base'; import * as convert from '../utils/convert'; import { keyTypeId } from '../utils/crypto'; -import { ISigner, ITxJSON } from '../../interfaces'; +import { ISigner, ITxJSON } from '../types'; import Binary from '../Binary'; import { default as DataEntry, dictToData } from './DataEntry'; diff --git a/src/transactions/Burn.ts b/src/transactions/Burn.ts index 8a61e4f..a793e32 100644 --- a/src/transactions/Burn.ts +++ b/src/transactions/Burn.ts @@ -3,7 +3,7 @@ import { concatBytes } from '@noble/hashes/utils'; import { base58 } from '@scure/base'; import * as convert from '../utils/convert'; import { keyTypeId } from '../utils/crypto'; -import { ITxJSON } from '../../interfaces'; +import { ITxJSON } from '../types'; const DEFAULT_FEE = 100000000; const DEFAULT_VERSION = 3; diff --git a/src/transactions/CancelLease.ts b/src/transactions/CancelLease.ts index 0750e80..aa464c1 100644 --- a/src/transactions/CancelLease.ts +++ b/src/transactions/CancelLease.ts @@ -3,7 +3,7 @@ import { concatBytes } from '@noble/hashes/utils'; import { base58 } from '@scure/base'; import * as convert from '../utils/convert'; import { keyTypeId } from '../utils/crypto'; -import { ITxJSON } from '../../interfaces'; +import { ITxJSON } from '../types'; const DEFAULT_FEE = 100000000; const DEFAULT_VERSION = 3; diff --git a/src/transactions/CancelSponsorship.ts b/src/transactions/CancelSponsorship.ts index e53aa37..5ee8e87 100644 --- a/src/transactions/CancelSponsorship.ts +++ b/src/transactions/CancelSponsorship.ts @@ -3,7 +3,7 @@ import { concatBytes } from '@noble/hashes/utils'; import { base58 } from '@scure/base'; import * as convert from '../utils/convert'; import { keyTypeId } from '../utils/crypto'; -import { ISigner, ITxJSON } from '../../interfaces'; +import { ISigner, ITxJSON } from '../types'; const DEFAULT_FEE = 500000000; const DEFAULT_VERSION = 3; diff --git a/src/transactions/Data.ts b/src/transactions/Data.ts index 1d649fe..ed1a159 100644 --- a/src/transactions/Data.ts +++ b/src/transactions/Data.ts @@ -3,7 +3,7 @@ import { concatBytes } from '@noble/hashes/utils'; import { base58 } from '@scure/base'; import * as convert from '../utils/convert'; import { keyTypeId } from '../utils/crypto'; -import { ITxJSON } from '../../interfaces'; +import { ITxJSON } from '../types'; import { default as DataEntry, dictToData } from './DataEntry'; import Binary from '../Binary'; diff --git a/src/transactions/Lease.ts b/src/transactions/Lease.ts index ecd113e..b5fa90d 100644 --- a/src/transactions/Lease.ts +++ b/src/transactions/Lease.ts @@ -3,7 +3,7 @@ import { concatBytes } from '@noble/hashes/utils'; import { base58 } from '@scure/base'; import * as convert from '../utils/convert'; import { keyTypeId } from '../utils/crypto'; -import { ISigner, ITxJSON } from '../../interfaces'; +import { ISigner, ITxJSON } from '../types'; const DEFAULT_FEE = 100000000; const DEFAULT_VERSION = 3; diff --git a/src/transactions/MappedAnchor.ts b/src/transactions/MappedAnchor.ts index 7c14675..4cdbf96 100644 --- a/src/transactions/MappedAnchor.ts +++ b/src/transactions/MappedAnchor.ts @@ -3,7 +3,7 @@ import { concatBytes } from '@noble/hashes/utils'; import { base58 } from '@scure/base'; import * as convert from '../utils/convert'; import { keyTypeId } from '../utils/crypto'; -import { IBinary, IPair, ITxJSON } from '../../interfaces'; +import { IBinary, IPair, ITxJSON } from '../types'; import Binary from '../Binary'; const BASE_FEE = 25000000; diff --git a/src/transactions/MassTransfer.ts b/src/transactions/MassTransfer.ts index 8ce4cfc..2daacdb 100644 --- a/src/transactions/MassTransfer.ts +++ b/src/transactions/MassTransfer.ts @@ -3,7 +3,7 @@ import { concatBytes } from '@noble/hashes/utils'; import { base58 } from '@scure/base'; import * as convert from '../utils/convert'; import { keyTypeId } from '../utils/crypto'; -import { ITransfer, ITxJSON } from '../../interfaces'; +import { ITransfer, ITxJSON } from '../types'; import Binary from '../Binary'; const BASE_FEE = 100000000; diff --git a/src/transactions/Register.ts b/src/transactions/Register.ts index d00c87f..6bdb572 100644 --- a/src/transactions/Register.ts +++ b/src/transactions/Register.ts @@ -3,7 +3,7 @@ import { concatBytes } from '@noble/hashes/utils'; import { base58 } from '@scure/base'; import * as convert from '../utils/convert'; import { keyTypeId } from '../utils/crypto'; -import { IPublicAccount, ISigner, ITxJSON } from '../../interfaces'; +import { IPublicAccount, ISigner, ITxJSON } from '../types'; const BASE_FEE = 25000000; const VAR_FEE = 10000000; diff --git a/src/transactions/RevokeAssociation.ts b/src/transactions/RevokeAssociation.ts index b4195b9..8ea697c 100644 --- a/src/transactions/RevokeAssociation.ts +++ b/src/transactions/RevokeAssociation.ts @@ -3,7 +3,7 @@ import { concatBytes } from '@noble/hashes/utils'; import { base58 } from '@scure/base'; import * as convert from '../utils/convert'; import { keyTypeId } from '../utils/crypto'; -import { ISigner, ITxJSON } from '../../interfaces'; +import { ISigner, ITxJSON } from '../types'; import Binary from '../Binary'; const DEFAULT_FEE = 50000000; diff --git a/src/transactions/Sponsorship.ts b/src/transactions/Sponsorship.ts index c25cf0c..b401a2a 100644 --- a/src/transactions/Sponsorship.ts +++ b/src/transactions/Sponsorship.ts @@ -3,7 +3,7 @@ import { concatBytes } from '@noble/hashes/utils'; import { base58 } from '@scure/base'; import * as convert from '../utils/convert'; import { keyTypeId } from '../utils/crypto'; -import { ISigner, ITxJSON } from '../../interfaces'; +import { ISigner, ITxJSON } from '../types'; const DEFAULT_FEE = 500000000; const DEFAULT_VERSION = 3; diff --git a/src/transactions/Statement.ts b/src/transactions/Statement.ts index 56dd99c..7e1ce1b 100644 --- a/src/transactions/Statement.ts +++ b/src/transactions/Statement.ts @@ -3,7 +3,7 @@ import { concatBytes } from '@noble/hashes/utils'; import { base58 } from '@scure/base'; import * as convert from '../utils/convert'; import { keyTypeId } from '../utils/crypto'; -import { ISigner, ITxJSON } from '../../interfaces'; +import { ISigner, ITxJSON } from '../types'; import Binary from '../Binary'; import { default as DataEntry, dictToData } from './DataEntry'; diff --git a/src/transactions/Transaction.ts b/src/transactions/Transaction.ts index 99bbaf7..99402d8 100644 --- a/src/transactions/Transaction.ts +++ b/src/transactions/Transaction.ts @@ -1,6 +1,6 @@ import { base58 } from '@scure/base'; import { PublicNode } from '../node/'; -import { ISigner, ITxJSON, TKeyType } from '../../interfaces'; +import { ISigner, ITxJSON, TKeyType } from '../types'; import { getNetwork } from '../utils'; export default abstract class Transaction { diff --git a/src/transactions/Transfer.ts b/src/transactions/Transfer.ts index 39cf4cf..3d02edb 100644 --- a/src/transactions/Transfer.ts +++ b/src/transactions/Transfer.ts @@ -3,7 +3,7 @@ import { concatBytes } from '@noble/hashes/utils'; import { base58 } from '@scure/base'; import * as convert from '../utils/convert'; import { keyTypeId } from '../utils/crypto'; -import { ISigner, ITxJSON } from '../../interfaces'; +import { ISigner, ITxJSON } from '../types'; import Binary from '../Binary'; const DEFAULT_FEE = 100000000; diff --git a/src/transactions/index.ts b/src/transactions/index.ts index 67a7118..b286d33 100644 --- a/src/transactions/index.ts +++ b/src/transactions/index.ts @@ -13,7 +13,7 @@ import Transfer from './Transfer'; import Burn from './Burn'; import MappedAnchor from './MappedAnchor'; import Statement from './Statement'; -import { ITxJSON } from '../../interfaces'; +import { ITxJSON } from '../types'; export { Transaction, diff --git a/src/types/accounts.d.ts b/src/types/accounts.d.ts new file mode 100644 index 0000000..75e740b --- /dev/null +++ b/src/types/accounts.d.ts @@ -0,0 +1,34 @@ +import { IBinary } from './binary'; + +export type TKeyType = 'ed25519' | 'secp256k1' | 'secp256r1'; + +export interface IAccountIn { + address?: string; + publicKey?: string | Uint8Array; + privateKey?: string | Uint8Array; + keyType?: string; + seed?: string; + seedPassword?: string; + nonce?: number | string | Uint8Array; + derivationPath?: string; + parent?: { + seed: string; + keyType?: string; + }; +} + +export interface ISigner { + sign(message: string | Uint8Array): IBinary; + address: string; + publicKey: string; + keyType: TKeyType; +} + +export interface ISignable { + signWith(account: ISigner): this; +} + +export interface IPublicAccount { + keyType: TKeyType; + publicKey: string; +} diff --git a/src/types/binary.d.ts b/src/types/binary.d.ts new file mode 100644 index 0000000..64e0dee --- /dev/null +++ b/src/types/binary.d.ts @@ -0,0 +1,17 @@ +export type TBinary = Uint8Array | number[]; + +export interface IBinary extends Uint8Array { + readonly base58: string; + readonly base64: string; + readonly hex: string; + toString(): string; + hash(): IBinary; + slice(start?: number, end?: number): IBinary; + reverse(): IBinary; + toReversed(): IBinary; +} + +export interface IKeyPairBytes { + privateKey?: IBinary; + publicKey: IBinary; +} diff --git a/src/types/events.d.ts b/src/types/events.d.ts new file mode 100644 index 0000000..e584782 --- /dev/null +++ b/src/types/events.d.ts @@ -0,0 +1,17 @@ +import { IPublicAccount } from './accounts'; + +export interface IEventChainJSON extends Record { + id: string; + events: Array; +} + +export interface IEventJSON { + timestamp?: number; + previous?: string; + signKey?: IPublicAccount; + signature?: string; + hash?: string; + mediaType: string; + data: string; + attachments?: Array<{ name: string; mediaType: string; data: string }>; +} diff --git a/src/types/identities.d.ts b/src/types/identities.d.ts new file mode 100644 index 0000000..80a64ae --- /dev/null +++ b/src/types/identities.d.ts @@ -0,0 +1,14 @@ +export interface IDIDService { + id?: string; + type: string; + serviceEndpoint: string | Record | Array>; + description?: string; + [key: string]: any; +} + +export type TDIDRelationship = + | 'authentication' + | 'assertionMethod' + | 'keyAgreement' + | 'capabilityInvocation' + | 'capabilityDelegation'; diff --git a/src/types/index.d.ts b/src/types/index.d.ts new file mode 100644 index 0000000..a49c36b --- /dev/null +++ b/src/types/index.d.ts @@ -0,0 +1,27 @@ +export * from './accounts'; +export * from './binary'; +export * from './events'; +export * from './identities'; +export * from './messages'; +export * from './transactions'; + +// Missing interfaces +declare global { + interface Window { + msCrypto?: any; + Response?: any; + Promise: PromiseConstructor; + } + + interface ErrorConstructor { + captureStackTrace(thisArg: any, func: any): void; + } +} + +// Replacement for --allowJs +declare module '*.js' { + const content: { + [key: string]: any; + }; + export = content; +} diff --git a/src/types/messages.d.ts b/src/types/messages.d.ts new file mode 100644 index 0000000..45336b8 --- /dev/null +++ b/src/types/messages.d.ts @@ -0,0 +1,21 @@ +import { IPublicAccount } from './accounts'; + +interface IMessageJSONBase { + type: string; + sender: IPublicAccount; + recipient: string; + timestamp: Date | string; + signature?: string; + hash?: string; +} + +interface IMessageJSONEncrypted extends IMessageJSONBase { + encryptedData: string; +} + +interface IMessageJSONUnencrypted extends IMessageJSONBase { + mediaType: string; + data: string; +} + +export type IMessageJSON = IMessageJSONEncrypted | IMessageJSONUnencrypted; diff --git a/src/types/transactions.d.ts b/src/types/transactions.d.ts new file mode 100644 index 0000000..68f2816 --- /dev/null +++ b/src/types/transactions.d.ts @@ -0,0 +1,13 @@ +export type IPair = { + key: T; + value: T; +}; + +export interface ITxJSON extends Record { + type: number; +} + +export interface ITransfer { + recipient: string; + amount: number; +} diff --git a/src/utils/base64.ts b/src/utils/base64.ts index 3be7e5d..a74396c 100644 --- a/src/utils/base64.ts +++ b/src/utils/base64.ts @@ -1,4 +1,4 @@ -import { TBinary } from '../../interfaces'; +import { TBinary } from '../types'; // Using native functions is faster than using the @scure/base diff --git a/src/utils/convert.ts b/src/utils/convert.ts index ef90115..29db500 100644 --- a/src/utils/convert.ts +++ b/src/utils/convert.ts @@ -1,4 +1,4 @@ -import { TBinary } from '../../interfaces'; +import { TBinary } from '../types'; import { int16ToBytes, int32ToBytes } from './bytes'; export function booleanToBytes(input: boolean): Uint8Array { diff --git a/src/utils/crypto.ts b/src/utils/crypto.ts index b16b9f5..2f1a66e 100644 --- a/src/utils/crypto.ts +++ b/src/utils/crypto.ts @@ -4,7 +4,7 @@ import { concatBytes } from '@noble/hashes/utils'; import * as constants from '../constants'; import { sha256 } from '@noble/hashes/sha256'; import { blake2b } from '@noble/hashes/blake2b'; -import { TKeyType } from '../../interfaces'; +import { TKeyType } from '../types'; const ADDRESS_VERSION = 1; diff --git a/test/events/Event.spec.ts b/test/events/Event.spec.ts index a5ebd29..ea74ea4 100644 --- a/test/events/Event.spec.ts +++ b/test/events/Event.spec.ts @@ -3,7 +3,7 @@ import { EventChain, Event } from '../../src/events'; import { AccountFactoryED25519 } from '../../src/accounts'; import Binary from '../../src/Binary'; import * as sinon from 'sinon'; -import { IEventJSON } from '../../interfaces'; +import { IEventJSON } from '../../src/types'; import { EVENT_CHAIN_V1 } from '../../src/events/EventChain'; describe('Event', () => { diff --git a/test/events/EventChain.spec.ts b/test/events/EventChain.spec.ts index 8797338..d5a9c8c 100644 --- a/test/events/EventChain.spec.ts +++ b/test/events/EventChain.spec.ts @@ -2,7 +2,7 @@ import { expect } from 'chai'; import { EventChain, Event } from '../../src/events'; import { AccountFactoryED25519 } from '../../src/accounts'; import Binary from '../../src/Binary'; -import { IEventChainJSON } from '../../interfaces'; +import { IEventChainJSON } from '../../src/types'; import { EVENT_CHAIN_V1, EVENT_CHAIN_V2 } from '../../src/events/EventChain'; describe('EventChain', () => { diff --git a/test/message/Message.spec.ts b/test/message/Message.spec.ts index e3432fd..bd06ee6 100644 --- a/test/message/Message.spec.ts +++ b/test/message/Message.spec.ts @@ -4,7 +4,7 @@ import { expect } from 'chai'; import { Message } from '../../src/messages'; import { AccountFactoryED25519 as AccountFactory } from '../../src/accounts'; import Binary from '../../src/Binary'; -import { IMessageJSON } from '../../interfaces'; +import { IMessageJSON } from '../../src/types'; describe('Message', () => { const seed = 'satisfy sustain shiver skill betray mother appear pupil coconut weasel firm top puzzle monkey seek'; diff --git a/test/node/PublicNode.spec.ts b/test/node/PublicNode.spec.ts index 53dec3d..80607cb 100644 --- a/test/node/PublicNode.spec.ts +++ b/test/node/PublicNode.spec.ts @@ -3,7 +3,7 @@ import * as sinon from 'sinon'; import { PublicNode } from '../../src/node'; import { RequestError } from '../../src/errors'; -import { ITxJSON } from '../../interfaces'; +import { ITxJSON } from '../../src/types'; import { Anchor } from '../../src/transactions'; describe('PublicNode', () => { diff --git a/tsconfig.json b/tsconfig.json index cd04c71..2f3d3eb 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -13,5 +13,6 @@ "esModuleInterop": true, "moduleResolution": "node16" }, - "include": ["src"] + "include": ["src"], + "exclude": ["src/bundle.ts"] } From ff4053132c383cb4ef7fb321223e64ee84164895 Mon Sep 17 00:00:00 2001 From: arnold Date: Fri, 21 Jul 2023 10:41:24 -0400 Subject: [PATCH 7/7] Create `bundle.ts` so the bundles version still contains all classes. --- src/bundle.ts | 16 ++++++++++++++++ webpack.config.js | 2 +- 2 files changed, 17 insertions(+), 1 deletion(-) create mode 100644 src/bundle.ts diff --git a/src/bundle.ts b/src/bundle.ts new file mode 100644 index 0000000..9d3b0ca --- /dev/null +++ b/src/bundle.ts @@ -0,0 +1,16 @@ +import LTO from './LTO'; +export { LTO }; +export { default as Binary } from './Binary'; + +export * from './accounts'; +export * from './errors'; +export * from './events'; +export * from './identities'; +export * from './messages'; +export * from './node'; +export * from './transactions'; +export * from './utils'; + +export function connect(networkId?: string): LTO { + return new LTO(networkId); +} diff --git a/webpack.config.js b/webpack.config.js index dc7d5e0..3106472 100644 --- a/webpack.config.js +++ b/webpack.config.js @@ -5,7 +5,7 @@ module.exports = { experiments: { outputModule: false, }, - entry: './src/index.ts', + entry: './src/bundle.ts', devtool: 'source-map', output: { path: path.resolve(__dirname, 'dist'),