Skip to content

Commit

Permalink
Add function for stringifying wrapper types as unwrapped, serializabl…
Browse files Browse the repository at this point in the history
…e types (#306)

* Add function for unwrapping and serializing types which include concordium domain types
  • Loading branch information
soerenbf authored Nov 13, 2023
1 parent 58e1e26 commit 1c859a0
Show file tree
Hide file tree
Showing 27 changed files with 706 additions and 11 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/deployment.yml
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ jobs:
path: |
./node_modules
./docs/node_modules
./packages/ccd-js-gen/node_modules
key: ${{ runner.os }}-yarn-${{ hashFiles('yarn.lock') }}
restore-keys: |
${{ runner.os }}-yarn-${{ hashFiles('yarn.lock') }}
Expand Down Expand Up @@ -104,6 +105,7 @@ jobs:
path: |
./node_modules
./docs/node_modules
./packages/ccd-js-gen/node_modules
key: ${{ runner.os }}-yarn-${{ hashFiles('yarn.lock') }}

- name: Generate typedoc documentation
Expand Down
8 changes: 8 additions & 0 deletions .github/workflows/pipeline.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ jobs:
path: |
./node_modules
./docs/node_modules
./packages/ccd-js-gen/node_modules
key: ${{ runner.os }}-yarn-${{ hashFiles('yarn.lock') }}-${{ env.DUMMY }}
restore-keys: |
${{ runner.os }}-yarn
Expand Down Expand Up @@ -83,6 +84,7 @@ jobs:
path: |
./node_modules
./docs/node_modules
./packages/ccd-js-gen/node_modules
key: ${{ runner.os }}-yarn

- name: Install rust
Expand Down Expand Up @@ -126,6 +128,7 @@ jobs:
path: |
./node_modules
./docs/node_modules
./packages/ccd-js-gen/node_modules
key: ${{ runner.os }}-yarn

- name: Get build-debug
Expand Down Expand Up @@ -162,6 +165,7 @@ jobs:
path: |
./node_modules
./docs/node_modules
./packages/ccd-js-gen/node_modules
key: ${{ runner.os }}-yarn

- name: Get build-debug
Expand Down Expand Up @@ -189,6 +193,7 @@ jobs:
path: |
./node_modules
./docs/node_modules
./packages/ccd-js-gen/node_modules
key: ${{ runner.os }}-yarn

- name: Get build-debug
Expand Down Expand Up @@ -217,6 +222,7 @@ jobs:
path: |
./node_modules
./docs/node_modules
./packages/ccd-js-gen/node_modules
key: ${{ runner.os }}-yarn

- name: Get build-debug
Expand Down Expand Up @@ -245,6 +251,7 @@ jobs:
path: |
./node_modules
./docs/node_modules
./packages/ccd-js-gen/node_modules
key: ${{ runner.os }}-yarn

- name: Lint markdown
Expand All @@ -268,6 +275,7 @@ jobs:
path: |
./node_modules
./docs/node_modules
./packages/ccd-js-gen/node_modules
key: ${{ runner.os }}-yarn

- name: Lint markdown
Expand Down
6 changes: 3 additions & 3 deletions packages/ccd-js-gen/jest.config.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import type { Config } from 'jest';
import type {} from 'ts-jest';

export const esModules = ['@noble/ed25519', '@concordium/web-sdk'];

const config: Config = {
preset: 'ts-jest/presets/js-with-ts-esm',
moduleNameMapper: {
'^(\\.\\.?\\/.+)\\.js$': '$1', // Remap esmodule file extensions
},
transformIgnorePatterns: [
'node_modules/(?!@noble/ed25519)', // @noble/ed25519 is an ES module only
],
transformIgnorePatterns: [`node_modules/(?!${esModules.join('|')})`],
};

export default config;
7 changes: 7 additions & 0 deletions packages/sdk/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# Changelog

## 7.1.0

### Added

- `jsonUnwrapStringify` function, which can be used to unwrap concordium domain types to their inner values before serializing, to ease compatibility with dependants deserializing stringified JSON.


## 7.0.3

### Fixed
Expand Down
2 changes: 1 addition & 1 deletion packages/sdk/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@concordium/web-sdk",
"version": "7.0.3",
"version": "7.1.0",
"license": "Apache-2.0",
"engines": {
"node": ">=16"
Expand Down
7 changes: 6 additions & 1 deletion packages/sdk/src/pub/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,12 @@ export {
TypedJsonParseErrorCode,
TypedJson,
} from '../types/util.js';
export { jsonParse, jsonStringify } from '../types/json.js';
export {
jsonParse,
jsonStringify,
jsonUnwrapStringify,
BigintFormatType,
} 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
15 changes: 15 additions & 0 deletions packages/sdk/src/types/AccountAddress.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ import {
} from './util.js';
import { Base58String } from '../types.js';

// IMPORTANT:
// When adding functionality to this module, it is important to not change the wrapper class, as changing this might break compatibility
// between different versions of the SDK, e.g. if a dependency exposes an API that depends on the class and a class from a different version
// of the SDK is passed.

/**
* The {@linkcode TypedJsonDiscriminator} discriminator associated with {@linkcode Type} type.
*/
Expand All @@ -30,6 +35,16 @@ class AccountAddress {
) {}
}

/**
* Unwraps {@linkcode Type} value
*
* @param value value to unwrap.
* @returns the unwrapped {@linkcode Serializable} value
*/
export function toUnwrappedJSON(value: Type): Serializable {
return toBase58(value);
}

/**
* Representation of an account address, which enforces that it:
* - Is a valid base58 string with version byte of 1.
Expand Down
15 changes: 15 additions & 0 deletions packages/sdk/src/types/BlockHash.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ import {
makeFromTypedJson,
} from './util.js';

// IMPORTANT:
// When adding functionality to this module, it is important to not change the wrapper class, as changing this might break compatibility
// between different versions of the SDK, e.g. if a dependency exposes an API that depends on the class and a class from a different version
// of the SDK is passed.

/**
* The number of bytes used to represent a block hash.
*/
Expand All @@ -31,6 +36,16 @@ class BlockHash {
) {}
}

