Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: v0.7.x amino data MsgAttest support #90

Merged
merged 6 commits into from
Sep 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,29 @@
All notable changes to this project will be documented in this file.
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.

## [0.7.1](https://github.com/regen-network/regen-js/compare/v0.7.0...v0.7.1) (2024-08-05)


### Bug Fixes

* use base64 string for hash in amino ([88737e5](https://github.com/regen-network/regen-js/commit/88737e56f612fb957e997e26ff852662768c2046))





# [0.7.0](https://github.com/regen-network/regen-js/compare/v0.6.0...v0.7.0) (2024-08-05)


### Features

* add amino data MsgAttest converter ([739163b](https://github.com/regen-network/regen-js/commit/739163b341ff466d14fdd7df8025ffd7c0cadfe4))






# [0.6.0](https://github.com/regen-network/regen-js/compare/v0.3.0...v0.6.0) (2023-03-27)


Expand Down
2 changes: 1 addition & 1 deletion lerna.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@
"packages": [
"packages/*"
],
"version": "0.6.0"
"version": "0.7.1"
}
23 changes: 23 additions & 0 deletions packages/api/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,29 @@
All notable changes to this project will be documented in this file.
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.

## [0.7.1](https://github.com/regen-network/regen-js/compare/v0.7.0...v0.7.1) (2024-08-05)


### Bug Fixes

* use base64 string for hash in amino ([88737e5](https://github.com/regen-network/regen-js/commit/88737e56f612fb957e997e26ff852662768c2046))





# [0.7.0](https://github.com/regen-network/regen-js/compare/v0.6.0...v0.7.0) (2024-08-05)


### Features

* add amino data MsgAttest converter ([739163b](https://github.com/regen-network/regen-js/commit/739163b341ff466d14fdd7df8025ffd7c0cadfe4))






# [0.6.0](https://github.com/regen-network/regen-js/compare/v0.3.0...v0.6.0) (2023-03-27)


Expand Down
2 changes: 1 addition & 1 deletion packages/api/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@regen-network/api",
"version": "0.6.0",
"version": "0.7.1",
"author": "[email protected]",
"description": "A client library for the Regen Ledger",
"license": "Apache-2.0",
Expand Down
8 changes: 8 additions & 0 deletions packages/api/src/tx/modules/data/converters.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { AminoConverters } from '@cosmjs/stargate';
import { attestConverter, attestTypeUrl } from './v1/attest_amino';

export function createDataAminoConverters(): AminoConverters {
return {
[attestTypeUrl]: attestConverter(),
};
}
79 changes: 79 additions & 0 deletions packages/api/src/tx/modules/data/v1/attest_amino.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import { AminoMsg } from '@cosmjs/amino';

import { MsgAttest } from '../../../../generated/regen/data/v1/tx';
import {
DigestAlgorithm,
GraphCanonicalizationAlgorithm,
GraphMerkleTree,
ContentHash_Graph,
} from '../../../../generated/regen/data/v1/types';
import { AminoConverter } from '@cosmjs/stargate';
import { omitDefault } from '../../converter-utils';

const msgAttestAminoType = 'regen-ledger/MsgAttest';

export const attestTypeUrl = '/' + MsgAttest.$type;

type AminoContentHash_Graph = {
hash: string;
digest_algorithm?: DigestAlgorithm;
canonicalization_algorithm?: GraphCanonicalizationAlgorithm;
merkle_tree?: GraphMerkleTree;
};
export interface AminoMsgAttest extends AminoMsg {
readonly type: typeof msgAttestAminoType;
readonly value: {
readonly attestor: string;
readonly content_hashes: AminoContentHash_Graph[];
};
}

export function attestConverter(): AminoConverter {
return {
aminoType: msgAttestAminoType,
toAmino: ({
attestor,
contentHashes,
}: MsgAttest): AminoMsgAttest['value'] => ({
attestor,
content_hashes: contentHashes.map(contentHash => ({
hash: base64FromBytes(contentHash.hash),
digest_algorithm: omitDefault(contentHash.digestAlgorithm),
canonicalization_algorithm: omitDefault(
contentHash.canonicalizationAlgorithm,
),
merkle_tree: omitDefault(contentHash.merkleTree),
})),
}),
fromAmino: ({
attestor,
content_hashes,
}: AminoMsgAttest['value']): Partial<MsgAttest> => ({
attestor,
contentHashes: content_hashes.map(contentHash => ({
$type: ContentHash_Graph.$type,
hash: bytesFromBase64(contentHash.hash),
digestAlgorithm: contentHash.digest_algorithm || 0,
canonicalizationAlgorithm: contentHash.canonicalization_algorithm || 0,
merkleTree: contentHash.merkle_tree || 0,
})),
}),
};
}

function bytesFromBase64(b64: string): Uint8Array {
const bin = window.atob(b64);
const arr = new Uint8Array(bin.length);
for (let i = 0; i < bin.length; ++i) {
arr[i] = bin.charCodeAt(i);
}
return arr;
}

function base64FromBytes(arr: Uint8Array): string {
const bin: string[] = [];
arr.forEach(byte => {
bin.push(String.fromCharCode(byte));
});
return window.btoa(bin.join(''));
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
MsgBuyDirect_Order,
} from '../../../../../generated/regen/ecocredit/marketplace/v1/tx';
import { AminoCoin } from './sell_amino';
import { omitDefault } from '../../converter-utils';
import { omitDefault } from '../../../converter-utils';

const msgBuyDirectAmnioType = 'regen.marketplace/MsgBuyDirect';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {
MsgSell,
MsgSell_Order,
} from '../../../../../generated/regen/ecocredit/marketplace/v1/tx';
import { AminoDate } from '../../converter-utils';
import { AminoDate } from '../../../converter-utils';

const msgSellAminoType = 'regen.marketplace/MsgSell';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
BatchIssuance,
OriginTx,
} from '../../../../generated/regen/ecocredit/v1/types';
import { AminoDate } from '../converter-utils';
import { AminoDate } from '../../converter-utils';

const msgCreateBatchAminoType = 'regen/MsgCreateBatch';

Expand Down
1 change: 1 addition & 0 deletions packages/api/src/tx/modules/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export { createEcocreditAminoConverters } from './ecocredit/converters';
export { createDataAminoConverters } from './data/converters';
6 changes: 5 additions & 1 deletion packages/api/src/tx/msg.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@ import { Registry, EncodeObject, GeneratedType } from '@cosmjs/proto-signing';

import { SigningConnectionOptions } from '../api';
import { createStargateSigningClient } from './stargate-signing';
import { createEcocreditAminoConverters } from './modules';
import {
createDataAminoConverters,
createEcocreditAminoConverters,
} from './modules';
import { messageTypeRegistry } from '../generated/typeRegistry';

export interface MessageClient {
Expand All @@ -26,6 +29,7 @@ export interface MessageClient {
function createDefaultTypes(): AminoConverters {
return {
...createEcocreditAminoConverters(),
...createDataAminoConverters(),
};
}

Expand Down
17 changes: 17 additions & 0 deletions packages/demo-app/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,23 @@
All notable changes to this project will be documented in this file.
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.

## [0.7.1](https://github.com/regen-network/regen-js/compare/v0.7.0...v0.7.1) (2024-08-05)

**Note:** Version bump only for package @regen-network/demo-app





# [0.7.0](https://github.com/regen-network/regen-js/compare/v0.6.0...v0.7.0) (2024-08-05)

**Note:** Version bump only for package @regen-network/demo-app






# [0.6.0](https://github.com/regen-network/regen-js/compare/v0.3.0...v0.6.0) (2023-03-27)


Expand Down
4 changes: 2 additions & 2 deletions packages/demo-app/package.json
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
{
"name": "@regen-network/demo-app",
"version": "0.6.0",
"version": "0.7.1",
"author": "[email protected]",
"description": "Demo web application using @regen-network/api",
"license": "Apache-2.0",
"private": true,
"repository": "https://github.com/regen-network/regen-js",
"dependencies": {
"@regen-network/api": "^0.6.0",
"@regen-network/api": "^0.7.1",
"@testing-library/jest-dom": "^5.11.8",
"@testing-library/react": "^11.2.2",
"@testing-library/user-event": "^12.6.0",
Expand Down
Loading