From 98efa8f11b2a844fe2a2eb1512fe9bf7c6aeb613 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=B8ren=20Bruus=20Zeppelin?= Date: Thu, 3 Aug 2023 15:31:44 +0200 Subject: [PATCH 001/179] Compile web files to ES modules + change browser entrypoint to modules --- package.json | 3 +- packages/common/package.json | 6 +++- packages/nodejs/package.json | 4 ++- packages/nodejs/tsconfig.json | 11 ++++-- packages/rust-bindings/package.json | 6 +++- packages/web/package.json | 8 +++-- packages/web/tsconfig.json | 7 ++-- packages/web/webpack.config.js | 16 +++++---- tsconfig-base.json | 8 +++-- tsconfig.json | 14 +++++--- yarn.lock | 55 +++++++++++++++++++++++++++++ 11 files changed, 114 insertions(+), 24 deletions(-) diff --git a/package.json b/package.json index 0cad16afa..c91e1f3f0 100644 --- a/package.json +++ b/package.json @@ -70,6 +70,7 @@ "markdown:lint-fix": "yarn markdownlint --fix pages/**/*.md packages/*/README.md README.md", "markdown:linkcheck": "yarn markdown-link-check --config .markdown-linkcheck.json pages/**/*.md packages/*/README.md README.md", "build": "FORCE_COLOR=true yarn workspaces foreach -v -t --no-private run build", - "build:dev": "FORCE_COLOR=true yarn workspaces foreach -v -t --no-private run build-dev" + "build:dev": "FORCE_COLOR=true yarn workspaces foreach -v -t --no-private run build-dev", + "clean": "FORCE_COLOR=true yarn workspaces foreach -p --no-private run clean" } } diff --git a/packages/common/package.json b/packages/common/package.json index 65aa9c066..0660868de 100644 --- a/packages/common/package.json +++ b/packages/common/package.json @@ -10,6 +10,8 @@ "url": "https://github.com/Concordium/concordium-node-sdk-js", "directory": "packages/common" }, + "type": "module", + "sideEffects": false, "main": "lib/index.js", "types": "lib/index.d.ts", "files": [ @@ -34,6 +36,7 @@ "jest": "^27.0.6", "lint-staged": "^12.0.2", "prettier": "^2.3.2", + "rimraf": "^5.0.1", "ts-jest": "^27.0.3", "typescript": "^4.3.5" }, @@ -48,7 +51,8 @@ "lint-fix": "yarn --silent lint --fix; exit 0", "test": "jest", "build": "rm -rf grpc; mkdir -p grpc; yarn generate && tsc", - "build-dev": "tsc" + "build-dev": "tsc", + "clean": "rimraf -- lib grpc tsconfig.tsbuildinfo" }, "dependencies": { "@concordium/rust-bindings": "1.1.0", diff --git a/packages/nodejs/package.json b/packages/nodejs/package.json index d47381060..f5bd8d0f9 100644 --- a/packages/nodejs/package.json +++ b/packages/nodejs/package.json @@ -42,6 +42,7 @@ "jest": "^27.0.6", "lint-staged": "^12.0.2", "prettier": "^2.3.2", + "rimraf": "^5.0.1", "ts-jest": "^27.0.3", "typescript": "^4.3.5" }, @@ -57,7 +58,8 @@ "lint-fix": "yarn --silent lint --fix; exit 0", "test": "jest", "build": "rm -rf grpc; mkdir -p grpc; yarn generate && tsc", - "build-dev": "tsc" + "build-dev": "tsc", + "clean": "rimraf -- lib grpc tsconfig.tsbuildinfo" }, "dependencies": { "@concordium/common-sdk": "9.0.0", diff --git a/packages/nodejs/tsconfig.json b/packages/nodejs/tsconfig.json index b62f8da08..5ebb244c2 100644 --- a/packages/nodejs/tsconfig.json +++ b/packages/nodejs/tsconfig.json @@ -1,11 +1,16 @@ { "extends": "../../tsconfig-base.json", - "include": ["src/**/*"], + "include": [ + "src/**/*" + ], "references": [ - { "path": "../common"} + { + "path": "../common" + } ], "compilerOptions": { "rootDir": "src", - "outDir": "./lib" + "outDir": "./lib", + "module": "CommonJS" } } diff --git a/packages/rust-bindings/package.json b/packages/rust-bindings/package.json index c80539fab..8dd1e039b 100644 --- a/packages/rust-bindings/package.json +++ b/packages/rust-bindings/package.json @@ -21,6 +21,10 @@ "clippy": "cargo +1.62 clippy --color=always --tests --benches -- -Dclippy::all", "build": "wasm-pack build --target web --out-dir pkg/bundler \"$@\" && wasm-pack build --target nodejs --out-dir pkg/node \"$@\"", "build-dev": "yarn build --dev", - "build:rust-bindings": "yarn build" + "build:rust-bindings": "yarn build", + "clean": "rimraf -- target pkg" + }, + "devDependencies": { + "rimraf": "^5.0.1" } } diff --git a/packages/web/package.json b/packages/web/package.json index 1f7b468b3..20542c6bf 100644 --- a/packages/web/package.json +++ b/packages/web/package.json @@ -2,11 +2,13 @@ "name": "@concordium/web-sdk", "version": "6.0.0", "license": "Apache-2.0", - "browser": "lib/concordium.min.js", - "types": "lib/index.d.ts", + "browser": "lib/mjs/index.js", + "types": "lib/types/index.d.ts", "files": [ "lib/**/*" ], + "type": "module", + "sideEffects": false, "repository": { "type": "git", "url": "https://github.com/Concordium/concordium-node-sdk-js", @@ -41,6 +43,7 @@ "eslint-plugin-import": "^2.23.4", "eslint-plugin-prettier": "^3.4.0", "jest": "^27.0.6", + "rimraf": "^5.0.1", "stream-browserify": "^3.0.0", "ts-jest": "^27.0.3", "typescript": "^4.3.5", @@ -58,6 +61,7 @@ "scripts": { "build": "tsc && webpack", "build-dev": "yarn build", + "clean": "rimraf -- lib .webpack-cache tsconfig.tsbuildinfo", "lint": "eslint . --cache --ext .ts,.tsx --max-warnings 0", "lint-fix": "yarn --silent lint --fix; exit 0", "test": "jest" diff --git a/packages/web/tsconfig.json b/packages/web/tsconfig.json index 46193bdf7..62faa3571 100644 --- a/packages/web/tsconfig.json +++ b/packages/web/tsconfig.json @@ -2,11 +2,14 @@ "extends": "../../tsconfig-base.json", "include": ["src/**/*"], "references": [ - { "path": "../common"} + { + "path": "../common" + } ], "compilerOptions": { "lib": ["ES2020", "dom"], "rootDir": "./src", - "outDir": "./lib" + "outDir": "./lib/mjs", + "declarationDir": "./lib/types" } } diff --git a/packages/web/webpack.config.js b/packages/web/webpack.config.js index d7aaab9cf..3a06774a2 100644 --- a/packages/web/webpack.config.js +++ b/packages/web/webpack.config.js @@ -1,14 +1,18 @@ -const path = require('path'); -const webpack = require('webpack'); +import path from 'path'; +import webpack from 'webpack'; +import { fileURLToPath } from 'url'; -module.exports = { +const __filename = fileURLToPath(import.meta.url); +const __dirname = path.dirname(__filename); + +export default { mode: 'production', cache: { type: 'filesystem', cacheDirectory: path.resolve(__dirname, '.webpack-cache'), }, entry: { - concordium: './lib/index.js', + concordium: './lib/mjs/index.js', }, plugins: [ new webpack.SourceMapDevToolPlugin({ @@ -20,7 +24,7 @@ module.exports = { ], resolve: { fallback: { - stream: require.resolve('stream-browserify'), + stream: 'stream-browserify', }, }, module: { @@ -60,7 +64,7 @@ module.exports = { }, output: { filename: '[name].min.js', - path: path.resolve(__dirname, 'lib'), + path: path.resolve(__dirname, 'lib/umd'), library: 'concordiumSDK', libraryTarget: 'umd', publicPath: '', diff --git a/tsconfig-base.json b/tsconfig-base.json index 6c921dc22..8d70dbe89 100644 --- a/tsconfig-base.json +++ b/tsconfig-base.json @@ -1,12 +1,14 @@ { "compilerOptions": { "strict": true, - "lib": ["ES2020"], - "module": "commonjs", + "lib": [ + "ES2020" + ], + "module": "ES2020", "declaration": true, "moduleResolution": "Node", "target": "ES2020", "esModuleInterop": true, - "composite": true, + "composite": true } } diff --git a/tsconfig.json b/tsconfig.json index 208f2ae0d..ce3e00dd3 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -4,12 +4,18 @@ "resolveJsonModule": true, "composite": true, "strict": true, - "declaration": true, + "declaration": true }, "files": [], "references": [ - { "path": "./packages/common" }, - { "path": "./packages/nodejs" }, - { "path": "./packages/web" } + { + "path": "./packages/common" + }, + { + "path": "./packages/nodejs" + }, + { + "path": "./packages/web" + } ] } diff --git a/yarn.lock b/yarn.lock index 4965d23f2..53feee225 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1344,6 +1344,7 @@ __metadata: json-bigint: ^1.0.0 lint-staged: ^12.0.2 prettier: ^2.3.2 + rimraf: ^5.0.1 ts-jest: ^27.0.3 typescript: ^4.3.5 uuid: ^8.3.2 @@ -1400,6 +1401,7 @@ __metadata: jest: ^27.0.6 lint-staged: ^12.0.2 prettier: ^2.3.2 + rimraf: ^5.0.1 ts-jest: ^27.0.3 typescript: ^4.3.5 languageName: unknown @@ -1408,6 +1410,8 @@ __metadata: "@concordium/rust-bindings@1.1.0, @concordium/rust-bindings@workspace:packages/rust-bindings": version: 0.0.0-use.local resolution: "@concordium/rust-bindings@workspace:packages/rust-bindings" + dependencies: + rimraf: ^5.0.1 languageName: unknown linkType: soft @@ -1429,6 +1433,7 @@ __metadata: eslint-plugin-prettier: ^3.4.0 jest: ^27.0.6 process: ^0.11.10 + rimraf: ^5.0.1 stream-browserify: ^3.0.0 ts-jest: ^27.0.3 typescript: ^4.3.5 @@ -4988,6 +4993,21 @@ __metadata: languageName: node linkType: hard +"glob@npm:^10.2.5": + version: 10.3.3 + resolution: "glob@npm:10.3.3" + dependencies: + foreground-child: ^3.1.0 + jackspeak: ^2.0.3 + minimatch: ^9.0.1 + minipass: ^5.0.0 || ^6.0.2 || ^7.0.0 + path-scurry: ^1.10.1 + bin: + glob: dist/cjs/src/bin.js + checksum: 29190d3291f422da0cb40b77a72fc8d2c51a36524e99b8bf412548b7676a6627489528b57250429612b6eec2e6fe7826d328451d3e694a9d15e575389308ec53 + languageName: node + linkType: hard + "glob@npm:^7.1.1, glob@npm:^7.1.2, glob@npm:^7.1.3, glob@npm:^7.1.4, glob@npm:^7.2.0": version: 7.2.3 resolution: "glob@npm:7.2.3" @@ -6774,6 +6794,13 @@ __metadata: languageName: node linkType: hard +"lru-cache@npm:^9.1.1 || ^10.0.0": + version: 10.0.0 + resolution: "lru-cache@npm:10.0.0" + checksum: 18f101675fe283bc09cda0ef1e3cc83781aeb8373b439f086f758d1d91b28730950db785999cd060d3c825a8571c03073e8c14512b6655af2188d623031baf50 + languageName: node + linkType: hard + "lunr@npm:^2.3.9": version: 2.3.9 resolution: "lunr@npm:2.3.9" @@ -7159,6 +7186,13 @@ __metadata: languageName: node linkType: hard +"minipass@npm:^5.0.0 || ^6.0.2 || ^7.0.0": + version: 7.0.2 + resolution: "minipass@npm:7.0.2" + checksum: 46776de732eb7cef2c7404a15fb28c41f5c54a22be50d47b03c605bf21f5c18d61a173c0a20b49a97e7a65f78d887245066410642551e45fffe04e9ac9e325bc + languageName: node + linkType: hard + "minizlib@npm:^2.1.1, minizlib@npm:^2.1.2": version: 2.1.2 resolution: "minizlib@npm:2.1.2" @@ -7678,6 +7712,16 @@ __metadata: languageName: node linkType: hard +"path-scurry@npm:^1.10.1": + version: 1.10.1 + resolution: "path-scurry@npm:1.10.1" + dependencies: + lru-cache: ^9.1.1 || ^10.0.0 + minipass: ^5.0.0 || ^6.0.2 || ^7.0.0 + checksum: e2557cff3a8fb8bc07afdd6ab163a92587884f9969b05bbbaf6fe7379348bfb09af9ed292af12ed32398b15fb443e81692047b786d1eeb6d898a51eb17ed7d90 + languageName: node + linkType: hard + "path-scurry@npm:^1.7.0": version: 1.9.2 resolution: "path-scurry@npm:1.9.2" @@ -8186,6 +8230,17 @@ __metadata: languageName: node linkType: hard +"rimraf@npm:^5.0.1": + version: 5.0.1 + resolution: "rimraf@npm:5.0.1" + dependencies: + glob: ^10.2.5 + bin: + rimraf: dist/cjs/src/bin.js + checksum: bafce85391349a2d960847980bf9b5caa2a8887f481af630f1ea27e08288217293cec72d75e9a2ba35495c212789f66a7f3d23366ba6197026ab71c535126857 + languageName: node + linkType: hard + "ripemd160@npm:^2.0.1": version: 2.0.2 resolution: "ripemd160@npm:2.0.2" From 10ee405339a536a5573ebb79e6af74fe497ffd18 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=B8ren=20Bruus=20Zeppelin?= Date: Fri, 4 Aug 2023 08:40:55 +0200 Subject: [PATCH 002/179] Remove unneeded stream polyfill --- packages/common/src/accountTransactions.ts | 10 ++--- packages/common/src/deserialization.ts | 37 +++++++++---------- packages/common/src/deserializationHelpers.ts | 13 +++++-- packages/web/package.json | 1 - packages/web/webpack.config.js | 5 --- yarn.lock | 1 - 6 files changed, 32 insertions(+), 35 deletions(-) diff --git a/packages/common/src/accountTransactions.ts b/packages/common/src/accountTransactions.ts index 640d52db8..d82fdbf2d 100644 --- a/packages/common/src/accountTransactions.ts +++ b/packages/common/src/accountTransactions.ts @@ -27,13 +27,13 @@ import { import { AccountAddress } from './types/accountAddress'; import { DataBlob } from './types/DataBlob'; import { CcdAmount } from './types/ccdAmount'; -import { Readable } from 'stream'; +import { Cursor } from './deserializationHelpers'; interface AccountTransactionHandler< PayloadType extends AccountTransactionPayload = AccountTransactionPayload > { serialize: (payload: PayloadType) => Buffer; - deserialize: (serializedPayload: Readable) => PayloadType; + deserialize: (serializedPayload: Cursor) => PayloadType; getBaseEnergyCost: (payload: PayloadType) => bigint; } @@ -50,7 +50,7 @@ export class SimpleTransferHandler return Buffer.concat([serializedToAddress, serializedAmount]); } - deserialize(serializedPayload: Readable): SimpleTransferPayload { + deserialize(serializedPayload: Cursor): SimpleTransferPayload { const toAddress = AccountAddress.fromBytes( Buffer.from(serializedPayload.read(32)) ); @@ -79,7 +79,7 @@ export class SimpleTransferWithMemoHandler ]); } - deserialize(serializedPayload: Readable): SimpleTransferWithMemoPayload { + deserialize(serializedPayload: Cursor): SimpleTransferWithMemoPayload { const toAddress = AccountAddress.fromBytes( Buffer.from(serializedPayload.read(32)) ); @@ -250,7 +250,7 @@ export class RegisterDataHandler return encodeDataBlob(payload.data); } - deserialize(serializedPayload: Readable): RegisterDataPayload { + deserialize(serializedPayload: Cursor): RegisterDataPayload { const memoLength = serializedPayload.read(2).readUInt16BE(0); return { data: new DataBlob(Buffer.from(serializedPayload.read(memoLength))), diff --git a/packages/common/src/deserialization.ts b/packages/common/src/deserialization.ts index 369fe8e4b..aebf2000f 100644 --- a/packages/common/src/deserialization.ts +++ b/packages/common/src/deserialization.ts @@ -1,6 +1,7 @@ import * as wasm from '@concordium/rust-bindings'; import { Buffer } from 'buffer/'; import { getAccountTransactionHandler } from './accountTransactions'; +import { Cursor } from './deserializationHelpers'; import { AccountTransaction, AccountTransactionHeader, @@ -12,15 +13,14 @@ import { } from './types'; import { AccountAddress } from './types/accountAddress'; import { TransactionExpiry } from './types/transactionExpiry'; -import { PassThrough, Readable } from 'stream'; /** - * Reads an unsigned 8-bit integer from the given {@link Readable}. + * Reads an unsigned 8-bit integer from the given {@link Cursor}. * * @param source input stream * @returns number from 0 to 255 */ -export function deserializeUint8(source: Readable): number { +export function deserializeUint8(source: Cursor): number { return source.read(1).readUInt8(0); } @@ -51,10 +51,10 @@ export function deserializeContractState( } function deserializeMap( - serialized: Readable, - decodeSize: (size: Readable) => number, - decodeKey: (k: Readable) => K, - decodeValue: (t: Readable) => T + serialized: Cursor, + decodeSize: (size: Cursor) => number, + decodeKey: (k: Cursor) => K, + decodeValue: (t: Cursor) => T ): Record { const size = decodeSize(serialized); const result = {} as Record; @@ -67,13 +67,13 @@ function deserializeMap( } function deserializeAccountTransactionSignature( - signatures: Readable + signatures: Cursor ): AccountTransactionSignature { - const decodeSignature = (serialized: Readable) => { + const decodeSignature = (serialized: Cursor) => { const length = serialized.read(2).readUInt16BE(0); return serialized.read(length).toString('hex'); }; - const decodeCredentialSignatures = (serialized: Readable) => + const decodeCredentialSignatures = (serialized: Cursor) => deserializeMap( serialized, deserializeUint8, @@ -89,7 +89,7 @@ function deserializeAccountTransactionSignature( } function deserializeTransactionHeader( - serializedHeader: Readable + serializedHeader: Cursor ): AccountTransactionHeader { const sender = AccountAddress.fromBytes( Buffer.from(serializedHeader.read(32)) @@ -111,7 +111,7 @@ function deserializeTransactionHeader( }; } -function deserializeAccountTransaction(serializedTransaction: Readable): { +function deserializeAccountTransaction(serializedTransaction: Cursor): { accountTransaction: AccountTransaction; signatures: AccountTransactionSignature; } { @@ -143,7 +143,7 @@ function deserializeAccountTransaction(serializedTransaction: Readable): { }; } -function deserializeCredentialDeployment(serializedDeployment: Readable) { +function deserializeCredentialDeployment(serializedDeployment: Cursor) { const raw = wasm.deserializeCredentialDeployment( serializedDeployment.read().toString('hex') ); @@ -183,10 +183,9 @@ export type BlockItem = export function deserializeTransaction( serializedTransaction: Buffer ): BlockItem { - const bufferStream = new PassThrough(); - bufferStream.end(serializedTransaction); + const cursor = new Cursor(serializedTransaction); - const version = deserializeUint8(bufferStream); + const version = deserializeUint8(cursor); if (version !== 0) { throw new Error( 'Supplied version ' + @@ -194,17 +193,17 @@ export function deserializeTransaction( ' is not valid. Only transactions with version 0 format are supported' ); } - const blockItemKind = deserializeUint8(bufferStream); + const blockItemKind = deserializeUint8(cursor); switch (blockItemKind) { case BlockItemKind.AccountTransactionKind: return { kind: BlockItemKind.AccountTransactionKind, - transaction: deserializeAccountTransaction(bufferStream), + transaction: deserializeAccountTransaction(cursor), }; case BlockItemKind.CredentialDeploymentKind: return { kind: BlockItemKind.CredentialDeploymentKind, - transaction: deserializeCredentialDeployment(bufferStream), + transaction: deserializeCredentialDeployment(cursor), }; case BlockItemKind.UpdateInstructionKind: throw new Error( diff --git a/packages/common/src/deserializationHelpers.ts b/packages/common/src/deserializationHelpers.ts index 336227f29..6bc9e27dc 100644 --- a/packages/common/src/deserializationHelpers.ts +++ b/packages/common/src/deserializationHelpers.ts @@ -26,12 +26,17 @@ export class Cursor { return new Cursor(Buffer.from(data, 'hex')); } - /** Read a number of bytes from the cursor */ - public read(numBytes: number): Buffer { + /** + * Read a number of bytes from the cursor + * + * @param {number} [numBytes] - Number of bytes to read. If not defined, the remaining data is read. + */ + public read(numBytes?: number): Buffer { + const n = numBytes ?? this.remainingBytes.length; const data = Buffer.from( - this.data.subarray(this.cursor, this.cursor + numBytes) + this.data.subarray(this.cursor, this.cursor + n) ); - this.cursor += numBytes; + this.cursor += n; return data; } diff --git a/packages/web/package.json b/packages/web/package.json index 20542c6bf..473cc503a 100644 --- a/packages/web/package.json +++ b/packages/web/package.json @@ -44,7 +44,6 @@ "eslint-plugin-prettier": "^3.4.0", "jest": "^27.0.6", "rimraf": "^5.0.1", - "stream-browserify": "^3.0.0", "ts-jest": "^27.0.3", "typescript": "^4.3.5", "webpack": "^5.72.0", diff --git a/packages/web/webpack.config.js b/packages/web/webpack.config.js index 3a06774a2..c78263c4c 100644 --- a/packages/web/webpack.config.js +++ b/packages/web/webpack.config.js @@ -22,11 +22,6 @@ export default { process: 'process/browser', }), ], - resolve: { - fallback: { - stream: 'stream-browserify', - }, - }, module: { rules: [ { diff --git a/yarn.lock b/yarn.lock index 53feee225..68363abf3 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1434,7 +1434,6 @@ __metadata: jest: ^27.0.6 process: ^0.11.10 rimraf: ^5.0.1 - stream-browserify: ^3.0.0 ts-jest: ^27.0.3 typescript: ^4.3.5 webpack: ^5.72.0 From 4e9a695f69fce31a1815645fc003301e4bd6d03c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=B8ren=20Bruus=20Zeppelin?= Date: Fri, 4 Aug 2023 10:09:22 +0200 Subject: [PATCH 003/179] Get rid of "import * as" statements where applicable --- packages/common/src/alias.ts | 4 ++-- packages/common/src/cis4/util.ts | 4 ++-- .../common/src/credentialDeploymentTransactions.ts | 10 ++++------ packages/common/src/hash.ts | 4 ++-- packages/common/src/signHelpers.ts | 6 +++--- 5 files changed, 13 insertions(+), 15 deletions(-) diff --git a/packages/common/src/alias.ts b/packages/common/src/alias.ts index d504d90d0..0913a0ad6 100644 --- a/packages/common/src/alias.ts +++ b/packages/common/src/alias.ts @@ -1,4 +1,4 @@ -import * as bs58check from 'bs58check'; +import { encode as bs58Encode } from 'bs58check'; import { Buffer } from 'buffer/'; import { AccountAddress } from './types/accountAddress'; @@ -53,5 +53,5 @@ export function getAlias( commonBytes, aliasBytes, ]); - return new AccountAddress(bs58check.encode(prefixedWithVersion)); + return new AccountAddress(bs58Encode(prefixedWithVersion)); } diff --git a/packages/common/src/cis4/util.ts b/packages/common/src/cis4/util.ts index a8bc93179..304727a95 100644 --- a/packages/common/src/cis4/util.ts +++ b/packages/common/src/cis4/util.ts @@ -1,5 +1,5 @@ import { Buffer } from 'buffer/'; -import * as ed25519 from '@noble/ed25519'; +import { getPublicKey } from '@noble/ed25519'; import type { ContractAddress, HexString } from '../types'; import type { CIS2 } from '../cis2'; @@ -281,7 +281,7 @@ export class Web3IdSigner { */ public static async from(privateKey: HexString): Promise { const publicKey = Buffer.from( - await ed25519.getPublicKey(Buffer.from(privateKey, 'hex')) + await getPublicKey(Buffer.from(privateKey, 'hex')) ).toString('hex'); return new Web3IdSigner(privateKey, publicKey); } diff --git a/packages/common/src/credentialDeploymentTransactions.ts b/packages/common/src/credentialDeploymentTransactions.ts index af07e0ace..a36c16b12 100644 --- a/packages/common/src/credentialDeploymentTransactions.ts +++ b/packages/common/src/credentialDeploymentTransactions.ts @@ -18,13 +18,13 @@ import * as wasm from '@concordium/rust-bindings'; import { TransactionExpiry } from './types/transactionExpiry'; import { AccountAddress } from './types/accountAddress'; import { sha256 } from './hash'; -import * as bs58check from 'bs58check'; +import { encode as bs58Encode } from 'bs58check'; import { Buffer } from 'buffer/'; import { ConcordiumHdWallet } from './HdWallet'; import { AttributesKeys, CredentialDeploymentDetails, HexString } from '.'; import { filterRecord, mapRecord } from './util'; import { getCredentialDeploymentSignDigest } from './serialization'; -import * as ed from '@noble/ed25519'; +import { sign } from '@noble/ed25519'; /** * Generates the unsigned credential information that has to be signed when @@ -185,9 +185,7 @@ export function buildSignedCredentialForExistingAccount( export function getAccountAddress(credId: string): AccountAddress { const hashedCredId = sha256([Buffer.from(credId, 'hex')]); const prefixedWithVersion = Buffer.concat([Buffer.of(1), hashedCredId]); - const accountAddress = new AccountAddress( - bs58check.encode(prefixedWithVersion) - ); + const accountAddress = new AccountAddress(bs58Encode(prefixedWithVersion)); return accountAddress; } @@ -326,5 +324,5 @@ export async function signCredentialTransaction( signingKey: HexString ): Promise { const digest = getCredentialDeploymentSignDigest(credDeployment); - return Buffer.from(await ed.sign(digest, signingKey)).toString('hex'); + return Buffer.from(await sign(digest, signingKey)).toString('hex'); } diff --git a/packages/common/src/hash.ts b/packages/common/src/hash.ts index 042243770..27f4b034b 100644 --- a/packages/common/src/hash.ts +++ b/packages/common/src/hash.ts @@ -1,8 +1,8 @@ import { Buffer } from 'buffer/'; -import * as hash from 'hash.js'; +import { sha256 as libSha256 } from 'hash.js'; export function sha256(data: (Buffer | Uint8Array)[]): Buffer { - const sha256Hash = hash.sha256(); + const sha256Hash = libSha256(); data.forEach((input) => sha256Hash.update(input)); return Buffer.from(sha256Hash.digest('hex'), 'hex'); } diff --git a/packages/common/src/signHelpers.ts b/packages/common/src/signHelpers.ts index bb20faa0f..42caf69db 100644 --- a/packages/common/src/signHelpers.ts +++ b/packages/common/src/signHelpers.ts @@ -9,7 +9,7 @@ import { WalletExportFormat, WithAccountKeys, } from './types'; -import * as ed from '@noble/ed25519'; +import { sign, verify } from '@noble/ed25519'; import { Buffer } from 'buffer/'; import { AccountAddress } from './types/accountAddress'; import { sha256 } from './hash'; @@ -43,7 +43,7 @@ export interface AccountSigner { export const getSignature = async ( digest: Buffer, privateKey: HexString -): Promise => Buffer.from(await ed.sign(digest, privateKey)); +): Promise => Buffer.from(await sign(digest, privateKey)); /** * Creates an `AccountSigner` for an account which uses the first credential's first keypair. @@ -268,7 +268,7 @@ export async function verifyMessageSignature( ); } if ( - !(await ed.verify( + !(await verify( credentialSignature[Number(keyIndex)], digest, credentialKeys.keys[Number(keyIndex)].verifyKey From e879cefeaff2b0a58b0186297be748f21805cdcf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=B8ren=20Bruus=20Zeppelin?= Date: Fri, 4 Aug 2023 10:44:39 +0200 Subject: [PATCH 004/179] Undo sideEffects declaration for now --- packages/common/package.json | 1 - packages/web/package.json | 1 - 2 files changed, 2 deletions(-) diff --git a/packages/common/package.json b/packages/common/package.json index 0660868de..08f26e3ff 100644 --- a/packages/common/package.json +++ b/packages/common/package.json @@ -11,7 +11,6 @@ "directory": "packages/common" }, "type": "module", - "sideEffects": false, "main": "lib/index.js", "types": "lib/index.d.ts", "files": [ diff --git a/packages/web/package.json b/packages/web/package.json index 473cc503a..97b45156a 100644 --- a/packages/web/package.json +++ b/packages/web/package.json @@ -8,7 +8,6 @@ "lib/**/*" ], "type": "module", - "sideEffects": false, "repository": { "type": "git", "url": "https://github.com/Concordium/concordium-node-sdk-js", From c671b91b2bd392bb1eab4e3327a8af2790d82383 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=B8ren=20Bruus=20Zeppelin?= Date: Fri, 4 Aug 2023 13:12:31 +0200 Subject: [PATCH 005/179] Restructure code into scoped sections --- .gitignore | 1 + packages/common/package.json | 9 + packages/common/src/GenericContract.ts | 2 +- packages/common/src/accountHelpers.ts | 19 -- packages/common/src/cis0.ts | 2 +- packages/common/src/cis2/CIS2Contract.ts | 2 +- packages/common/src/cis4/CIS4Contract.ts | 3 +- packages/common/src/cis4/util.ts | 2 +- .../src/credentialDeploymentTransactions.ts | 2 +- packages/common/src/deserialization.ts | 222 +---------------- packages/common/src/{ => grpc}/GRPCClient.ts | 33 ++- packages/common/src/grpc/index.ts | 1 + .../translation.ts} | 15 +- .../common/src/{ => identity}/idProofTypes.ts | 4 +- .../common/src/{ => identity}/idProofs.ts | 2 +- .../common/src/{ => identity}/identity.ts | 2 +- packages/common/src/identity/index.ts | 3 + packages/common/src/index.ts | 27 +- .../src/{ => json-rpc}/JsonRpcClient.ts | 14 +- packages/common/src/json-rpc/index.ts | 2 + .../{ => json-rpc}/providers/httpProvider.ts | 0 .../src/{ => json-rpc}/providers/provider.ts | 2 +- packages/common/src/serialization.ts | 136 ----------- packages/common/src/{ => wasm}/HdWallet.ts | 4 +- packages/common/src/wasm/accountHelpers.ts | 19 ++ packages/common/src/wasm/deserialization.ts | 230 ++++++++++++++++++ packages/common/src/wasm/index.ts | 18 ++ .../common/src/{ => wasm}/schemaHelpers.ts | 2 +- packages/common/src/wasm/serialization.ts | 142 +++++++++++ 29 files changed, 473 insertions(+), 447 deletions(-) rename packages/common/src/{ => grpc}/GRPCClient.ts (98%) create mode 100644 packages/common/src/grpc/index.ts rename packages/common/src/{GRPCTypeTranslation.ts => grpc/translation.ts} (99%) rename packages/common/src/{ => identity}/idProofTypes.ts (98%) rename packages/common/src/{ => identity}/idProofs.ts (99%) rename packages/common/src/{ => identity}/identity.ts (98%) create mode 100644 packages/common/src/identity/index.ts rename packages/common/src/{ => json-rpc}/JsonRpcClient.ts (97%) create mode 100644 packages/common/src/json-rpc/index.ts rename packages/common/src/{ => json-rpc}/providers/httpProvider.ts (100%) rename packages/common/src/{ => json-rpc}/providers/provider.ts (98%) rename packages/common/src/{ => wasm}/HdWallet.ts (99%) create mode 100644 packages/common/src/wasm/accountHelpers.ts create mode 100644 packages/common/src/wasm/deserialization.ts create mode 100644 packages/common/src/wasm/index.ts rename packages/common/src/{ => wasm}/schemaHelpers.ts (97%) create mode 100644 packages/common/src/wasm/serialization.ts diff --git a/.gitignore b/.gitignore index b69a29000..f6f8e6a4e 100644 --- a/.gitignore +++ b/.gitignore @@ -22,6 +22,7 @@ doc # Auto generate files from the gRPC proto file grpc +!packages/common/*/grpc # Webpack cache .webpack-cache diff --git a/packages/common/package.json b/packages/common/package.json index 08f26e3ff..05289bf50 100644 --- a/packages/common/package.json +++ b/packages/common/package.json @@ -13,6 +13,15 @@ "type": "module", "main": "lib/index.js", "types": "lib/index.d.ts", + "exports": { + "./wasm": "./lib/wasm/index.js", + "./cis0": "./lib/cis0.js", + "./cis2": "./lib/cis2/index.js", + "./cis4": "./lib/cis4/index.js", + "./identity": "./lib/identity/index.js", + "./grpc": "./lib/grpc/index.js", + "./json-rpc": "./lib/json-rpc/index.js" + }, "files": [ "/grpc/**/*", "/lib/**/*" diff --git a/packages/common/src/GenericContract.ts b/packages/common/src/GenericContract.ts index 685ed55c2..b6bd5c8ad 100644 --- a/packages/common/src/GenericContract.ts +++ b/packages/common/src/GenericContract.ts @@ -2,7 +2,7 @@ import { Buffer } from 'buffer/'; import { stringify } from 'json-bigint'; import { checkParameterLength, getContractName } from './contractHelpers'; -import { ConcordiumGRPCClient } from './GRPCClient'; +import { ConcordiumGRPCClient } from './grpc/GRPCClient'; import { AccountSigner, signTransaction } from './signHelpers'; import { AccountTransactionType, diff --git a/packages/common/src/accountHelpers.ts b/packages/common/src/accountHelpers.ts index 6ca4f3461..78e7c08d6 100644 --- a/packages/common/src/accountHelpers.ts +++ b/packages/common/src/accountHelpers.ts @@ -1,12 +1,9 @@ -import * as wasm from '@concordium/rust-bindings'; -import { AccountAddress } from './types/accountAddress'; import { ReduceStakePendingChange, RemovalPendingChange, AccountInfo, AccountInfoBaker, AccountInfoDelegator, - GenerateBakerKeysOutput, } from './types'; /** Whether {@link AccountInfo} parameter given is of type {@link AccountInfoDelegator}, i.e. the account is a delegator */ @@ -29,19 +26,3 @@ export const isReduceStakePendingChange = ( export const isRemovalPendingChange = ( spc: ReduceStakePendingChange | RemovalPendingChange ): spc is RemovalPendingChange => !isReduceStakePendingChange(spc); - -/** - * Generates random baker keys for the specified account, that can be used with the configureBaker transaction - * @param account the address of the account that the keys should be added to. - * @returns an object containing the public baker keys, their associated proofs and their associated private keys. - */ -export function generateBakerKeys( - account: AccountAddress -): GenerateBakerKeysOutput { - const rawKeys = wasm.generateBakerKeys(account.address); - try { - return JSON.parse(rawKeys); - } catch (e) { - throw new Error(rawKeys); - } -} diff --git a/packages/common/src/cis0.ts b/packages/common/src/cis0.ts index b2af250d3..b0e9fa6c4 100644 --- a/packages/common/src/cis0.ts +++ b/packages/common/src/cis0.ts @@ -2,7 +2,7 @@ import { Buffer } from 'buffer/'; import { stringify } from 'json-bigint'; import { ContractAddress, HexString } from './types'; -import { ConcordiumGRPCClient } from './GRPCClient'; +import { ConcordiumGRPCClient } from './grpc/GRPCClient'; import { encodeWord16, packBufferWithWord8Length, diff --git a/packages/common/src/cis2/CIS2Contract.ts b/packages/common/src/cis2/CIS2Contract.ts index e97df2273..87ec8de8f 100644 --- a/packages/common/src/cis2/CIS2Contract.ts +++ b/packages/common/src/cis2/CIS2Contract.ts @@ -1,7 +1,7 @@ import { stringify } from 'json-bigint'; import { ContractAddress, HexString, InvokeContractResult } from '../types'; -import { ConcordiumGRPCClient } from '../GRPCClient'; +import { ConcordiumGRPCClient } from '../grpc/GRPCClient'; import { AccountSigner } from '../signHelpers'; import { serializeCIS2Transfers, diff --git a/packages/common/src/cis4/CIS4Contract.ts b/packages/common/src/cis4/CIS4Contract.ts index 7947e1a64..ed4e86dfa 100644 --- a/packages/common/src/cis4/CIS4Contract.ts +++ b/packages/common/src/cis4/CIS4Contract.ts @@ -1,6 +1,5 @@ import { Buffer } from 'buffer/'; -import { AccountSigner, ConcordiumGRPCClient } from '..'; import { ContractTransactionMetadata, ContractUpdateTransactionWithSchema, @@ -10,6 +9,8 @@ import { getContractUpdateDefaultExpiryDate, getInvoker, } from '../GenericContract'; +import { ConcordiumGRPCClient } from '../grpc/GRPCClient'; +import { AccountSigner } from '../signHelpers'; import type { ContractAddress, Base58String, diff --git a/packages/common/src/cis4/util.ts b/packages/common/src/cis4/util.ts index 304727a95..af7f8b021 100644 --- a/packages/common/src/cis4/util.ts +++ b/packages/common/src/cis4/util.ts @@ -2,7 +2,7 @@ import { Buffer } from 'buffer/'; import { getPublicKey } from '@noble/ed25519'; import type { ContractAddress, HexString } from '../types'; -import type { CIS2 } from '../cis2'; +import type { CIS2 } from '../cis2/util'; import { deserializeCIS2MetadataUrl, serializeCIS2MetadataUrl, diff --git a/packages/common/src/credentialDeploymentTransactions.ts b/packages/common/src/credentialDeploymentTransactions.ts index a36c16b12..e5bf440fa 100644 --- a/packages/common/src/credentialDeploymentTransactions.ts +++ b/packages/common/src/credentialDeploymentTransactions.ts @@ -20,7 +20,7 @@ import { AccountAddress } from './types/accountAddress'; import { sha256 } from './hash'; import { encode as bs58Encode } from 'bs58check'; import { Buffer } from 'buffer/'; -import { ConcordiumHdWallet } from './HdWallet'; +import { ConcordiumHdWallet } from './wasm/HdWallet'; import { AttributesKeys, CredentialDeploymentDetails, HexString } from '.'; import { filterRecord, mapRecord } from './util'; import { getCredentialDeploymentSignDigest } from './serialization'; diff --git a/packages/common/src/deserialization.ts b/packages/common/src/deserialization.ts index aebf2000f..386004b58 100644 --- a/packages/common/src/deserialization.ts +++ b/packages/common/src/deserialization.ts @@ -1,4 +1,3 @@ -import * as wasm from '@concordium/rust-bindings'; import { Buffer } from 'buffer/'; import { getAccountTransactionHandler } from './accountTransactions'; import { Cursor } from './deserializationHelpers'; @@ -6,10 +5,7 @@ import { AccountTransaction, AccountTransactionHeader, AccountTransactionSignature, - BlockItemKind, isAccountTransactionType, - SmartContractTypeValues, - TypedCredentialDeployment, } from './types'; import { AccountAddress } from './types/accountAddress'; import { TransactionExpiry } from './types/transactionExpiry'; @@ -24,32 +20,6 @@ export function deserializeUint8(source: Cursor): number { return source.read(1).readUInt8(0); } -/** - * Given a contract's raw state, its name and its schema, return the state as a JSON object. - * The return type is any, and the actual type should be determined by using the schema. - */ -export function deserializeContractState( - contractName: string, - schema: Buffer, - state: Buffer, - verboseErrorMessage = false - // eslint-disable-next-line @typescript-eslint/no-explicit-any -): any { - const serializedState = wasm.deserializeState( - contractName, - state.toString('hex'), - schema.toString('hex'), - verboseErrorMessage - ); - try { - return JSON.parse(serializedState); - } catch (e) { - throw new Error( - 'unable to deserialize state, due to: ' + serializedState - ); // In this case serializedState is the error message from the rust module - } -} - function deserializeMap( serialized: Cursor, decodeSize: (size: Cursor) => number, @@ -111,7 +81,7 @@ function deserializeTransactionHeader( }; } -function deserializeAccountTransaction(serializedTransaction: Cursor): { +export function deserializeAccountTransaction(serializedTransaction: Cursor): { accountTransaction: AccountTransaction; signatures: AccountTransactionSignature; } { @@ -142,193 +112,3 @@ function deserializeAccountTransaction(serializedTransaction: Cursor): { signatures, }; } - -function deserializeCredentialDeployment(serializedDeployment: Cursor) { - const raw = wasm.deserializeCredentialDeployment( - serializedDeployment.read().toString('hex') - ); - try { - const parsed = JSON.parse(raw); - return { - credential: parsed.credential, - expiry: parsed.messageExpiry, - }; - } catch { - // If the return value is not a proper JSON, it should be an error message. - throw new Error(raw); - } -} - -export type BlockItem = - | { - kind: BlockItemKind.AccountTransactionKind; - transaction: { - accountTransaction: AccountTransaction; - signatures: AccountTransactionSignature; - }; - } - | { - kind: BlockItemKind.CredentialDeploymentKind; - transaction: { - credential: TypedCredentialDeployment; - expiry: number; - }; - }; - -/** - * Deserializes a transaction, from the binary format used to send it to the node, back into an js object. - * @param serializedTransaction A buffer containing the binary transaction. It is expected to start with the version and blockItemKind. - * @returns An object specifiying the blockItemKind that the transaction has. The object also contains the actual transaction under the transaction field. - **/ -export function deserializeTransaction( - serializedTransaction: Buffer -): BlockItem { - const cursor = new Cursor(serializedTransaction); - - const version = deserializeUint8(cursor); - if (version !== 0) { - throw new Error( - 'Supplied version ' + - version + - ' is not valid. Only transactions with version 0 format are supported' - ); - } - const blockItemKind = deserializeUint8(cursor); - switch (blockItemKind) { - case BlockItemKind.AccountTransactionKind: - return { - kind: BlockItemKind.AccountTransactionKind, - transaction: deserializeAccountTransaction(cursor), - }; - case BlockItemKind.CredentialDeploymentKind: - return { - kind: BlockItemKind.CredentialDeploymentKind, - transaction: deserializeCredentialDeployment(cursor), - }; - case BlockItemKind.UpdateInstructionKind: - throw new Error( - 'deserialization of UpdateInstructions is not supported' - ); - default: - throw new Error('Invalid blockItemKind'); - } -} - -/** - * Deserializes a receive functions's return value from a sequence of bytes into a json object. - * @param returnValueBytes A buffer containing the return value as raw bytes. - * @param moduleSchema The raw module schema as a buffer. - * @param contractName The name of the contract where the receive function is located. - * @param functionName The name of the receive function which return value you want to deserialize. - * @param schemaVersion The schema version as a number. This parameter is optional, if you provide a serialized versioned schema this argument won't be needed. - * @param verboseErrorMessage Whether errors are in a verbose format or not. Defaults to `false`. - */ -export function deserializeReceiveReturnValue( - returnValueBytes: Buffer, - moduleSchema: Buffer, - contractName: string, - functionName: string, - schemaVersion?: number, - verboseErrorMessage = false - // eslint-disable-next-line @typescript-eslint/no-explicit-any -): any { - const deserializedReturnValue = wasm.deserializeReceiveReturnValue( - returnValueBytes.toString('hex'), - moduleSchema.toString('hex'), - contractName, - functionName, - schemaVersion, - verboseErrorMessage - ); - try { - return JSON.parse(deserializedReturnValue); - } catch (e) { - throw new Error( - 'unable to deserialize the return value, due to: ' + - deserializedReturnValue - ); // In this case deserializedReturnValue is the error message from the rust module - } -} - -/** - * Deserializes a receive function's error from a sequence of bytes into a json object. - * @param errorBytes A buffer containing the error as raw bytes. - * @param moduleSchema The raw module schema as a buffer. - * @param contractName The name of the contract where the receive function is located. - * @param functionName The name of the receive function which error you want to deserialize. - * @param verboseErrorMessage Whether errors are in a verbose format or not. Defaults to `false`. - */ -export function deserializeReceiveError( - errorBytes: Buffer, - moduleSchema: Buffer, - contractName: string, - functionName: string, - verboseErrorMessage = false - // eslint-disable-next-line @typescript-eslint/no-explicit-any -): any { - const deserializedError = wasm.deserializeReceiveError( - errorBytes.toString('hex'), - moduleSchema.toString('hex'), - contractName, - functionName, - verboseErrorMessage - ); - try { - return JSON.parse(deserializedError); - } catch (e) { - throw new Error( - 'unable to deserialize the error value, due to: ' + - deserializedError - ); // In this case deserializedError is the error message from the rust module - } -} - -/** - * Deserializes an init function's error from a sequence of bytes into a json object. - * @param errorBytes A buffer containing the error as raw bytes. - * @param moduleSchema The raw module schema as a buffer. - * @param contractName The name of the init function which error you want to deserialize. - * @param verboseErrorMessage Whether errors are in a verbose format or not. Defaults to `false`. - */ -export function deserializeInitError( - errorBytes: Buffer, - moduleSchema: Buffer, - contractName: string, - verboseErrorMessage = false - // eslint-disable-next-line @typescript-eslint/no-explicit-any -): any { - const deserializedError = wasm.deserializeInitError( - errorBytes.toString('hex'), - moduleSchema.toString('hex'), - contractName, - verboseErrorMessage - ); - try { - return JSON.parse(deserializedError); - } catch (e) { - throw new Error( - 'unable to deserialize the error value, due to: ' + - deserializedError - ); // In this case deserializedError is the error message from the rust module - } -} - -/** - * Given a binary value for a smart contract type, and the raw schema for that type, deserialize the value into the JSON representation. - * @param value the value that should be deserialized. - * @param rawSchema the schema for the type that the given value should be deserialized as - * @param verboseErrorMessage Whether errors are in a verbose format or not. Defaults to `false`. - * @returns the deserialized value - */ -export function deserializeTypeValue( - value: Buffer, - rawSchema: Buffer, - verboseErrorMessage = false -): SmartContractTypeValues { - const deserializedValue = wasm.deserializeTypeValue( - value.toString('hex'), - rawSchema.toString('hex'), - verboseErrorMessage - ); - return JSON.parse(deserializedValue); -} diff --git a/packages/common/src/GRPCClient.ts b/packages/common/src/grpc/GRPCClient.ts similarity index 98% rename from packages/common/src/GRPCClient.ts rename to packages/common/src/grpc/GRPCClient.ts index 1569caf23..bd416f3c4 100644 --- a/packages/common/src/GRPCClient.ts +++ b/packages/common/src/grpc/GRPCClient.ts @@ -5,16 +5,17 @@ * @module Common GRPC-Client */ import { Buffer } from 'buffer/'; -import * as v1 from './types'; -import * as v2 from '../grpc/v2/concordium/types'; -import { Base58String, HexString, isRpcError } from './types'; -import { QueriesClient } from '../grpc/v2/concordium/service.client'; import type { RpcTransport } from '@protobuf-ts/runtime-rpc'; -import { CredentialRegistrationId } from './types/CredentialRegistrationId'; -import * as translate from './GRPCTypeTranslation'; -import { AccountAddress } from './types/accountAddress'; -import { getAccountTransactionHandler } from './accountTransactions'; -import { calculateEnergyCost } from './energyCost'; + +import * as v1 from '../types'; +import * as v2 from '../../grpc/v2/concordium/types'; +import { Base58String, HexString, isRpcError } from '../types'; +import { QueriesClient } from '../../grpc/v2/concordium/service.client'; +import { CredentialRegistrationId } from '../types/CredentialRegistrationId'; +import * as translate from './translation'; +import { AccountAddress } from '../types/accountAddress'; +import { getAccountTransactionHandler } from '../accountTransactions'; +import { calculateEnergyCost } from '../energyCost'; import { countSignatures, isHex, @@ -23,14 +24,12 @@ import { mapStream, unwrap, wasmToSchema, -} from './util'; -import { - serializeAccountTransactionPayload, - serializeCredentialDeploymentPayload, -} from './serialization'; -import { BlockItemStatus, BlockItemSummary } from './types/blockItemSummary'; -import { ModuleReference } from './types/moduleReference'; -import { DEFAULT_INVOKE_ENERGY } from './constants'; +} from '../util'; +import { serializeAccountTransactionPayload } from '../serialization'; +import { BlockItemStatus, BlockItemSummary } from '../types/blockItemSummary'; +import { ModuleReference } from '../types/moduleReference'; +import { DEFAULT_INVOKE_ENERGY } from '../constants'; +import { serializeCredentialDeploymentPayload } from '../wasm/serialization'; // TODO: is this necessary?? /** * @hidden diff --git a/packages/common/src/grpc/index.ts b/packages/common/src/grpc/index.ts new file mode 100644 index 000000000..244251e7b --- /dev/null +++ b/packages/common/src/grpc/index.ts @@ -0,0 +1 @@ +export { ConcordiumGRPCClient } from './GRPCClient'; diff --git a/packages/common/src/GRPCTypeTranslation.ts b/packages/common/src/grpc/translation.ts similarity index 99% rename from packages/common/src/GRPCTypeTranslation.ts rename to packages/common/src/grpc/translation.ts index 02a343074..6756237e4 100644 --- a/packages/common/src/GRPCTypeTranslation.ts +++ b/packages/common/src/grpc/translation.ts @@ -1,12 +1,13 @@ -import * as v1 from './types'; -import * as v2 from '../grpc/v2/concordium/types'; -import { mapRecord, unwrap } from './util'; import { Buffer } from 'buffer/'; import bs58check from 'bs58check'; -import { AccountAddress } from './types/accountAddress'; -import { ModuleReference } from './types/moduleReference'; -import { CcdAmount } from './types/ccdAmount'; -import { Base58String } from './types'; + +import * as v1 from '../types'; +import * as v2 from '../../grpc/v2/concordium/types'; +import { mapRecord, unwrap } from '../util'; +import { AccountAddress } from '../types/accountAddress'; +import { ModuleReference } from '../types/moduleReference'; +import { CcdAmount } from '../types/ccdAmount'; +import { Base58String } from '../types'; function unwrapToHex(bytes: Uint8Array | undefined): v1.HexString { return Buffer.from(unwrap(bytes)).toString('hex'); diff --git a/packages/common/src/idProofTypes.ts b/packages/common/src/identity/idProofTypes.ts similarity index 98% rename from packages/common/src/idProofTypes.ts rename to packages/common/src/identity/idProofTypes.ts index 679b9cf4b..6a743c395 100644 --- a/packages/common/src/idProofTypes.ts +++ b/packages/common/src/identity/idProofTypes.ts @@ -1,10 +1,10 @@ -import { +import type { AttributeKey, CryptographicParameters, IdentityObjectV1, Network, Versioned, -} from '.'; +} from '../types'; export enum StatementTypes { RevealAttribute = 'RevealAttribute', diff --git a/packages/common/src/idProofs.ts b/packages/common/src/identity/idProofs.ts similarity index 99% rename from packages/common/src/idProofs.ts rename to packages/common/src/identity/idProofs.ts index 4dd68fdef..3da28bc33 100644 --- a/packages/common/src/idProofs.ts +++ b/packages/common/src/identity/idProofs.ts @@ -4,7 +4,7 @@ import { AttributeKeyString, AttributesKeys, IdDocType, -} from './types'; +} from '../types'; import { AtomicStatement, IdProofInput, diff --git a/packages/common/src/identity.ts b/packages/common/src/identity/identity.ts similarity index 98% rename from packages/common/src/identity.ts rename to packages/common/src/identity/identity.ts index 81a3663e1..36c2f78e1 100644 --- a/packages/common/src/identity.ts +++ b/packages/common/src/identity/identity.ts @@ -7,7 +7,7 @@ import { IpInfo, Network, Versioned, -} from './types'; +} from '../types'; export type IdentityRequestInput = { ipInfo: IpInfo; diff --git a/packages/common/src/identity/index.ts b/packages/common/src/identity/index.ts new file mode 100644 index 000000000..199c1b0b2 --- /dev/null +++ b/packages/common/src/identity/index.ts @@ -0,0 +1,3 @@ +export * from './idProofs'; +export * from './idProofTypes'; +export * from './identity'; diff --git a/packages/common/src/index.ts b/packages/common/src/index.ts index f1732d1ea..148353cf9 100644 --- a/packages/common/src/index.ts +++ b/packages/common/src/index.ts @@ -4,16 +4,10 @@ export { getAccountTransactionHash, getAccountTransactionSignDigest, getCredentialDeploymentSignDigest, - getCredentialDeploymentTransactionHash, getCredentialForExistingAccountSignDigest, - serializeInitContractParameters, - serializeUpdateContractParameters, serializeAccountTransactionForSubmission, - serializeCredentialDeploymentTransactionForSubmission, getSignedCredentialDeploymentTransactionHash, - serializeTypeValue, serializeAccountTransactionPayload, - serializeCredentialDeploymentPayload, } from './serialization'; export { sha256 }; export { CredentialRegistrationId } from './types/CredentialRegistrationId'; @@ -24,36 +18,17 @@ export { DataBlob } from './types/DataBlob'; export { ModuleReference } from './types/moduleReference'; export * from './credentialDeploymentTransactions'; export { isAlias, getAlias } from './alias'; -export { - deserializeContractState, - deserializeTransaction, - deserializeReceiveReturnValue, - deserializeReceiveError, - deserializeInitError, - deserializeTypeValue, -} from './deserialization'; -export * from './idProofs'; -export * from './idProofTypes'; +export { deserializeAccountTransaction } from './deserialization'; export * from './signHelpers'; export * from './versionedTypeHelpers'; export * from './accountHelpers'; export * from './blockSummaryHelpers'; export * from './rewardStatusHelpers'; -export * from './HdWallet'; -export * from './schemaHelpers'; export { getContractName } from './contractHelpers'; export { isHex, streamToList, wasmToSchema, unwrap } from './util'; -export { HttpProvider } from './providers/httpProvider'; -export { JsonRpcClient } from './JsonRpcClient'; -export * from './identity'; -export { ConcordiumGRPCClient } from './GRPCClient'; - export { getAccountTransactionHandler } from './accountTransactions'; export * from './energyCost'; export * from './uleb128'; -export * from './cis2'; -export * from './cis0'; -export * from './cis4'; diff --git a/packages/common/src/JsonRpcClient.ts b/packages/common/src/json-rpc/JsonRpcClient.ts similarity index 97% rename from packages/common/src/JsonRpcClient.ts rename to packages/common/src/json-rpc/JsonRpcClient.ts index 692f43d2f..b9110f98d 100644 --- a/packages/common/src/JsonRpcClient.ts +++ b/packages/common/src/json-rpc/JsonRpcClient.ts @@ -18,21 +18,21 @@ import { TransactionStatus, TransactionSummary, Versioned, -} from './types'; -import { AccountAddress } from './types/accountAddress'; +} from '../types'; +import { AccountAddress } from '../types/accountAddress'; import Provider, { JsonRpcResponse } from './providers/provider'; import { serializeAccountTransactionForSubmission, serializeSignedCredentialDeploymentDetailsForSubmission, -} from './serialization'; -import { CcdAmount } from './types/ccdAmount'; -import { ModuleReference } from './types/moduleReference'; +} from '../serialization'; +import { CcdAmount } from '../types/ccdAmount'; +import { ModuleReference } from '../types/moduleReference'; import { buildJsonResponseReviver, intToStringTransformer, isValidHash, -} from './util'; -import { CredentialRegistrationId } from './types/CredentialRegistrationId'; +} from '../util'; +import { CredentialRegistrationId } from '../types/CredentialRegistrationId'; function transformJsonResponse( jsonString: string, diff --git a/packages/common/src/json-rpc/index.ts b/packages/common/src/json-rpc/index.ts new file mode 100644 index 000000000..57d7788bc --- /dev/null +++ b/packages/common/src/json-rpc/index.ts @@ -0,0 +1,2 @@ +export { HttpProvider } from './providers/httpProvider'; +export { JsonRpcClient } from './JsonRpcClient'; diff --git a/packages/common/src/providers/httpProvider.ts b/packages/common/src/json-rpc/providers/httpProvider.ts similarity index 100% rename from packages/common/src/providers/httpProvider.ts rename to packages/common/src/json-rpc/providers/httpProvider.ts diff --git a/packages/common/src/providers/provider.ts b/packages/common/src/json-rpc/providers/provider.ts similarity index 98% rename from packages/common/src/providers/provider.ts rename to packages/common/src/json-rpc/providers/provider.ts index 67cb1e2b4..7967d165b 100644 --- a/packages/common/src/providers/provider.ts +++ b/packages/common/src/json-rpc/providers/provider.ts @@ -1,4 +1,4 @@ -import { Invoker } from '../types'; +import { Invoker } from '../../types'; /* eslint-disable @typescript-eslint/no-explicit-any */ interface JsonRpcResponseBase { diff --git a/packages/common/src/serialization.ts b/packages/common/src/serialization.ts index def4867f4..cd36df13d 100644 --- a/packages/common/src/serialization.ts +++ b/packages/common/src/serialization.ts @@ -31,7 +31,6 @@ import { calculateEnergyCost } from './energyCost'; import { countSignatures } from './util'; import { AccountAddress } from './types/accountAddress'; import { sha256 } from './hash'; -import * as wasm from '@concordium/rust-bindings'; function serializeAccountTransactionType(type: AccountTransactionType): Buffer { return Buffer.from(Uint8Array.of(type)); @@ -406,130 +405,6 @@ export function getCredentialDeploymentSignDigest( ]); } -interface DeploymentDetailsResult { - credInfo: string; - serializedTransaction: string; - transactionHash: string; -} - -/** - * Gets the transaction hash that is used to look up the status of a credential - * deployment transaction. - * @param credentialDeployment the transaction to hash - * @param signatures the signatures that will also be part of the hash - * @returns the sha256 hash of the serialized block item kind, signatures, and credential deployment transaction - */ -export function getCredentialDeploymentTransactionHash( - credentialDeployment: CredentialDeploymentDetails, - signatures: string[] -): string { - const credentialDeploymentInfo: DeploymentDetailsResult = JSON.parse( - wasm.getDeploymentDetails( - signatures, - JSON.stringify(credentialDeployment.unsignedCdi), - credentialDeployment.expiry.expiryEpochSeconds - ) - ); - return credentialDeploymentInfo.transactionHash; -} - -/** - * Serializes a credential deployment transaction of a new account, so that it is ready for being - * submitted to the node. - * @param credentialDeployment the credenetial deployment transaction - * @param signatures the signatures on the hash of unsigned credential deployment information - * @returns the serialization of the credential deployment transaction ready for being submitted to a node - */ -export function serializeCredentialDeploymentTransactionForSubmission( - credentialDeployment: CredentialDeploymentDetails, - signatures: string[] -): Buffer { - const credentialDeploymentInfo: DeploymentDetailsResult = JSON.parse( - wasm.getDeploymentDetails( - signatures, - JSON.stringify(credentialDeployment.unsignedCdi), - credentialDeployment.expiry.expiryEpochSeconds - ) - ); - return Buffer.from(credentialDeploymentInfo.serializedTransaction, 'hex'); -} - -/** - * @param contractName name of the contract that the init contract transaction will initialize - * @param parameters the parameters to be serialized. Should correspond to the JSON representation. - * @param rawSchema buffer for the schema of a module that contains the contract - * @param schemaVersion the version of the schema provided - * @param verboseErrorMessage Whether errors are in a verbose format or not. Defaults to `false`. - * @returns serialized buffer of init contract parameters - */ -export function serializeInitContractParameters( - contractName: string, - // eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/explicit-module-boundary-types - parameters: any, - rawSchema: Buffer, - schemaVersion?: SchemaVersion, - verboseErrorMessage = false -): Buffer { - const serializedParameters = wasm.serializeInitContractParameters( - JSON.stringify(parameters), - rawSchema.toString('hex'), - contractName, - schemaVersion, - verboseErrorMessage - ); - return Buffer.from(serializedParameters, 'hex'); -} - -/** - * @param contractName name of the contract that the update contract transaction will update - * @param receiveFunctionName name of function that the update contract transaction will invoke - * @param parameters the parameters to be serialized. Should correspond to the JSON representation. - * @param rawSchema buffer for the schema of a module that contains the contract - * @param schemaVersion the version of the schema provided - * @param verboseErrorMessage Whether errors are in a verbose format or not. Defaults to `false`. - * @returns serialized buffer of update contract parameters - */ -export function serializeUpdateContractParameters( - contractName: string, - receiveFunctionName: string, - // eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/explicit-module-boundary-types - parameters: any, - rawSchema: Buffer, - schemaVersion?: SchemaVersion, - verboseErrorMessage = false -): Buffer { - const serializedParameters = wasm.serializeReceiveContractParameters( - JSON.stringify(parameters), - rawSchema.toString('hex'), - contractName, - receiveFunctionName, - schemaVersion, - verboseErrorMessage - ); - return Buffer.from(serializedParameters, 'hex'); -} - -/** - * Given a value for a smart contract type, and the raw schema for that type, serialize the value into binary format. - * @param value the value that should be serialized. Should correspond to the JSON representation - * @param rawSchema the schema for the type that the given value should be serialized as - * @param verboseErrorMessage Whether errors are in a verbose format or not. Defaults to `false`. - * @returns serialized buffer of the value - */ -export function serializeTypeValue( - // eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/explicit-module-boundary-types - value: any, - rawSchema: Buffer, - verboseErrorMessage = false -): Buffer { - const serializedValue = wasm.serializeTypeValue( - JSON.stringify(value), - rawSchema.toString('hex'), - verboseErrorMessage - ); - return Buffer.from(serializedValue, 'hex'); -} - /** * @deprecated the SignedCredentialDeploymentDetails is only used with JSON-RPC */ @@ -576,14 +451,3 @@ export function getSignedCredentialDeploymentTransactionHash( serializeSignedCredentialDeploymentDetails(credentialDetails); return sha256([serializedDetails]).toString('hex'); } - -export function serializeCredentialDeploymentPayload( - signatures: string[], - credentialDeploymentTransaction: CredentialDeploymentTransaction -): Buffer { - const payloadByteArray = wasm.serializeCredentialDeploymentPayload( - signatures, - JSON.stringify(credentialDeploymentTransaction.unsignedCdi) - ); - return Buffer.from(payloadByteArray); -} diff --git a/packages/common/src/HdWallet.ts b/packages/common/src/wasm/HdWallet.ts similarity index 99% rename from packages/common/src/HdWallet.ts rename to packages/common/src/wasm/HdWallet.ts index 1ac29ba43..1d685c875 100644 --- a/packages/common/src/HdWallet.ts +++ b/packages/common/src/wasm/HdWallet.ts @@ -2,8 +2,8 @@ import * as wasm from '@concordium/rust-bindings'; import { mnemonicToSeedSync, validateMnemonic } from '@scure/bip39'; import { wordlist } from '@scure/bip39/wordlists/english'; import { Buffer } from 'buffer/'; -import { AttributesKeys, Network, CryptographicParameters } from './types'; -import { isHex } from './util'; +import { AttributesKeys, Network, CryptographicParameters } from '../types'; +import { isHex } from '../util'; /** * Class for Hierarchical Deterministic key derivation for Concordium identities and accounts. diff --git a/packages/common/src/wasm/accountHelpers.ts b/packages/common/src/wasm/accountHelpers.ts new file mode 100644 index 000000000..1f7aa98b0 --- /dev/null +++ b/packages/common/src/wasm/accountHelpers.ts @@ -0,0 +1,19 @@ +import * as wasm from '@concordium/rust-bindings'; +import { GenerateBakerKeysOutput } from '../types'; +import { AccountAddress } from '../types/accountAddress'; + +/** + * Generates random baker keys for the specified account, that can be used with the configureBaker transaction + * @param account the address of the account that the keys should be added to. + * @returns an object containing the public baker keys, their associated proofs and their associated private keys. + */ +export function generateBakerKeys( + account: AccountAddress +): GenerateBakerKeysOutput { + const rawKeys = wasm.generateBakerKeys(account.address); + try { + return JSON.parse(rawKeys); + } catch (e) { + throw new Error(rawKeys); + } +} diff --git a/packages/common/src/wasm/deserialization.ts b/packages/common/src/wasm/deserialization.ts new file mode 100644 index 000000000..b71b83ef5 --- /dev/null +++ b/packages/common/src/wasm/deserialization.ts @@ -0,0 +1,230 @@ +import * as wasm from '@concordium/rust-bindings'; +import { Buffer } from 'buffer/'; +import { + deserializeAccountTransaction, + deserializeUint8, +} from '../deserialization'; +import { Cursor } from '../deserializationHelpers'; +import { + AccountTransaction, + AccountTransactionSignature, + BlockItemKind, + SmartContractTypeValues, + TypedCredentialDeployment, +} from '../types'; + +/** + * Given a contract's raw state, its name and its schema, return the state as a JSON object. + * The return type is any, and the actual type should be determined by using the schema. + */ +export function deserializeContractState( + contractName: string, + schema: Buffer, + state: Buffer, + verboseErrorMessage = false + // eslint-disable-next-line @typescript-eslint/no-explicit-any +): any { + const serializedState = wasm.deserializeState( + contractName, + state.toString('hex'), + schema.toString('hex'), + verboseErrorMessage + ); + try { + return JSON.parse(serializedState); + } catch (e) { + throw new Error( + 'unable to deserialize state, due to: ' + serializedState + ); // In this case serializedState is the error message from the rust module + } +} + +function deserializeCredentialDeployment(serializedDeployment: Cursor) { + const raw = wasm.deserializeCredentialDeployment( + serializedDeployment.read().toString('hex') + ); + try { + const parsed = JSON.parse(raw); + return { + credential: parsed.credential, + expiry: parsed.messageExpiry, + }; + } catch { + // If the return value is not a proper JSON, it should be an error message. + throw new Error(raw); + } +} + +export type BlockItem = + | { + kind: BlockItemKind.AccountTransactionKind; + transaction: { + accountTransaction: AccountTransaction; + signatures: AccountTransactionSignature; + }; + } + | { + kind: BlockItemKind.CredentialDeploymentKind; + transaction: { + credential: TypedCredentialDeployment; + expiry: number; + }; + }; + +/** + * Deserializes a transaction, from the binary format used to send it to the node, back into an js object. + * @param serializedTransaction A buffer containing the binary transaction. It is expected to start with the version and blockItemKind. + * @returns An object specifiying the blockItemKind that the transaction has. The object also contains the actual transaction under the transaction field. + **/ +export function deserializeTransaction( + serializedTransaction: Buffer +): BlockItem { + const cursor = new Cursor(serializedTransaction); + + const version = deserializeUint8(cursor); + if (version !== 0) { + throw new Error( + 'Supplied version ' + + version + + ' is not valid. Only transactions with version 0 format are supported' + ); + } + const blockItemKind = deserializeUint8(cursor); + switch (blockItemKind) { + case BlockItemKind.AccountTransactionKind: + return { + kind: BlockItemKind.AccountTransactionKind, + transaction: deserializeAccountTransaction(cursor), + }; + case BlockItemKind.CredentialDeploymentKind: + return { + kind: BlockItemKind.CredentialDeploymentKind, + transaction: deserializeCredentialDeployment(cursor), + }; + case BlockItemKind.UpdateInstructionKind: + throw new Error( + 'deserialization of UpdateInstructions is not supported' + ); + default: + throw new Error('Invalid blockItemKind'); + } +} + +/** + * Deserializes a receive functions's return value from a sequence of bytes into a json object. + * @param returnValueBytes A buffer containing the return value as raw bytes. + * @param moduleSchema The raw module schema as a buffer. + * @param contractName The name of the contract where the receive function is located. + * @param functionName The name of the receive function which return value you want to deserialize. + * @param schemaVersion The schema version as a number. This parameter is optional, if you provide a serialized versioned schema this argument won't be needed. + * @param verboseErrorMessage Whether errors are in a verbose format or not. Defaults to `false`. + */ +export function deserializeReceiveReturnValue( + returnValueBytes: Buffer, + moduleSchema: Buffer, + contractName: string, + functionName: string, + schemaVersion?: number, + verboseErrorMessage = false + // eslint-disable-next-line @typescript-eslint/no-explicit-any +): any { + const deserializedReturnValue = wasm.deserializeReceiveReturnValue( + returnValueBytes.toString('hex'), + moduleSchema.toString('hex'), + contractName, + functionName, + schemaVersion, + verboseErrorMessage + ); + try { + return JSON.parse(deserializedReturnValue); + } catch (e) { + throw new Error( + 'unable to deserialize the return value, due to: ' + + deserializedReturnValue + ); // In this case deserializedReturnValue is the error message from the rust module + } +} + +/** + * Deserializes a receive function's error from a sequence of bytes into a json object. + * @param errorBytes A buffer containing the error as raw bytes. + * @param moduleSchema The raw module schema as a buffer. + * @param contractName The name of the contract where the receive function is located. + * @param functionName The name of the receive function which error you want to deserialize. + * @param verboseErrorMessage Whether errors are in a verbose format or not. Defaults to `false`. + */ +export function deserializeReceiveError( + errorBytes: Buffer, + moduleSchema: Buffer, + contractName: string, + functionName: string, + verboseErrorMessage = false + // eslint-disable-next-line @typescript-eslint/no-explicit-any +): any { + const deserializedError = wasm.deserializeReceiveError( + errorBytes.toString('hex'), + moduleSchema.toString('hex'), + contractName, + functionName, + verboseErrorMessage + ); + try { + return JSON.parse(deserializedError); + } catch (e) { + throw new Error( + 'unable to deserialize the error value, due to: ' + + deserializedError + ); // In this case deserializedError is the error message from the rust module + } +} + +/** + * Deserializes an init function's error from a sequence of bytes into a json object. + * @param errorBytes A buffer containing the error as raw bytes. + * @param moduleSchema The raw module schema as a buffer. + * @param contractName The name of the init function which error you want to deserialize. + * @param verboseErrorMessage Whether errors are in a verbose format or not. Defaults to `false`. + */ +export function deserializeInitError( + errorBytes: Buffer, + moduleSchema: Buffer, + contractName: string, + verboseErrorMessage = false + // eslint-disable-next-line @typescript-eslint/no-explicit-any +): any { + const deserializedError = wasm.deserializeInitError( + errorBytes.toString('hex'), + moduleSchema.toString('hex'), + contractName, + verboseErrorMessage + ); + try { + return JSON.parse(deserializedError); + } catch (e) { + throw new Error( + 'unable to deserialize the error value, due to: ' + + deserializedError + ); // In this case deserializedError is the error message from the rust module + } +} + +/** + * Given a binary value for a smart contract type, and the raw schema for that type, deserialize the value into the JSON representation. + * @param value the value that should be deserialized. + * @param rawSchema the schema for the type that the given value should be deserialized as + * @param verboseErrorMessage Whether errors are in a verbose format or not. Defaults to `false`. + * @returns the deserialized value + */ +export function deserializeTypeValue( + value: Buffer, + rawSchema: Buffer, + verboseErrorMessage = false +): SmartContractTypeValues { + const deserializedValue = wasm.deserializeTypeValue( + value.toString('hex'), + rawSchema.toString('hex'), + verboseErrorMessage + ); + return JSON.parse(deserializedValue); +} diff --git a/packages/common/src/wasm/index.ts b/packages/common/src/wasm/index.ts new file mode 100644 index 000000000..10438cc74 --- /dev/null +++ b/packages/common/src/wasm/index.ts @@ -0,0 +1,18 @@ +export { + getCredentialDeploymentTransactionHash, + serializeInitContractParameters, + serializeUpdateContractParameters, + serializeCredentialDeploymentTransactionForSubmission, + serializeTypeValue, + serializeCredentialDeploymentPayload, +} from './serialization'; +export { + deserializeContractState, + deserializeTransaction, + deserializeReceiveReturnValue, + deserializeReceiveError, + deserializeInitError, + deserializeTypeValue, +} from './deserialization'; +export * from './HdWallet'; +export * from './schemaHelpers'; diff --git a/packages/common/src/schemaHelpers.ts b/packages/common/src/wasm/schemaHelpers.ts similarity index 97% rename from packages/common/src/schemaHelpers.ts rename to packages/common/src/wasm/schemaHelpers.ts index 27394aaa8..1e265723c 100644 --- a/packages/common/src/schemaHelpers.ts +++ b/packages/common/src/wasm/schemaHelpers.ts @@ -1,6 +1,6 @@ import { Buffer } from 'buffer/'; -import { SchemaVersion } from './types'; import * as wasm from '@concordium/rust-bindings'; +import { SchemaVersion } from '../types'; /** * @param moduleSchema buffer for the schema of a module that contains the contract diff --git a/packages/common/src/wasm/serialization.ts b/packages/common/src/wasm/serialization.ts new file mode 100644 index 000000000..d8df269da --- /dev/null +++ b/packages/common/src/wasm/serialization.ts @@ -0,0 +1,142 @@ +import * as wasm from '@concordium/rust-bindings'; +import { Buffer } from 'buffer/'; +import { + CredentialDeploymentDetails, + CredentialDeploymentTransaction, + SchemaVersion, +} from '../types'; + +interface DeploymentDetailsResult { + credInfo: string; + serializedTransaction: string; + transactionHash: string; +} + +/** + * Gets the transaction hash that is used to look up the status of a credential + * deployment transaction. + * @param credentialDeployment the transaction to hash + * @param signatures the signatures that will also be part of the hash + * @returns the sha256 hash of the serialized block item kind, signatures, and credential deployment transaction + */ +export function getCredentialDeploymentTransactionHash( + credentialDeployment: CredentialDeploymentDetails, + signatures: string[] +): string { + const credentialDeploymentInfo: DeploymentDetailsResult = JSON.parse( + wasm.getDeploymentDetails( + signatures, + JSON.stringify(credentialDeployment.unsignedCdi), + credentialDeployment.expiry.expiryEpochSeconds + ) + ); + return credentialDeploymentInfo.transactionHash; +} + +/** + * Serializes a credential deployment transaction of a new account, so that it is ready for being + * submitted to the node. + * @param credentialDeployment the credenetial deployment transaction + * @param signatures the signatures on the hash of unsigned credential deployment information + * @returns the serialization of the credential deployment transaction ready for being submitted to a node + */ +export function serializeCredentialDeploymentTransactionForSubmission( + credentialDeployment: CredentialDeploymentDetails, + signatures: string[] +): Buffer { + const credentialDeploymentInfo: DeploymentDetailsResult = JSON.parse( + wasm.getDeploymentDetails( + signatures, + JSON.stringify(credentialDeployment.unsignedCdi), + credentialDeployment.expiry.expiryEpochSeconds + ) + ); + return Buffer.from(credentialDeploymentInfo.serializedTransaction, 'hex'); +} + +/** + * @param contractName name of the contract that the init contract transaction will initialize + * @param parameters the parameters to be serialized. Should correspond to the JSON representation. + * @param rawSchema buffer for the schema of a module that contains the contract + * @param schemaVersion the version of the schema provided + * @param verboseErrorMessage Whether errors are in a verbose format or not. Defaults to `false`. + * @returns serialized buffer of init contract parameters + */ +export function serializeInitContractParameters( + contractName: string, + // eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/explicit-module-boundary-types + parameters: any, + rawSchema: Buffer, + schemaVersion?: SchemaVersion, + verboseErrorMessage = false +): Buffer { + const serializedParameters = wasm.serializeInitContractParameters( + JSON.stringify(parameters), + rawSchema.toString('hex'), + contractName, + schemaVersion, + verboseErrorMessage + ); + return Buffer.from(serializedParameters, 'hex'); +} + +/** + * @param contractName name of the contract that the update contract transaction will update + * @param receiveFunctionName name of function that the update contract transaction will invoke + * @param parameters the parameters to be serialized. Should correspond to the JSON representation. + * @param rawSchema buffer for the schema of a module that contains the contract + * @param schemaVersion the version of the schema provided + * @param verboseErrorMessage Whether errors are in a verbose format or not. Defaults to `false`. + * @returns serialized buffer of update contract parameters + */ +export function serializeUpdateContractParameters( + contractName: string, + receiveFunctionName: string, + // eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/explicit-module-boundary-types + parameters: any, + rawSchema: Buffer, + schemaVersion?: SchemaVersion, + verboseErrorMessage = false +): Buffer { + const serializedParameters = wasm.serializeReceiveContractParameters( + JSON.stringify(parameters), + rawSchema.toString('hex'), + contractName, + receiveFunctionName, + schemaVersion, + verboseErrorMessage + ); + return Buffer.from(serializedParameters, 'hex'); +} + +/** + * Given a value for a smart contract type, and the raw schema for that type, serialize the value into binary format. + * @param value the value that should be serialized. Should correspond to the JSON representation + * @param rawSchema the schema for the type that the given value should be serialized as + * @param verboseErrorMessage Whether errors are in a verbose format or not. Defaults to `false`. + * @returns serialized buffer of the value + */ +export function serializeTypeValue( + // eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/explicit-module-boundary-types + value: any, + rawSchema: Buffer, + verboseErrorMessage = false +): Buffer { + const serializedValue = wasm.serializeTypeValue( + JSON.stringify(value), + rawSchema.toString('hex'), + verboseErrorMessage + ); + return Buffer.from(serializedValue, 'hex'); +} + +export function serializeCredentialDeploymentPayload( + signatures: string[], + credentialDeploymentTransaction: CredentialDeploymentTransaction +): Buffer { + const payloadByteArray = wasm.serializeCredentialDeploymentPayload( + signatures, + JSON.stringify(credentialDeploymentTransaction.unsignedCdi) + ); + return Buffer.from(payloadByteArray); +} From 90b11c9a9274ea1f6c37400d8f70f065e0d264d2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=B8ren=20Bruus=20Zeppelin?= Date: Fri, 4 Aug 2023 13:44:10 +0200 Subject: [PATCH 006/179] Explicitly declare public code --- packages/common/jest.config.js | 2 +- packages/common/package.json | 15 +++---- packages/common/src/index.ts | 42 ++++---------------- packages/common/src/pub/cis0.ts | 1 + packages/common/src/pub/cis2.ts | 1 + packages/common/src/pub/cis4.ts | 1 + packages/common/src/pub/grpc.ts | 1 + packages/common/src/pub/identity.ts | 1 + packages/common/src/pub/index.ts | 34 ++++++++++++++++ packages/common/src/pub/json-rpc.ts | 1 + packages/common/src/pub/wasm.ts | 1 + packages/common/test/HdWallet.test.ts | 2 +- packages/common/test/accountHelpers.test.ts | 2 +- packages/common/test/alias.test.ts | 2 +- packages/common/test/deserialization.test.ts | 2 +- packages/common/test/idProofs.test.ts | 4 +- packages/common/test/schemaHelpers.test.ts | 2 +- packages/common/test/serialization.test.ts | 4 +- packages/nodejs/src/clientV2.ts | 2 +- packages/nodejs/src/index.ts | 3 +- 20 files changed, 70 insertions(+), 53 deletions(-) create mode 100644 packages/common/src/pub/cis0.ts create mode 100644 packages/common/src/pub/cis2.ts create mode 100644 packages/common/src/pub/cis4.ts create mode 100644 packages/common/src/pub/grpc.ts create mode 100644 packages/common/src/pub/identity.ts create mode 100644 packages/common/src/pub/index.ts create mode 100644 packages/common/src/pub/json-rpc.ts create mode 100644 packages/common/src/pub/wasm.ts diff --git a/packages/common/jest.config.js b/packages/common/jest.config.js index 40810b105..ef7d14308 100644 --- a/packages/common/jest.config.js +++ b/packages/common/jest.config.js @@ -1,4 +1,4 @@ -module.exports = { +export default { preset: 'ts-jest', testEnvironment: 'node', moduleFileExtensions: ['js', 'ts', 'json'], diff --git a/packages/common/package.json b/packages/common/package.json index 05289bf50..4606381ff 100644 --- a/packages/common/package.json +++ b/packages/common/package.json @@ -14,13 +14,14 @@ "main": "lib/index.js", "types": "lib/index.d.ts", "exports": { - "./wasm": "./lib/wasm/index.js", - "./cis0": "./lib/cis0.js", - "./cis2": "./lib/cis2/index.js", - "./cis4": "./lib/cis4/index.js", - "./identity": "./lib/identity/index.js", - "./grpc": "./lib/grpc/index.js", - "./json-rpc": "./lib/json-rpc/index.js" + ".": "./lib/pub/index.js", + "./wasm": "./lib/pub/wasm.js", + "./cis0": "./lib/pub/cis0.js", + "./cis2": "./lib/pub/cis2.js", + "./cis4": "./lib/pub/cis4.js", + "./identity": "./lib/pub/identity.js", + "./grpc": "./lib/pub/grpc.js", + "./json-rpc": "./lib/pub/json-rpc.js" }, "files": [ "/grpc/**/*", diff --git a/packages/common/src/index.ts b/packages/common/src/index.ts index 148353cf9..0d4aa72d6 100644 --- a/packages/common/src/index.ts +++ b/packages/common/src/index.ts @@ -1,34 +1,8 @@ -import { sha256 } from './hash'; -export * from './types'; -export { - getAccountTransactionHash, - getAccountTransactionSignDigest, - getCredentialDeploymentSignDigest, - getCredentialForExistingAccountSignDigest, - serializeAccountTransactionForSubmission, - getSignedCredentialDeploymentTransactionHash, - serializeAccountTransactionPayload, -} from './serialization'; -export { sha256 }; -export { CredentialRegistrationId } from './types/CredentialRegistrationId'; -export { AccountAddress } from './types/accountAddress'; -export { CcdAmount } from './types/ccdAmount'; -export { TransactionExpiry } from './types/transactionExpiry'; -export { DataBlob } from './types/DataBlob'; -export { ModuleReference } from './types/moduleReference'; -export * from './credentialDeploymentTransactions'; -export { isAlias, getAlias } from './alias'; -export { deserializeAccountTransaction } from './deserialization'; -export * from './signHelpers'; -export * from './versionedTypeHelpers'; -export * from './accountHelpers'; -export * from './blockSummaryHelpers'; -export * from './rewardStatusHelpers'; - -export { getContractName } from './contractHelpers'; -export { isHex, streamToList, wasmToSchema, unwrap } from './util'; - -export { getAccountTransactionHandler } from './accountTransactions'; -export * from './energyCost'; - -export * from './uleb128'; +export * from './pub'; +export * from './pub/wasm'; +export * from './pub/identity'; +export * from './pub/json-rpc'; +export * from './pub/grpc'; +export * from './pub/cis0'; +export * from './pub/cis2'; +export * from './pub/cis4'; diff --git a/packages/common/src/pub/cis0.ts b/packages/common/src/pub/cis0.ts new file mode 100644 index 000000000..ffeed7d44 --- /dev/null +++ b/packages/common/src/pub/cis0.ts @@ -0,0 +1 @@ +export * from '../cis0'; diff --git a/packages/common/src/pub/cis2.ts b/packages/common/src/pub/cis2.ts new file mode 100644 index 000000000..446ca3073 --- /dev/null +++ b/packages/common/src/pub/cis2.ts @@ -0,0 +1 @@ +export * from '../cis2'; diff --git a/packages/common/src/pub/cis4.ts b/packages/common/src/pub/cis4.ts new file mode 100644 index 000000000..37e1f2a8f --- /dev/null +++ b/packages/common/src/pub/cis4.ts @@ -0,0 +1 @@ +export * from '../cis4'; diff --git a/packages/common/src/pub/grpc.ts b/packages/common/src/pub/grpc.ts new file mode 100644 index 000000000..9bcf615bc --- /dev/null +++ b/packages/common/src/pub/grpc.ts @@ -0,0 +1 @@ +export * from '../grpc'; diff --git a/packages/common/src/pub/identity.ts b/packages/common/src/pub/identity.ts new file mode 100644 index 000000000..5bd19556e --- /dev/null +++ b/packages/common/src/pub/identity.ts @@ -0,0 +1 @@ +export * from '../identity'; diff --git a/packages/common/src/pub/index.ts b/packages/common/src/pub/index.ts new file mode 100644 index 000000000..350ef9a84 --- /dev/null +++ b/packages/common/src/pub/index.ts @@ -0,0 +1,34 @@ +import { sha256 } from '../hash'; +export * from '../types'; +export { + getAccountTransactionHash, + getAccountTransactionSignDigest, + getCredentialDeploymentSignDigest, + getCredentialForExistingAccountSignDigest, + serializeAccountTransactionForSubmission, + getSignedCredentialDeploymentTransactionHash, + serializeAccountTransactionPayload, +} from '../serialization'; +export { sha256 }; +export { CredentialRegistrationId } from '../types/CredentialRegistrationId'; +export { AccountAddress } from '../types/accountAddress'; +export { CcdAmount } from '../types/ccdAmount'; +export { TransactionExpiry } from '../types/transactionExpiry'; +export { DataBlob } from '../types/DataBlob'; +export { ModuleReference } from '../types/moduleReference'; +export * from '../credentialDeploymentTransactions'; +export { isAlias, getAlias } from '../alias'; +export { deserializeAccountTransaction } from '../deserialization'; +export * from '../signHelpers'; +export * from '../versionedTypeHelpers'; +export * from '../accountHelpers'; +export * from '../blockSummaryHelpers'; +export * from '../rewardStatusHelpers'; + +export { getContractName } from '../contractHelpers'; +export { isHex, streamToList, wasmToSchema, unwrap } from '../util'; + +export { getAccountTransactionHandler } from '../accountTransactions'; +export * from '../energyCost'; + +export * from '../uleb128'; diff --git a/packages/common/src/pub/json-rpc.ts b/packages/common/src/pub/json-rpc.ts new file mode 100644 index 000000000..4739e252b --- /dev/null +++ b/packages/common/src/pub/json-rpc.ts @@ -0,0 +1 @@ +export * from '../json-rpc'; diff --git a/packages/common/src/pub/wasm.ts b/packages/common/src/pub/wasm.ts new file mode 100644 index 000000000..c66225e4a --- /dev/null +++ b/packages/common/src/pub/wasm.ts @@ -0,0 +1 @@ +export * from '../wasm'; diff --git a/packages/common/test/HdWallet.test.ts b/packages/common/test/HdWallet.test.ts index 75b4db183..e98f75445 100644 --- a/packages/common/test/HdWallet.test.ts +++ b/packages/common/test/HdWallet.test.ts @@ -1,5 +1,5 @@ import { Buffer } from 'buffer/'; -import { ConcordiumHdWallet } from '../src/HdWallet'; +import { ConcordiumHdWallet } from '../src/wasm/HdWallet'; const TEST_SEED_1 = 'efa5e27326f8fa0902e647b52449bf335b7b605adc387015ec903f41d95080eb71361cbc7fb78721dcd4f3926a337340aa1406df83332c44c1cdcfe100603860'; import * as ed from '@noble/ed25519'; diff --git a/packages/common/test/accountHelpers.test.ts b/packages/common/test/accountHelpers.test.ts index 43c3f730a..837cfa5c7 100644 --- a/packages/common/test/accountHelpers.test.ts +++ b/packages/common/test/accountHelpers.test.ts @@ -1,5 +1,5 @@ import { AccountAddress } from '../src'; -import { generateBakerKeys } from '../src/accountHelpers'; +import { generateBakerKeys } from '../src/wasm/accountHelpers'; test('generate baker keys', () => { const accountAddress = '3eP94feEdmhYiPC1333F9VoV31KGMswonuHk5tqmZrzf761zK5'; diff --git a/packages/common/test/alias.test.ts b/packages/common/test/alias.test.ts index c87d9ed1b..7d771019f 100644 --- a/packages/common/test/alias.test.ts +++ b/packages/common/test/alias.test.ts @@ -1,5 +1,5 @@ import { AccountAddress } from '../src/types/accountAddress'; -import * as bs58check from 'bs58check'; +import bs58check from 'bs58check'; import { getAlias, isAlias } from '../src/alias'; import { Buffer } from 'buffer/'; diff --git a/packages/common/test/deserialization.test.ts b/packages/common/test/deserialization.test.ts index cf42fd25a..04a078262 100644 --- a/packages/common/test/deserialization.test.ts +++ b/packages/common/test/deserialization.test.ts @@ -4,7 +4,7 @@ import { deserializeReceiveReturnValue, deserializeReceiveError, deserializeInitError, -} from '../src/deserialization'; +} from '../src/wasm/deserialization'; import { Buffer } from 'buffer/'; import { serializeAccountTransactionForSubmission } from '../src/serialization'; import { diff --git a/packages/common/test/idProofs.test.ts b/packages/common/test/idProofs.test.ts index d47b0869d..814c7cdb8 100644 --- a/packages/common/test/idProofs.test.ts +++ b/packages/common/test/idProofs.test.ts @@ -4,8 +4,8 @@ import { attributesWithRange, attributesWithSet, RangeStatement, -} from '../src/idProofTypes'; -import { getIdProof, IdStatementBuilder } from '../src/idProofs'; +} from '../src/identity/idProofTypes'; +import { getIdProof, IdStatementBuilder } from '../src/identity/idProofs'; import fs from 'fs'; test('Creating a statement with multiple atomic statements on the same attribute fails', () => { diff --git a/packages/common/test/schemaHelpers.test.ts b/packages/common/test/schemaHelpers.test.ts index 52aaf3a88..84671477a 100644 --- a/packages/common/test/schemaHelpers.test.ts +++ b/packages/common/test/schemaHelpers.test.ts @@ -3,7 +3,7 @@ import { Buffer } from 'buffer/'; import { displayTypeSchemaTemplate, getUpdateContractParameterSchema, -} from '../src/schemaHelpers'; +} from '../src/wasm/schemaHelpers'; test('schema template display', () => { const fullSchema = Buffer.from( diff --git a/packages/common/test/serialization.test.ts b/packages/common/test/serialization.test.ts index 5d4813448..e759b6b98 100644 --- a/packages/common/test/serialization.test.ts +++ b/packages/common/test/serialization.test.ts @@ -5,9 +5,11 @@ import { CcdAmount } from '../src/types/ccdAmount'; import { serializeAccountTransactionForSubmission, serializeAccountTransactionSignature, +} from '../src/serialization'; +import { serializeUpdateContractParameters, serializeTypeValue, -} from '../src/serialization'; +} from '../src/wasm/serialization'; import { AccountTransaction, AccountTransactionHeader, diff --git a/packages/nodejs/src/clientV2.ts b/packages/nodejs/src/clientV2.ts index cb2ac0303..e09417e47 100644 --- a/packages/nodejs/src/clientV2.ts +++ b/packages/nodejs/src/clientV2.ts @@ -1,6 +1,6 @@ import { ChannelCredentials } from '@grpc/grpc-js'; import { GrpcOptions, GrpcTransport } from '@protobuf-ts/grpc-transport'; -import { ConcordiumGRPCClient } from '@concordium/common-sdk/lib/GRPCClient'; +import { ConcordiumGRPCClient } from '@concordium/common-sdk/lib/grpc/GRPCClient'; /** * Initialize a gRPC client for a specific concordium node. diff --git a/packages/nodejs/src/index.ts b/packages/nodejs/src/index.ts index a7f69149c..2d9b47335 100644 --- a/packages/nodejs/src/index.ts +++ b/packages/nodejs/src/index.ts @@ -5,10 +5,9 @@ * */ -import ConcordiumNodeClient from './client'; export * from './clientV2'; -export { ConcordiumNodeClient }; +export { default as ConcordiumNodeClient } from './client'; export { decryptMobileWalletExport, EncryptedData } from './wallet/crypto'; export { MobileWalletExport } from './wallet/types'; export * from '@concordium/common-sdk'; From 0fde1e79d38444987577f6e6df368678cb24dd3a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=B8ren=20Bruus=20Zeppelin?= Date: Mon, 7 Aug 2023 10:21:44 +0200 Subject: [PATCH 007/179] Convert common to hybrid module, publishing both cjs and esm --- .eslintrc.json | 70 ++++++++++++++++++++---- .gitignore | 3 +- packages/common/jest.config.js | 2 +- packages/common/package.json | 72 +++++++++++++++++++------ packages/common/src/grpc/GRPCClient.ts | 4 +- packages/common/src/grpc/translation.ts | 2 +- packages/common/src/index.ts | 1 + packages/common/src/pub/index.ts | 1 - packages/common/src/pub/util.ts | 1 + packages/common/tsconfig.json | 2 +- 10 files changed, 124 insertions(+), 34 deletions(-) create mode 100644 packages/common/src/pub/util.ts diff --git a/.eslintrc.json b/.eslintrc.json index e9cd6365b..f03114f99 100644 --- a/.eslintrc.json +++ b/.eslintrc.json @@ -13,41 +13,91 @@ "parserOptions": { "ecmaVersion": 2020, "sourceType": "module", - "project": [ "./packages/**/tsconfig.eslint.json", "./tsconfig.eslint.json", "./examples/tsconfig.eslint.json"] + "project": [ + "./packages/**/tsconfig.eslint.json", + "./tsconfig.eslint.json", + "./examples/tsconfig.eslint.json" + ] }, "plugins": [ "@typescript-eslint", "import" ], "rules": { - "quotes": [2, "single", { "avoidEscape": true }], - "import/no-unresolved": [2, { "ignore": ["@concordium/rust-bindings", "@concordium/common-sdk", "@concordium/node-sdk", "grpc"]}], - "import/no-extraneous-dependencies": ["error", {"devDependencies": ["**/*.test.ts", "**/*.test.tsx", "**/*.config.js"]}], - "@typescript-eslint/no-unused-vars": ["warn", { "ignoreRestSiblings": true }] + "quotes": [ + 2, + "single", + { + "avoidEscape": true + } + ], + "import/no-unresolved": [ + 2, + { + "ignore": [ + "@concordium/rust-bindings", + "@concordium/common-sdk", + "@concordium/node-sdk", + "grpc" + ] + } + ], + "import/no-extraneous-dependencies": [ + "error", + { + "devDependencies": [ + "**/*.test.ts", + "**/*.test.tsx", + "**/*.config.js" + ] + } + ], + "@typescript-eslint/no-unused-vars": [ + "warn", + { + "ignoreRestSiblings": true + } + ] }, "overrides": [ { - "files": ["*.config.js"], + "files": [ + "*.config.js" + ], "rules": { "@typescript-eslint/no-var-requires": "off", "import/namespace": "off" } } ], - "ignorePatterns": ["**/pkg/**/", "**/lib/**/", "deps/**/*", "**/nodejs/grpc/*", "**/common/grpc/*", "typedoc/**"], + "ignorePatterns": [ + "**/pkg/**/", + "**/lib/**/", + "deps/**/*", + "**/nodejs/grpc-api/*", + "**/common/grpc-api/*", + "typedoc/**" + ], "settings": { "import/ignore": [ "bs58check" ], "import/parsers": { - "@typescript-eslint/parser": [".ts", ".tsx"] + "@typescript-eslint/parser": [ + ".ts", + ".tsx" + ] }, "import/resolver": { "typescript": { - "project": ["packages/*/tsconfig.json"] + "project": [ + "packages/*/tsconfig.json" + ] }, "node": { - "project": ["packages/*/tsconfig.json"] + "project": [ + "packages/*/tsconfig.json" + ] } } } diff --git a/.gitignore b/.gitignore index f6f8e6a4e..15646c335 100644 --- a/.gitignore +++ b/.gitignore @@ -21,8 +21,7 @@ dist doc # Auto generate files from the gRPC proto file -grpc -!packages/common/*/grpc +grpc-api # Webpack cache .webpack-cache diff --git a/packages/common/jest.config.js b/packages/common/jest.config.js index ef7d14308..40810b105 100644 --- a/packages/common/jest.config.js +++ b/packages/common/jest.config.js @@ -1,4 +1,4 @@ -export default { +module.exports = { preset: 'ts-jest', testEnvironment: 'node', moduleFileExtensions: ['js', 'ts', 'json'], diff --git a/packages/common/package.json b/packages/common/package.json index 4606381ff..05b7d2a93 100644 --- a/packages/common/package.json +++ b/packages/common/package.json @@ -10,22 +10,60 @@ "url": "https://github.com/Concordium/concordium-node-sdk-js", "directory": "packages/common" }, - "type": "module", - "main": "lib/index.js", - "types": "lib/index.d.ts", + "main": "lib/cjs/index.js", + "types": "lib/cjs/index.d.ts", + "module": "lib/mjs/index.js", + "browser": "lib/mjs/index.js", "exports": { - ".": "./lib/pub/index.js", - "./wasm": "./lib/pub/wasm.js", - "./cis0": "./lib/pub/cis0.js", - "./cis2": "./lib/pub/cis2.js", - "./cis4": "./lib/pub/cis4.js", - "./identity": "./lib/pub/identity.js", - "./grpc": "./lib/pub/grpc.js", - "./json-rpc": "./lib/pub/json-rpc.js" + ".": { + "types": "./lib/types/pub/index.d.ts", + "import": "./lib/mjs/pub/index.js", + "require": "./lib/cjs/pub/index.js" + }, + "./wasm": { + "types": "./lib/types/pub/wasm.d.ts", + "import": "./lib/mjs/pub/wasm.js", + "require": "./lib/cjs/pub/wasm.js" + }, + "./util": { + "types": "./lib/types/pub/util.d.ts", + "import": "./lib/mjs/pub/util.js", + "require": "./lib/cjs/pub/util.js" + }, + "./cis0": { + "types": "./lib/types/pub/cis0.d.ts", + "import": "./lib/mjs/pub/cis0.js", + "require": "./lib/cjs/pub/cis0.js" + }, + "./cis2": { + "types": "./lib/types/pub/cis2.d.ts", + "import": "./lib/mjs/pub/cis2.js", + "require": "./lib/cjs/pub/cis2.js" + }, + "./cis4": { + "types": "./lib/types/pub/cis4.d.ts", + "import": "./lib/mjs/pub/cis4.js", + "require": "./lib/cjs/pub/cis4.js" + }, + "./identity": { + "types": "./lib/types/pub/identity.d.ts", + "import": "./lib/mjs/pub/identity.js", + "require": "./lib/cjs/pub/identity.js" + }, + "./grpc": { + "types": "./lib/types/pub/grpc.d.ts", + "import": "./lib/mjs/pub/grpc.js", + "require": "./lib/cjs/pub/grpc.js" + }, + "./json-rpc": { + "types": "./lib/types/pub/json-rpc.d.ts", + "import": "./lib/mjs/pub/json-rpc.js", + "require": "./lib/cjs/pub/json-rpc.js" + } }, "files": [ - "/grpc/**/*", - "/lib/**/*" + "/lib/cjs/**/*", + "/lib/mjs/**/*" ], "devDependencies": { "@protobuf-ts/plugin": "2.8.1", @@ -54,14 +92,16 @@ "tabWidth": 4 }, "scripts": { - "generate-ts-v2": "yarn run grpc_tools_node_protoc --plugin=protoc-gen-ts=../../node_modules/@protobuf-ts/plugin/bin/protoc-gen-ts --ts_opt 'optimize_code_size,output_legacy_commonjs,output_javascript' --ts_out=grpc -I ../../deps/concordium-base/concordium-grpc-api ../../deps/concordium-base/concordium-grpc-api/v2/concordium/*.proto", + "generate-ts-v2": "yarn run grpc_tools_node_protoc --plugin=protoc-gen-ts=../../node_modules/@protobuf-ts/plugin/bin/protoc-gen-ts --ts_opt 'optimize_code_size,output_legacy_commonjs,output_javascript' --ts_out=grpc-api -I ../../deps/concordium-base/concordium-grpc-api ../../deps/concordium-base/concordium-grpc-api/v2/concordium/*.proto", "generate": "([ -e \"../../deps/concordium-base/concordium-grpc-api\" ] && yarn generate-ts-v2) || echo 'Please checkout submodules before building'", "lint": "eslint . --cache --ext .ts,.tsx --max-warnings 0", "lint-fix": "yarn --silent lint --fix; exit 0", "test": "jest", - "build": "rm -rf grpc; mkdir -p grpc; yarn generate && tsc", + "tsc:esm": "tsc", + "tsc:cjs": "tsc --outDir ./lib/cjs --module commonjs", + "build": "rm -rf grpc-api; mkdir -p grpc-api; yarn generate && yarn tsc:esm && yarn tsc:cjs && cp -R grpc-api lib", "build-dev": "tsc", - "clean": "rimraf -- lib grpc tsconfig.tsbuildinfo" + "clean": "rimraf -- lib grpc-api tsconfig.tsbuildinfo" }, "dependencies": { "@concordium/rust-bindings": "1.1.0", diff --git a/packages/common/src/grpc/GRPCClient.ts b/packages/common/src/grpc/GRPCClient.ts index bd416f3c4..1ff42bb99 100644 --- a/packages/common/src/grpc/GRPCClient.ts +++ b/packages/common/src/grpc/GRPCClient.ts @@ -8,9 +8,9 @@ import { Buffer } from 'buffer/'; import type { RpcTransport } from '@protobuf-ts/runtime-rpc'; import * as v1 from '../types'; -import * as v2 from '../../grpc/v2/concordium/types'; +import * as v2 from '../../grpc-api/v2/concordium/types'; import { Base58String, HexString, isRpcError } from '../types'; -import { QueriesClient } from '../../grpc/v2/concordium/service.client'; +import { QueriesClient } from '../../grpc-api/v2/concordium/service.client'; import { CredentialRegistrationId } from '../types/CredentialRegistrationId'; import * as translate from './translation'; import { AccountAddress } from '../types/accountAddress'; diff --git a/packages/common/src/grpc/translation.ts b/packages/common/src/grpc/translation.ts index 6756237e4..578bd99ae 100644 --- a/packages/common/src/grpc/translation.ts +++ b/packages/common/src/grpc/translation.ts @@ -2,7 +2,7 @@ import { Buffer } from 'buffer/'; import bs58check from 'bs58check'; import * as v1 from '../types'; -import * as v2 from '../../grpc/v2/concordium/types'; +import * as v2 from '../../grpc-api/v2/concordium/types'; import { mapRecord, unwrap } from '../util'; import { AccountAddress } from '../types/accountAddress'; import { ModuleReference } from '../types/moduleReference'; diff --git a/packages/common/src/index.ts b/packages/common/src/index.ts index 0d4aa72d6..7e2b80a20 100644 --- a/packages/common/src/index.ts +++ b/packages/common/src/index.ts @@ -1,4 +1,5 @@ export * from './pub'; +export * from './pub/util'; export * from './pub/wasm'; export * from './pub/identity'; export * from './pub/json-rpc'; diff --git a/packages/common/src/pub/index.ts b/packages/common/src/pub/index.ts index 350ef9a84..48c447f4c 100644 --- a/packages/common/src/pub/index.ts +++ b/packages/common/src/pub/index.ts @@ -26,7 +26,6 @@ export * from '../blockSummaryHelpers'; export * from '../rewardStatusHelpers'; export { getContractName } from '../contractHelpers'; -export { isHex, streamToList, wasmToSchema, unwrap } from '../util'; export { getAccountTransactionHandler } from '../accountTransactions'; export * from '../energyCost'; diff --git a/packages/common/src/pub/util.ts b/packages/common/src/pub/util.ts new file mode 100644 index 000000000..4bda8bb2a --- /dev/null +++ b/packages/common/src/pub/util.ts @@ -0,0 +1 @@ +export * from '../util'; diff --git a/packages/common/tsconfig.json b/packages/common/tsconfig.json index d560719fc..db1fe19cf 100644 --- a/packages/common/tsconfig.json +++ b/packages/common/tsconfig.json @@ -6,6 +6,6 @@ ], "compilerOptions": { "rootDir": "./src", - "outDir": "./lib" + "outDir": "./lib/mjs" } } From 69f3107cbf1a7f28793fa248da118106b65d605d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=B8ren=20Bruus=20Zeppelin?= Date: Mon, 7 Aug 2023 10:42:47 +0200 Subject: [PATCH 008/179] Adapt nodejs package to common hybrid --- packages/nodejs/jest.config.js | 2 +- packages/nodejs/package.json | 8 ++++---- packages/nodejs/src/client.ts | 8 +++----- packages/nodejs/src/clientV2.ts | 2 +- packages/nodejs/src/util.ts | 2 +- packages/nodejs/test/clientV2.test.ts | 6 +++--- packages/nodejs/test/credentialDeployment.test.ts | 2 +- packages/nodejs/test/manualTests.test.ts | 2 +- packages/nodejs/tsconfig.jest.json | 7 ------- 9 files changed, 15 insertions(+), 24 deletions(-) delete mode 100644 packages/nodejs/tsconfig.jest.json diff --git a/packages/nodejs/jest.config.js b/packages/nodejs/jest.config.js index efc8bc73b..40810b105 100644 --- a/packages/nodejs/jest.config.js +++ b/packages/nodejs/jest.config.js @@ -5,7 +5,7 @@ module.exports = { moduleDirectories: ['node_modules'], globals: { 'ts-jest': { - tsconfig: 'tsconfig.jest.json', + tsconfig: 'tsconfig.json', }, }, }; diff --git a/packages/nodejs/package.json b/packages/nodejs/package.json index f5bd8d0f9..d027d2622 100644 --- a/packages/nodejs/package.json +++ b/packages/nodejs/package.json @@ -51,15 +51,15 @@ "tabWidth": 4 }, "scripts": { - "generate-js": "yarn run grpc_tools_node_protoc --js_out=import_style=commonjs,binary:grpc --grpc_out=grpc_js:grpc --plugin=protoc-gen-grpc=../../node_modules/.bin/grpc_tools_node_protoc_plugin -I ../../deps/concordium-base/concordium-grpc-api ../../deps/concordium-base/concordium-grpc-api/**/*.proto", - "generate-ts": "yarn run grpc_tools_node_protoc --plugin=protoc-gen-ts=../../node_modules/grpc_tools_node_protoc_ts/bin/protoc-gen-ts --ts_out=grpc_js:grpc -I ../../deps/concordium-base/concordium-grpc-api ../../deps/concordium-base/concordium-grpc-api/concordium_p2p_rpc.proto", + "generate-js": "yarn run grpc_tools_node_protoc --js_out=import_style=commonjs,binary:grpc-api --grpc_out=grpc_js:grpc-api --plugin=protoc-gen-grpc=../../node_modules/.bin/grpc_tools_node_protoc_plugin -I ../../deps/concordium-base/concordium-grpc-api ../../deps/concordium-base/concordium-grpc-api/**/*.proto", + "generate-ts": "yarn run grpc_tools_node_protoc --plugin=protoc-gen-ts=../../node_modules/grpc_tools_node_protoc_ts/bin/protoc-gen-ts --ts_out=grpc_js:grpc-api -I ../../deps/concordium-base/concordium-grpc-api ../../deps/concordium-base/concordium-grpc-api/concordium_p2p_rpc.proto", "generate": "([ -e \"../../deps/concordium-base/concordium-grpc-api\" ] && yarn generate-js && yarn generate-ts) || echo 'Please checkout submodules before building'", "lint": "eslint . --cache --ext .ts,.tsx --max-warnings 0", "lint-fix": "yarn --silent lint --fix; exit 0", "test": "jest", - "build": "rm -rf grpc; mkdir -p grpc; yarn generate && tsc", + "build": "rm -rf grpc-api; mkdir -p grpc-api; yarn generate && tsc", "build-dev": "tsc", - "clean": "rimraf -- lib grpc tsconfig.tsbuildinfo" + "clean": "rimraf -- lib grpc-api tsconfig.tsbuildinfo" }, "dependencies": { "@concordium/common-sdk": "9.0.0", diff --git a/packages/nodejs/src/client.ts b/packages/nodejs/src/client.ts index a814c5c5c..4404b3956 100644 --- a/packages/nodejs/src/client.ts +++ b/packages/nodejs/src/client.ts @@ -1,6 +1,6 @@ import { ChannelCredentials, Metadata, ServiceError } from '@grpc/grpc-js'; import { Buffer as BufferFormater } from 'buffer/'; -import { P2PClient } from '../grpc/concordium_p2p_rpc_grpc_pb'; +import { P2PClient } from '../grpc-api/concordium_p2p_rpc_grpc_pb'; import { AccountAddress, BlockHash, @@ -14,7 +14,7 @@ import { SendTransactionRequest, TransactionHash, InvokeContractRequest, -} from '../grpc/concordium_p2p_rpc_pb'; +} from '../grpc-api/concordium_p2p_rpc_pb'; import { serializeAccountTransactionForSubmission, serializeCredentialDeploymentTransactionForSubmission, @@ -81,13 +81,11 @@ import { TimeoutParameters, Ratio, ConcordiumBftStatus, -} from '@concordium/common-sdk'; -import { buildJsonResponseReviver, intToStringTransformer, isValidHash, stringToInt, -} from '@concordium/common-sdk/lib/util'; +} from '@concordium/common-sdk'; import { intListToStringList, unwrapBoolResponse, diff --git a/packages/nodejs/src/clientV2.ts b/packages/nodejs/src/clientV2.ts index e09417e47..477ddad5c 100644 --- a/packages/nodejs/src/clientV2.ts +++ b/packages/nodejs/src/clientV2.ts @@ -1,6 +1,6 @@ import { ChannelCredentials } from '@grpc/grpc-js'; import { GrpcOptions, GrpcTransport } from '@protobuf-ts/grpc-transport'; -import { ConcordiumGRPCClient } from '@concordium/common-sdk/lib/grpc/GRPCClient'; +import { ConcordiumGRPCClient } from '@concordium/common-sdk'; /** * Initialize a gRPC client for a specific concordium node. diff --git a/packages/nodejs/src/util.ts b/packages/nodejs/src/util.ts index 562471977..7120bbae1 100644 --- a/packages/nodejs/src/util.ts +++ b/packages/nodejs/src/util.ts @@ -1,6 +1,6 @@ import * as fs from 'fs'; import { Buffer } from 'buffer/'; -import { BoolResponse, JsonResponse } from '../grpc/concordium_p2p_rpc_pb'; +import { BoolResponse, JsonResponse } from '../grpc-api/concordium_p2p_rpc_pb'; /** * @deprecated This is a helper function for the v1 gRPC client, which has been deprecated diff --git a/packages/nodejs/test/clientV2.test.ts b/packages/nodejs/test/clientV2.test.ts index 0d8319fdd..c6c33485c 100644 --- a/packages/nodejs/test/clientV2.test.ts +++ b/packages/nodejs/test/clientV2.test.ts @@ -1,11 +1,11 @@ import * as v1 from '@concordium/common-sdk'; -import * as v2 from '../../common/grpc/v2/concordium/types'; +import * as v2 from '../../common/grpc-api/v2/concordium/types'; import { testnetBulletproofGenerators } from './resources/bulletproofgenerators'; import { ConcordiumGRPCClient, getAccountIdentifierInput, getBlockHashInput, -} from '@concordium/common-sdk/lib/GRPCClient'; +} from '@concordium/common-sdk/lib/cjs/grpc/GRPCClient'; import { buildBasicAccountSigner, calculateEnergyCost, @@ -28,7 +28,7 @@ import * as ed from '@noble/ed25519'; import * as expected from './resources/expectedJsons'; import { Buffer } from 'buffer/'; -import { serializeAccountTransaction } from '@concordium/common-sdk/lib/serialization'; +import { serializeAccountTransaction } from '@concordium/common-sdk/lib/cjs/serialization'; import { TextEncoder, TextDecoder } from 'util'; diff --git a/packages/nodejs/test/credentialDeployment.test.ts b/packages/nodejs/test/credentialDeployment.test.ts index 8f52ac7c8..01b031e3e 100644 --- a/packages/nodejs/test/credentialDeployment.test.ts +++ b/packages/nodejs/test/credentialDeployment.test.ts @@ -13,7 +13,7 @@ import { import fs from 'fs'; import * as ed from '@noble/ed25519'; import { Buffer } from 'buffer/'; -import { deserializeTransaction } from '@concordium/common-sdk/lib/deserialization'; +import { deserializeTransaction } from '@concordium/common-sdk/lib/cjs/wasm/deserialization'; test('test deserialize credentialDeployment ', async () => { const identityInput: IdentityInput = getIdentityInput(); diff --git a/packages/nodejs/test/manualTests.test.ts b/packages/nodejs/test/manualTests.test.ts index cb2295935..4cadb415e 100644 --- a/packages/nodejs/test/manualTests.test.ts +++ b/packages/nodejs/test/manualTests.test.ts @@ -1,4 +1,4 @@ -import * as v2 from '../../common/grpc/v2/concordium/types'; +import * as v2 from '../../common/lib/grpc-api/v2/concordium/types'; import * as v1 from '@concordium/common-sdk'; import { buildBasicAccountSigner, diff --git a/packages/nodejs/tsconfig.jest.json b/packages/nodejs/tsconfig.jest.json deleted file mode 100644 index d3ce9180d..000000000 --- a/packages/nodejs/tsconfig.jest.json +++ /dev/null @@ -1,7 +0,0 @@ -{ - "extends": "./tsconfig", - "compilerOptions": { - "module": "CommonJS" - }, - "exclude": ["lib/pkg/**/*"] -} From b070276a4eb539a5266a2f88efb3e0beca7ca570 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=B8ren=20Bruus=20Zeppelin?= Date: Mon, 7 Aug 2023 11:17:03 +0200 Subject: [PATCH 009/179] Add ESM target for web --- packages/common/package.json | 43 +++++++++++++++++----------------- packages/common/tsconfig.json | 2 +- packages/web/package.json | 4 ++-- packages/web/tsconfig.json | 12 ++++++---- packages/web/webpack.config.js | 2 +- 5 files changed, 34 insertions(+), 29 deletions(-) diff --git a/packages/common/package.json b/packages/common/package.json index 05b7d2a93..6dc14e6b3 100644 --- a/packages/common/package.json +++ b/packages/common/package.json @@ -12,58 +12,59 @@ }, "main": "lib/cjs/index.js", "types": "lib/cjs/index.d.ts", - "module": "lib/mjs/index.js", - "browser": "lib/mjs/index.js", + "module": "lib/esm/index.js", + "browser": "lib/esm/index.js", "exports": { ".": { - "types": "./lib/types/pub/index.d.ts", - "import": "./lib/mjs/pub/index.js", + "types": "./lib/cjs/pub/index.d.ts", + "import": "./lib/esm/pub/index.js", "require": "./lib/cjs/pub/index.js" }, "./wasm": { - "types": "./lib/types/pub/wasm.d.ts", - "import": "./lib/mjs/pub/wasm.js", + "types": "./lib/cjs/pub/wasm.d.ts", + "import": "./lib/esm/pub/wasm.js", "require": "./lib/cjs/pub/wasm.js" }, "./util": { - "types": "./lib/types/pub/util.d.ts", - "import": "./lib/mjs/pub/util.js", + "types": "./lib/cjs/pub/util.d.ts", + "import": "./lib/esm/pub/util.js", "require": "./lib/cjs/pub/util.js" }, "./cis0": { - "types": "./lib/types/pub/cis0.d.ts", - "import": "./lib/mjs/pub/cis0.js", + "types": "./lib/cjs/pub/cis0.d.ts", + "import": "./lib/esm/pub/cis0.js", "require": "./lib/cjs/pub/cis0.js" }, "./cis2": { - "types": "./lib/types/pub/cis2.d.ts", - "import": "./lib/mjs/pub/cis2.js", + "types": "./lib/cjs/pub/cis2.d.ts", + "import": "./lib/esm/pub/cis2.js", "require": "./lib/cjs/pub/cis2.js" }, "./cis4": { - "types": "./lib/types/pub/cis4.d.ts", - "import": "./lib/mjs/pub/cis4.js", + "types": "./lib/cjs/pub/cis4.d.ts", + "import": "./lib/esm/pub/cis4.js", "require": "./lib/cjs/pub/cis4.js" }, "./identity": { - "types": "./lib/types/pub/identity.d.ts", - "import": "./lib/mjs/pub/identity.js", + "types": "./lib/cjs/pub/identity.d.ts", + "import": "./lib/esm/pub/identity.js", "require": "./lib/cjs/pub/identity.js" }, "./grpc": { - "types": "./lib/types/pub/grpc.d.ts", - "import": "./lib/mjs/pub/grpc.js", + "types": "./lib/cjs/pub/grpc.d.ts", + "import": "./lib/esm/pub/grpc.js", "require": "./lib/cjs/pub/grpc.js" }, "./json-rpc": { - "types": "./lib/types/pub/json-rpc.d.ts", - "import": "./lib/mjs/pub/json-rpc.js", + "types": "./lib/cjs/pub/json-rpc.d.ts", + "import": "./lib/esm/pub/json-rpc.js", "require": "./lib/cjs/pub/json-rpc.js" } }, "files": [ "/lib/cjs/**/*", - "/lib/mjs/**/*" + "/lib/esm/**/*", + "/lib/grpc-api/**/*" ], "devDependencies": { "@protobuf-ts/plugin": "2.8.1", diff --git a/packages/common/tsconfig.json b/packages/common/tsconfig.json index db1fe19cf..fdc46f332 100644 --- a/packages/common/tsconfig.json +++ b/packages/common/tsconfig.json @@ -6,6 +6,6 @@ ], "compilerOptions": { "rootDir": "./src", - "outDir": "./lib/mjs" + "outDir": "./lib/esm" } } diff --git a/packages/web/package.json b/packages/web/package.json index 97b45156a..e205aaaa1 100644 --- a/packages/web/package.json +++ b/packages/web/package.json @@ -2,8 +2,8 @@ "name": "@concordium/web-sdk", "version": "6.0.0", "license": "Apache-2.0", - "browser": "lib/mjs/index.js", - "types": "lib/types/index.d.ts", + "browser": "lib/esm/index.js", + "types": "lib/esm/index.d.ts", "files": [ "lib/**/*" ], diff --git a/packages/web/tsconfig.json b/packages/web/tsconfig.json index 62faa3571..d743a80bb 100644 --- a/packages/web/tsconfig.json +++ b/packages/web/tsconfig.json @@ -1,15 +1,19 @@ { "extends": "../../tsconfig-base.json", - "include": ["src/**/*"], + "include": [ + "src/**/*" + ], "references": [ { "path": "../common" } ], "compilerOptions": { - "lib": ["ES2020", "dom"], + "lib": [ + "ES2020", + "dom" + ], "rootDir": "./src", - "outDir": "./lib/mjs", - "declarationDir": "./lib/types" + "outDir": "./lib/esm" } } diff --git a/packages/web/webpack.config.js b/packages/web/webpack.config.js index c78263c4c..132617c55 100644 --- a/packages/web/webpack.config.js +++ b/packages/web/webpack.config.js @@ -12,7 +12,7 @@ export default { cacheDirectory: path.resolve(__dirname, '.webpack-cache'), }, entry: { - concordium: './lib/mjs/index.js', + concordium: './lib/esm/index.js', }, plugins: [ new webpack.SourceMapDevToolPlugin({ From 4e75596a49d6294a69bd83468e3d4f8566f2d1a1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=B8ren=20Bruus=20Zeppelin?= Date: Mon, 7 Aug 2023 14:00:15 +0200 Subject: [PATCH 010/179] Fix html examples sdk import --- packages/web/example/Alias.html | 2 +- packages/web/example/GetAccountInfo.html | 2 +- packages/web/example/GetCryptographicParameters.html | 2 +- packages/web/example/GetInstanceInfo.html | 2 +- packages/web/example/GetModuleSource.html | 2 +- packages/web/example/GetNonce.html | 2 +- packages/web/example/GetTransactionStatus.html | 2 +- packages/web/example/InvokeContract.html | 2 +- packages/web/example/SendCIS2Transfer.html | 2 +- packages/web/example/SendCIS2UpdateOperator.html | 2 +- packages/web/example/SendTransaction.html | 2 +- 11 files changed, 11 insertions(+), 11 deletions(-) diff --git a/packages/web/example/Alias.html b/packages/web/example/Alias.html index a25a41bc3..8f2d4a36c 100644 --- a/packages/web/example/Alias.html +++ b/packages/web/example/Alias.html @@ -3,7 +3,7 @@ - + + + + + + + + + + + - - - - -
- -
-