Skip to content

Commit

Permalink
tools: makeTestAddress helper
Browse files Browse the repository at this point in the history
- returns valid bech32 address for tests
  • Loading branch information
0xpatrickdev committed Dec 18, 2024
1 parent 9418efc commit 71dc3b0
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 0 deletions.
1 change: 1 addition & 0 deletions packages/orchestration/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@
"@endo/import-bundle": "^1.3.2",
"@endo/ses-ava": "^1.2.8",
"ava": "^5.3.1",
"bech32": "^2.0.0",
"c8": "^10.1.2",
"prettier": "^3.4.2",
"ts-blank-space": "^0.4.4"
Expand Down
22 changes: 22 additions & 0 deletions packages/orchestration/test/tools/make-test-address.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import test from '@endo/ses-ava/prepare-endo.js';
import { encodeAddressHook } from '@agoric/cosmic-proto/address-hooks.js';
import { makeTestAddress } from '../../tools/make-test-address.js';

test('makeTestAddress returns valid bech32', t => {
t.is(makeTestAddress(), 'agoric1qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqp7zqht');
t.is(makeTestAddress(1), 'agoric1qyqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqc09z0g');
t.is(
makeTestAddress(0, 'agoric', 32),
'agoric1qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqkppep3',
);
t.is(
makeTestAddress(0, 'cosmos'),
'cosmos1qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqnrql8a',
);
});

test('makeTestAddress creates address accepted by encodeAddressHook', t => {
const params = { EUD: makeTestAddress(0, 'osmosis') };
t.throws(() => encodeAddressHook('agoric1FakeLCAAddress', params));
t.notThrows(() => encodeAddressHook(makeTestAddress(), params));
});
21 changes: 21 additions & 0 deletions packages/orchestration/tools/make-test-address.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// eslint-disable-next-line import/no-extraneous-dependencies
import { bech32 } from 'bech32';

/**
* @param {number} index
* @param {string} prefix
* @param {number} byteLength
* @returns {string} a mock bech32 address for tests
*/
export const makeTestAddress = (
index = 0,
prefix = 'agoric',
byteLength = 20,
) => {
// create a simple 20-byte array (common address length)
const bytes = new Uint8Array(byteLength).fill(0);
// if index provided, put it in the first byte
if (index !== 0) bytes[0] = Number(index);
const words = bech32.toWords(bytes);
return bech32.encode(prefix, words);
};

0 comments on commit 71dc3b0

Please sign in to comment.