/**
* Unwraps {@linkcode Type} value
*
* @param value value to unwrap.
* @returns the unwrapped {@linkcode Serializable} value
*/
export function toUnwrappedJSON(value: Type): Serializable {
return toHexString(value);
}

/**
* Represents a hash of a block in the chain.
*/
Expand Down
5 changes: 5 additions & 0 deletions packages/sdk/src/types/CcdAmount.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ import {
makeFromTypedJson,
} from './util.js';

// IMPORTANT:
// When adding functionality to this module, it is important to not change the wrapper class, as changing this might break compatibility
// between different versions of the SDK, e.g. if a dependency exposes an API that depends on the class and a class from a different version
// of the SDK is passed.

const MICRO_CCD_PER_CCD = 1_000_000;
/**
* The {@linkcode TypedJsonDiscriminator} discriminator associated with {@linkcode Type} type.
Expand Down
24 changes: 22 additions & 2 deletions packages/sdk/src/types/ContractAddress.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,21 @@ import {
makeFromTypedJson,
} from './util.js';

// IMPORTANT:
// When adding functionality to this module, it is important to not change the wrapper class, as changing this might break compatibility
// between different versions of the SDK, e.g. if a dependency exposes an API that depends on the class and a class from a different version
// of the SDK is passed.

/**
* The {@linkcode TypedJsonDiscriminator} discriminator associated with {@linkcode Type} type.
*/
export const JSON_DISCRIMINATOR = TypedJsonDiscriminator.ContractAddress;
export type Serializable = { index: string; subindex: string };

type ContractAddressLike<T> = { index: T; subindex: T };
export type Serializable = ContractAddressLike<string>;

