Skip to content

Commit

Permalink
feat: vat-safe denomHash using noble sha256
Browse files Browse the repository at this point in the history
 - @noble/hashes dependency
 - unit test using bundle
  • Loading branch information
dckc committed Jun 26, 2024
1 parent cbe061c commit ca8c1f2
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 2 deletions.
7 changes: 5 additions & 2 deletions packages/orchestration/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@
},
"homepage": "https://github.com/Agoric/agoric-sdk#readme",
"dependencies": {
"@agoric/async-flow": "^0.1.0",
"@agoric/assert": "^0.6.0",
"@agoric/async-flow": "^0.1.0",
"@agoric/cosmic-proto": "^0.4.0",
"@agoric/ertp": "^0.16.2",
"@agoric/internal": "^0.3.2",
Expand All @@ -48,9 +48,12 @@
"@agoric/zoe": "^0.26.2",
"@agoric/zone": "^0.2.2",
"@endo/base64": "^1.0.5",
"@endo/bundle-source": "3.2.3",
"@endo/far": "^1.1.2",
"@endo/import-bundle": "1.1.2",
"@endo/marshal": "^1.5.0",
"@endo/patterns": "^1.4.0"
"@endo/patterns": "^1.4.0",
"@noble/hashes": "^1.4.0"
},
"devDependencies": {
"@chain-registry/client": "^1.47.4",
Expand Down
22 changes: 22 additions & 0 deletions packages/orchestration/src/utils/denomHash.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// @ts-check
import { sha256 } from '@noble/hashes/sha256';
import { bytesToHex } from '@noble/hashes/utils';

/**
* cf. https://tutorials.cosmos.network/tutorials/6-ibc-dev/
*
* @param {object} opts
* @param {string} [opts.portId]
* @param {string} [opts.channelId] required unless `path` is supplied
* @param {string} [opts.path] alternative to portId, channelId
* @param {string} opts.denom base denom
*/
export const denomHash = ({
portId = 'transfer',
channelId = /** @type {string | undefined} */ (undefined),
path = `${portId}/${channelId}`,
denom,
}) => {
const h = sha256.create().update(`${path}/${denom}`).digest();
return bytesToHex(h).toUpperCase();
};
18 changes: 18 additions & 0 deletions packages/orchestration/test/utils/denomHash.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import test from '@endo/ses-ava/prepare-endo.js';

import bundleSource from '@endo/bundle-source';
import { importBundle } from '@endo/import-bundle';
import { createRequire } from 'node:module';
import { denomHash } from '../../src/utils/denomHash.js';

const nodeRequire = createRequire(import.meta.url);

test('compartment use of denomHash', async t => {
const bundle = await bundleSource(nodeRequire.resolve('./denomHashEx.js'));
const { length } = JSON.stringify(bundle);
t.log('bundle length', length);
const expected = denomHash({ channelId: 'channel-0', denom: 'uatom' });

const { denomHashExample } = await importBundle(bundle);
t.deepEqual(denomHashExample(), expected);
});
6 changes: 6 additions & 0 deletions packages/orchestration/test/utils/denomHashEx.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { denomHash } from '../../src/utils/denomHash.js';

export const denomHashExample = () => {
const h = denomHash({ channelId: 'channel-0', denom: 'uatom' });
return h;
};

0 comments on commit ca8c1f2

Please sign in to comment.