Skip to content

Commit

Permalink
feat: ibc-hooks add derive function
Browse files Browse the repository at this point in the history
  • Loading branch information
emidev98 committed Nov 16, 2023
1 parent d6f0b54 commit 3e67d0a
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 3 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@terra-money/feather.js",
"version": "2.0.0-beta.12",
"version": "2.0.0-beta.13",
"description": "The JavaScript SDK for Terra and Feather chains",
"license": "MIT",
"author": "Terraform Labs, PTE.",
Expand Down
51 changes: 51 additions & 0 deletions src/core/ibc-hooks/helpers/derive-sender.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import { deriveIbcHooksSender } from './derive-sender';

describe('Must use DeriveIbcHooksSender to derive', () => {
it('a terra address successfully for channel-0 and prefix terra', () => {
const derived = deriveIbcHooksSender(
'channel-0',
'terra18qq9svm5tjywcysd549tzkk3ax04f43zml9xay',
'terra'
);

expect(derived).toStrictEqual(
'terra1qxyr66gyd6fjlr0826cce8fj4jg96hd5jdkg4e3e2hc3gl0r9zps9sc93t'
);
});

it('a terra address successfully for channel-1 and prefix terra', () => {
const derived = deriveIbcHooksSender(
'channel-1',
'terra18qq9svm5tjywcysd549tzkk3ax04f43zml9xay',
'terra'
);

expect(derived).toStrictEqual(
'terra1tdx2llx7dxp245p32rl5xf85qtfhnsn477fs6man34lthdcmr6wsxntykd'
);
});

it('a terra address successfully for channel-1 and prefix juno', () => {
const derived = deriveIbcHooksSender(
'channel-0',
'terra18qq9svm5tjywcysd549tzkk3ax04f43zml9xay',
'juno'
);

expect(derived).toStrictEqual(
'juno1qxyr66gyd6fjlr0826cce8fj4jg96hd5jdkg4e3e2hc3gl0r9zpsrpuvys'
);
});

it('a juno address successfully for channel-0 and prefix terra', () => {
const derived = deriveIbcHooksSender(
'channel-0',
'juno1yd8m9u8gexcjectscrduxmtw60psndqhcd87jt',
'terra'
);

expect(derived).toStrictEqual(
'terra1njxpjzu233h4ctlsyvqq4nuxl5y5cu9lxuprnqmv574ddqkqu7zqxg6u3k'
);
});
});
43 changes: 43 additions & 0 deletions src/core/ibc-hooks/helpers/derive-sender.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { AccAddress } from '../../../core/bech32';
import { bech32 } from 'bech32';
import { createHash } from 'crypto';

const senderPrefix = 'ibc-wasm-hook-intermediary';

// Function used to derive the counterparty chain address from the
// ibc channel, originSender and bech32prefix, for ibc-hooks.
//
// This function is ported to JS from the following Go code:
// https://github.com/cosmos/ibc-apps/blob/main/modules/ibc-hooks/keeper/keeper.go#L57-L62
export const deriveIbcHooksSender = (
channel: string,
originSender: AccAddress,
bech32Prefix: string
): AccAddress => {
const concatedAddress = `${channel}/${originSender}`;
const senderSHA256 = hash(senderPrefix, Buffer.from(concatedAddress));
const words = bech32.toWords(senderSHA256);

return bech32.encode(bech32Prefix, words).toString();
};

// private function to derive the string hash,
// bassed on the following Go code:
// https://github.com/cosmos/cosmos-sdk/blob/release/v0.47.x/types/address/hash.go#L26-L39
const hash = (typ: string, key: Buffer): Buffer => {
let hasher = createHash('sha256');

// Convert the string to a Buffer
const typeBuffer = Buffer.from(typ, 'utf-8');

// First hash
hasher.update(typeBuffer);
const th = hasher.digest();
hasher = createHash('sha256');

// Reset the hasher and do the second hash
hasher.update(th);
hasher.update(key);

return hasher.digest();
};
1 change: 1 addition & 0 deletions src/core/ibc-hooks/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './helpers/derive-sender';

0 comments on commit 3e67d0a

Please sign in to comment.