/** Address of a smart contract instance. */
class ContractAddress {
class ContractAddress implements ContractAddressLike<bigint> {
/** Having a private field prevents similar structured objects to be considered the same type (similar to nominal typing). */
private __type = JSON_DISCRIMINATOR;
constructor(
Expand All @@ -23,6 +30,19 @@ class ContractAddress {
) {}
}

/**
* Unwraps {@linkcode Type} value
*
* @param value value to unwrap.
* @returns the unwrapped {@linkcode Serializable} value
*/
export function toUnwrappedJSON({
index,
subindex,
}: Type): ContractAddressLike<bigint> {
return { index, subindex };
}

/** Address of a smart contract instance. */
export type Type = ContractAddress;

Expand Down
17 changes: 17 additions & 0 deletions packages/sdk/src/types/ContractEvent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,13 @@ import type {
SmartContractTypeValues,
} from '../types.js';

// IMPORTANT:
// When adding functionality to this module, it is important to not change the wrapper class, as changing this might break compatibility
// between different versions of the SDK, e.g. if a dependency exposes an API that depends on the class and a class from a different version
// of the SDK is passed.

export type Serializable = HexString;

/**
* An event logged by a smart contract instance.
*/
Expand All @@ -20,6 +27,16 @@ class ContractEvent {
) {}
}

/**
* Unwraps {@linkcode Type} value
*
* @param value value to unwrap.
* @returns the unwrapped {@linkcode Serializable} value
*/
export function toUnwrappedJSON(value: Type): Serializable {
return toHexString(value);
}

/**
* An event logged by a smart contract instance.
*/
Expand Down
15 changes: 15 additions & 0 deletions packages/sdk/src/types/ContractName.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ import {
makeFromTypedJson,
} from './util.js';

// IMPORTANT:
// When adding functionality to this module, it is important to not change the wrapper class, as changing this might break compatibility
// between different versions of the SDK, e.g. if a dependency exposes an API that depends on the class and a class from a different version
// of the SDK is passed.

/**
* The {@linkcode TypedJsonDiscriminator} discriminator associated with {@linkcode Type} type.
*/
Expand All @@ -22,6 +27,16 @@ class ContractName {
) {}
}

/**
* Unwraps {@linkcode Type} value
*
* @param value value to unwrap.
* @returns the unwrapped {@linkcode Serializable} value
*/
export function toUnwrappedJSON(value: Type): Serializable {
return toString(value);
}

/** The name of a smart contract. Note: This does _not_ including the 'init_' prefix. */
export type Type = ContractName;

Expand Down
7 changes: 6 additions & 1 deletion packages/sdk/src/types/CredentialRegistrationId.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ import {
makeFromTypedJson,
} from './util.js';

// IMPORTANT:
// When adding functionality to this module, it is important to not change the wrapper class, as changing this might break compatibility
// between different versions of the SDK, e.g. if a dependency exposes an API that depends on the class and a class from a different version
// of the SDK is passed.

/**
* The {@linkcode TypedJsonDiscriminator} discriminator associated with {@linkcode Type} type.
*/
Expand All @@ -28,7 +33,7 @@ class CredentialRegistrationId {
public readonly credId: string
) {}

public toJSON(): string {
public toJSON(): Serializable {
return this.credId;
}
}
Expand Down
15 changes: 15 additions & 0 deletions packages/sdk/src/types/Duration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ import {
makeFromTypedJson,
} from './util.js';

// IMPORTANT:
// When adding functionality to this module, it is important to not change the wrapper class, as changing this might break compatibility
// between different versions of the SDK, e.g. if a dependency exposes an API that depends on the class and a class from a different version
// of the SDK is passed.

/**
* The {@linkcode TypedJsonDiscriminator} discriminator associated with {@linkcode Type} type.
*/
Expand All @@ -24,6 +29,16 @@ class Duration {
) {}
}

/**
* Unwraps {@linkcode Type} value
*
* @param value value to unwrap.
* @returns the unwrapped {@linkcode bigint} value
*/
export function toUnwrappedJSON(value: Type): bigint {
return value.value;
}

/**
* Type representing a duration of time down to milliseconds.
* Can not be negative.
Expand Down
15 changes: 15 additions & 0 deletions packages/sdk/src/types/Energy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ import {
makeFromTypedJson,
} from './util.js';

// IMPORTANT:
// When adding functionality to this module, it is important to not change the wrapper class, as changing this might break compatibility
// between different versions of the SDK, e.g. if a dependency exposes an API that depends on the class and a class from a different version
// of the SDK is passed.

/**
* The {@linkcode TypedJsonDiscriminator} discriminator associated with {@linkcode Type} type.
*/
Expand All @@ -25,6 +30,16 @@ class Energy {
) {}
}

/**
* Unwraps {@linkcode Type} value
*
* @param value value to unwrap.
* @returns the unwrapped {@linkcode bigint} value
*/
export function toUnwrappedJSON(value: Type): bigint {
return value.value;
}

/** Energy measure. Used as part of cost calculations for transactions. */
export type Type = Energy;

Expand Down
Loading

0 comments on commit 1c859a0

Please sign in to comment.