Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/release/10' into ccd-js-gen-sche…
Browse files Browse the repository at this point in the history
…ma-param
  • Loading branch information
limemloh committed Oct 12, 2023
2 parents eac464b + 9982006 commit 0c86233
Show file tree
Hide file tree
Showing 51 changed files with 1,786 additions and 347 deletions.
6 changes: 5 additions & 1 deletion .eslintrc.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,11 @@ module.exports = {
'import/no-unresolved': [
2,
{
ignore: ['@concordium/rust-bindings', 'grpc-api'],
ignore: [
'@concordium/rust-bindings',
'grpc-api',
'^#.+$', // ESLint resolver does not support subpath imports: https://github.com/import-js/eslint-plugin-import/issues/1868.
],
},
],
'import/no-extraneous-dependencies': [
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/pipeline.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ on:


env:
DUMMY: 1 # For cache busting.
DUMMY: 2 # For cache busting.
NODE_VERSION: 18.16.0
RUST_VERSION: 1.65
RUST_FMT: nightly-2023-04-01-x86_64-unknown-linux-gnu
Expand Down
2 changes: 1 addition & 1 deletion examples/cis2/dryRun.transfer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ const client = new ConcordiumGRPCNodeClient(
const tokenId = cli.flags.tokenId;
const from = AccountAddress.fromBase58(cli.flags.from);
const toAddress = parseAddress(cli.flags.to);
const to: CIS2.Receiver = AccountAddress.isAccountAddress(toAddress)
const to: CIS2.Receiver = AccountAddress.instanceOf(toAddress)
? toAddress
: {
address: toAddress,
Expand Down
2 changes: 1 addition & 1 deletion examples/cis2/transfer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ const client = new ConcordiumGRPCNodeClient(
const tokenId = cli.flags.tokenId;
const from = AccountAddress.fromBase58(cli.flags.from);
const toAddress = parseAddress(cli.flags.to);
const to: CIS2.Receiver = AccountAddress.isAccountAddress(toAddress)
const to: CIS2.Receiver = AccountAddress.instanceOf(toAddress)
? toAddress
: {
address: toAddress,
Expand Down
4 changes: 2 additions & 2 deletions examples/cis4/registerCredential.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import fs from 'fs';
import path from 'path';
import meow from 'meow';
import { credentials } from '@grpc/grpc-js';
import * as ed25519 from '@noble/ed25519';
import * as ed25519 from '#ed25519';

import {
AccountAddress,
Expand Down Expand Up @@ -104,7 +104,7 @@ const signer = buildAccountSigner(wallet);
let holderPubKey: HexString;
if (!cli.flags.holderPubKey) {
const prv = ed25519.utils.randomPrivateKey();
const pub = Buffer.from(await ed25519.getPublicKey(prv)).toString(
const pub = Buffer.from(await ed25519.getPublicKeyAsync(prv)).toString(
'hex'
);

Expand Down
4 changes: 2 additions & 2 deletions examples/cis4/registerRevocationKeys.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import fs from 'fs';
import path from 'path';
import meow from 'meow';
import * as ed25519 from '@noble/ed25519';
import * as ed25519 from '#ed25519';
import { credentials } from '@grpc/grpc-js';

import {
Expand Down Expand Up @@ -88,7 +88,7 @@ const signer = buildAccountSigner(wallet);
let keys: HexString[] = cli.flags.keys ?? [];
if (!cli.flags.keys?.length) {
const prv = ed25519.utils.randomPrivateKey();
const pub = Buffer.from(await ed25519.getPublicKey(prv)).toString(
const pub = Buffer.from(await ed25519.getPublicKeyAsync(prv)).toString(
'hex'
);

Expand Down
6 changes: 3 additions & 3 deletions examples/client/getModuleList.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { parseEndpoint } from '../shared/util.js';
import { BlockHash, HexString } from '@concordium/web-sdk';
import { BlockHash, ModuleReference } from '@concordium/web-sdk';
import { ConcordiumGRPCNodeClient } from '@concordium/web-sdk/nodejs';
import { credentials } from '@grpc/grpc-js';

Expand Down Expand Up @@ -54,12 +54,12 @@ const client = new ConcordiumGRPCNodeClient(
cli.flags.block === undefined
? undefined
: BlockHash.fromHexString(cli.flags.block);
const moduleRefs: AsyncIterable<HexString> =
const moduleRefs: AsyncIterable<ModuleReference.Type> =
client.getModuleList(blockHash);
// #endregion documentation-snippet

// Prints module references
for await (const moduleRef of moduleRefs) {
console.log(moduleRef);
console.log(ModuleReference.toHexString(moduleRef));
}
})();
9 changes: 8 additions & 1 deletion examples/package.json
Original file line number Diff line number Diff line change
@@ -1,16 +1,23 @@
{
"name": "@concordium/examples",
"type": "module",
"imports": {
"#ed25519": {
"node": "./shims/ed25519.node.ts",
"default": "@noble/ed25519"
}
},
"dependencies": {
"@concordium/ccd-js-gen": "workspace:^",
"@concordium/web-sdk": "workspace:^",
"@grpc/grpc-js": "^1.3.4",
"@noble/ed25519": "^1.7.1",
"@noble/ed25519": "^2.0.0",
"buffer": "^6.0.3",
"meow": "11.0",
"node-fetch": "^3.3.2"
},
"devDependencies": {
"eslint": "^8.50.0",
"ts-node": "10.9",
"typescript": "^5.2.2"
},
Expand Down
9 changes: 9 additions & 0 deletions examples/shims/ed25519.node.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// To add support for node versions <19.
// From https://www.npmjs.com/package/@noble/ed25519#usage
import { webcrypto } from 'node:crypto';

// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
if (!globalThis.crypto) globalThis.crypto = webcrypto;

export * from '@noble/ed25519';
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
"devDependencies": {
"@typescript-eslint/eslint-plugin": "^6.7.0",
"@typescript-eslint/parser": "^6.7.0",
"eslint": "^7.29.0",
"eslint": "^8.51.0",
"eslint-config-prettier": "^8.3.0",
"eslint-import-resolver-exports": "^1.0.0-beta.5",
"eslint-import-resolver-typescript": "^2.7.1",
Expand Down
2 changes: 2 additions & 0 deletions packages/ccd-js-gen/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,15 @@
"@concordium/web-sdk": "6.x"
},
"dependencies": {
"@concordium/web-sdk": "6.x",
"buffer": "^6.0.3",
"commander": "^11.0.0",
"sanitize-filename": "^1.6.3",
"ts-morph": "^19.0.0"
},
"devDependencies": {
"@types/node": "^20.5.0",
"eslint": "^8.50.0",
"typescript": "^5.2.2"
},
"prettier": {
Expand Down
5 changes: 4 additions & 1 deletion packages/rust-bindings/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,12 @@
"build-bundler": "wasm-pack build ./packages/dapp --target bundler --out-dir $INIT_CWD/lib/dapp/bundler --out-name index \"$@\" && wasm-pack build ./packages/wallet --target bundler --out-dir $INIT_CWD/lib/wallet/bundler --out-name index \"$@\"",
"build": "yarn build-node \"$@\" && yarn build-bundler \"$@\" && yarn build-web \"$@\"",
"build:rust-bindings": "yarn build",
"clean": "rimraf -- target lib .webpack-cache"
"clean": "rimraf -- target lib .webpack-cache",
"lint": "eslint . --cache --ext .ts,.tsx --max-warnings 0",
"lint-fix": "yarn --silent lint --fix; exit 0"
},
"devDependencies": {
"eslint": "^8.50.0",
"rimraf": "^5.0.1",
"ts-loader": "^9.4.4",
"typescript": "^5.2.2",
Expand Down
4 changes: 4 additions & 0 deletions packages/sdk/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
- `deserializeTypeValue`

The API now uses dedicated types instead of language primitives:

- Use `AccountAddress` instead of a string with base58 encoding. Use `AccountAddress.fromBase58('<base58>')` to construct it.
- Use `BlockHash` instead of a string with hex encoding. Use `BlockHash.fromHexString('<hex>')` to construct it.
- Use `TranactionHash` instead of a string with hex encoding. Use `TransactionHash.fromHexString('<hex>')` to construct it.
Expand All @@ -48,6 +49,7 @@ The API now uses dedicated types instead of language primitives:
- Use `ModuleReference` instead of a string with hex encoding. Can be constructed using `ModuleReference.fromHexString('<hex-string>')`.

Several types have been replaced with a module containing the type itself together with functions for constructing and converting the type:

- `AccountAddress` is now a module with functions related to account addresses:
- To refer to `AccountAddress` as a type use `AccountAddress.Type`.
- Constructing `new AccountAddress("<address>")` is now `AccountAddress.fromBase58("<address>")`.
Expand Down Expand Up @@ -82,6 +84,8 @@ Several types have been replaced with a module containing the type itself togeth
- `EntrypointName` is now a module with functions related to entrypoint names of a smart contract.
- `ReceiveName` is now a module with functions related to receive-function names of a smart contract.
- `ReturnValue` is now a module with functions related to return values from invoking a smart contract.
- Functions `jsonStringify` and `jsonParse`, which acts as a regular `JSON.stringify` and `JSON.parse` correspondingly,
with the addition of stringifying concordium domain types in a wrapper object that can be parsed into the respective types.

### Changes

Expand Down
3 changes: 3 additions & 0 deletions packages/sdk/jest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ const config: Config = {
moduleNameMapper: {
'^(\\.\\.?\\/.+)\\.js$': '$1', // Remap esmodule file extensions
},
transformIgnorePatterns: [
'node_modules/(?!@noble/ed25519)', // @noble/ed25519 is an ES module only
],
transform: {
'^.+\\.[jt]sx?$': [
'ts-jest',
Expand Down
9 changes: 8 additions & 1 deletion packages/sdk/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,12 @@
"default": "./lib/esm/pub/*.js"
}
},
"imports": {
"#ed25519": {
"node": "./src/shims/ed25519.node.ts",
"default": "@noble/ed25519"
}
},
"files": [
"/src/**/*",
"/lib/esm/**/*",
Expand All @@ -45,6 +51,7 @@
"@types/jest": "^26.0.23",
"@types/json-bigint": "^1.0.1",
"@types/uuid": "^8.3.4",
"eslint": "^8.51.0",
"glob": "^10.3.10",
"grpc-tools": "^1.11.2",
"grpc_tools_node_protoc_ts": "5.3.0",
Expand All @@ -70,7 +77,7 @@
"dependencies": {
"@concordium/rust-bindings": "1.2.0",
"@grpc/grpc-js": "^1.9.4",
"@noble/ed25519": "^1.7.1",
"@noble/ed25519": "^2.0.0",
"@protobuf-ts/grpc-transport": "^2.9.1",
"@protobuf-ts/grpcweb-transport": "^2.9.1",
"@protobuf-ts/runtime-rpc": "^2.8.2",
Expand Down
4 changes: 2 additions & 2 deletions packages/sdk/src/GenericContract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,8 +151,8 @@ export class ContractDryRun<E extends string = string> {
): Promise<InvokeContractResult> {
const parameter = Parameter.fromBuffer(serializer(input));
const meta =
AccountAddress.isAccountAddress(metaOrInvoker) ||
ContractAddress.isContractAddress(metaOrInvoker)
AccountAddress.instanceOf(metaOrInvoker) ||
ContractAddress.instanceOf(metaOrInvoker)
? { invoker: metaOrInvoker }
: metaOrInvoker;
return this.grpcClient.invokeContract(
Expand Down
12 changes: 5 additions & 7 deletions packages/sdk/src/cis2/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ export function serializeContractAddress(

function serializeAddress(address: CIS2.Address): Buffer {
return Buffer.concat(
ContractAddress.isContractAddress(address)
ContractAddress.instanceOf(address)
? [encodeWord8(1), serializeContractAddress(address)]
: [encodeWord8(0), serializeAccountAddress(address)]
);
Expand Down Expand Up @@ -272,7 +272,7 @@ function serializeContractReceiver(receiver: CIS2.ContractReceiver): Buffer {

function serializeReceiver(receiver: CIS2.Receiver): Buffer {
return Buffer.concat(
AccountAddress.isAccountAddress(receiver)
AccountAddress.instanceOf(receiver)
? [encodeWord8(0), AccountAddress.toBuffer(receiver)]
: [encodeWord8(1), serializeContractReceiver(receiver)]
);
Expand Down Expand Up @@ -556,7 +556,7 @@ export function formatCIS2UpdateOperator(
): CIS2.UpdateOperatorParamJson {
return {
update: input.type === 'add' ? { Add: {} } : { Remove: {} },
operator: ContractAddress.isContractAddress(input.address)
operator: ContractAddress.instanceOf(input.address)
? {
Contract: [
{
Expand All @@ -575,9 +575,7 @@ export function formatCIS2UpdateOperator(
export function formatCIS2Transfer(
input: CIS2.Transfer
): CIS2.TransferParamJson {
const from: CIS2.AddressParamJson = ContractAddress.isContractAddress(
input.from
)
const from: CIS2.AddressParamJson = ContractAddress.instanceOf(input.from)
? {
Contract: [
{
Expand All @@ -588,7 +586,7 @@ export function formatCIS2Transfer(
}
: { Account: [AccountAddress.toBase58(input.from)] };
let to: CIS2.ReceiverParamJson;
if (AccountAddress.isAccountAddress(input.to)) {
if (AccountAddress.instanceOf(input.to)) {
to = { Account: [AccountAddress.toBase58(input.to)] };
} else {
to = {
Expand Down
4 changes: 2 additions & 2 deletions packages/sdk/src/cis4/util.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Buffer } from 'buffer/index.js';
import { getPublicKey } from '@noble/ed25519';
import * as ed from '#ed25519';

import type { HexString } from '../types.js';
import type { CIS2 } from '../cis2/util.js';
Expand Down Expand Up @@ -287,7 +287,7 @@ export class Web3IdSigner {
*/
public static async from(privateKey: HexString): Promise<Web3IdSigner> {
const publicKey = Buffer.from(
await getPublicKey(Buffer.from(privateKey, 'hex'))
await ed.getPublicKeyAsync(Buffer.from(privateKey, 'hex'))
).toString('hex');
return new Web3IdSigner(privateKey, publicKey);
}
Expand Down
16 changes: 8 additions & 8 deletions packages/sdk/src/grpc/GRPCClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -671,7 +671,7 @@ export class ConcordiumGRPCClient {
*
* @param blockHash an optional block hash to get the accounts at, otherwise retrieves from last finalized block.
* @param abortSignal an optional AbortSignal to close the stream.
* @returns an async iterable of account addresses represented as Base58 encoded strings.
* @returns an async iterable of account addresses.
*/
getAccountList(
blockHash?: BlockHash.Type,
Expand All @@ -692,16 +692,16 @@ export class ConcordiumGRPCClient {
*
* @param blockHash an optional block hash to get the contract modules at, otherwise retrieves from last finalized block.
* @param abortSignal an optional AbortSignal to close the stream.
* @returns an async iterable of contract module references, represented as hex strings.
* @returns an async iterable of contract module references.
*/
getModuleList(
blockHash?: BlockHash.Type,
abortSignal?: AbortSignal
): AsyncIterable<HexString> {
): AsyncIterable<ModuleReference.Type> {
const opts = { abort: abortSignal };
const hash = getBlockHashInput(blockHash);
const asyncIter = this.client.getModuleList(hash, opts).responses;
return mapStream(asyncIter, translate.unwrapValToHex);
return mapStream(asyncIter, ModuleReference.fromProto);
}

/**
Expand All @@ -714,7 +714,7 @@ export class ConcordiumGRPCClient {
* @param maxAmountOfAncestors the maximum amount of ancestors as a bigint.
* @param blockHash a optional block hash to get the ancestors at, otherwise retrieves from last finalized block.
* @param abortSignal an optional AbortSignal to close the stream.
* @returns an async iterable of ancestors' block hashes as hex strings.
* @returns an async iterable of ancestors' block hashes.
*/
getAncestors(
maxAmountOfAncestors: bigint,
Expand Down Expand Up @@ -1569,7 +1569,7 @@ export function getAccountIdentifierInput(
): v2.AccountIdentifierInput {
let returnIdentifier: v2.AccountIdentifierInput['accountIdentifierInput'];

if (AccountAddress.isAccountAddress(accountIdentifier)) {
if (AccountAddress.instanceOf(accountIdentifier)) {
returnIdentifier = {
oneofKind: 'address',
address: AccountAddress.toProto(accountIdentifier),
Expand Down Expand Up @@ -1627,14 +1627,14 @@ export function getInvokerInput(
): v2.Address | undefined {
if (!invoker) {
return undefined;
} else if (AccountAddress.isAccountAddress(invoker)) {
} else if (AccountAddress.instanceOf(invoker)) {
return {
type: {
oneofKind: 'account',
account: AccountAddress.toProto(invoker),
},
};
} else if (ContractAddress.isContractAddress(invoker)) {
} else if (ContractAddress.instanceOf(invoker)) {
return {
type: {
oneofKind: 'contract',
Expand Down
6 changes: 6 additions & 0 deletions packages/sdk/src/pub/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,12 @@ import * as ContractEvent from '../types/ContractEvent.js';
import * as CcdAmount from '../types/CcdAmount.js';
import * as TransactionExpiry from '../types/TransactionExpiry.js';
import * as ModuleReference from '../types/ModuleReference.js';
export {
TypedJsonParseError,
TypedJsonParseErrorCode,
TypedJson,
} from '../types/util.js';
export { jsonParse, jsonStringify } from '../types/json.js';

// These cannot be exported directly as modules because of a bug in an eslint plugin.
// https://github.com/import-js/eslint-plugin-import/issues/2289.
Expand Down
9 changes: 9 additions & 0 deletions packages/sdk/src/shims/ed25519.node.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// To add support for node versions <19.
// From https://www.npmjs.com/package/@noble/ed25519#usage
import { webcrypto } from 'node:crypto';

// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
if (!globalThis.crypto) globalThis.crypto = webcrypto;

export * from '@noble/ed25519';
Loading

0 comments on commit 0c86233

Please sign in to comment.