-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
98 additions
and
3 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' | ||
); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './helpers/derive-sender'; |