Skip to content

Commit

Permalink
feat: update merkle logic to apply latest chain update (#39)
Browse files Browse the repository at this point in the history
* update merkle logic to apply latest chain update

* remove console.log

---------

Co-authored-by: Harvey <[email protected]>
  • Loading branch information
beer-1 and JSHan94 authored Jun 14, 2024
1 parent a3e711e commit 831b925
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 9 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.

31 changes: 31 additions & 0 deletions src/lib/storage.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,30 @@ const v1 = [
receiver: 'init174knscjg688ddtxj8smyjz073r3w5mmsp3m0m2',
l1_denom: 'uinit',
amount: BigInt(1000000)
},
{
bridge_id: BigInt(1),
sequence: BigInt(4),
sender: 'init1wzenw7r2t2ra39k4l9yqq95pw55ap4sm4vsa9g',
receiver: 'init174knscjg688ddtxj8smyjz073r3w5mmsp3m0m2',
l1_denom: 'uinit',
amount: BigInt(1000231200)
},
{
bridge_id: BigInt(1),
sequence: BigInt(5),
sender: 'init1wzenw7r2t2ra39k4l9yqq95pw55ap4sm4vsa9g',
receiver: 'init174knscjg688ddtxj8smyjz073r3w5mmsp3m0m2',
l1_denom: 'uinit',
amount: BigInt(32340000)
},
{
bridge_id: BigInt(1),
sequence: BigInt(6),
sender: 'init1wzenw7r2t2ra39k4l9yqq95pw55ap4sm4vsa9g',
receiver: 'init174knscjg688ddtxj8smyjz073r3w5mmsp3m0m2',
l1_denom: 'uinit',
amount: BigInt(101230000)
}
]

Expand Down Expand Up @@ -80,5 +104,12 @@ describe('WithdrawStorage', () => {
])
).toString('base64')
expect(airdrop.verify(merkleProof, target)).toBeTruthy()
expect(merkleRoot).toEqual('VcN+0UZbTtGyyLfQtAHW+bCv5ixadyyT0ZZ26aUT1JY=')
expect(merkleProof).toEqual([
'gnUeNU3EnW4iBOk8wounvu98aTER0BP5dOD0lkuwBBE=',
'yE4zjliK5P9sfdzR2iNh6nYHmD+mjDK6dONuZ3QlVcA=',
'GQXXUQ5P/egGvbAHkYfWHIAfgyCEmnjz/fUMKrWCEn8='
])
expect(outputRoot).toEqual('0cg24XcpDwTIFXHY4jNyxg2EQS5RUqcMvlMJeuI5rf4=')
})
})
14 changes: 7 additions & 7 deletions src/lib/storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { sha3_256 } from './util'
import { WithdrawalTx } from './types'
import { AccAddress } from 'initia-l1'

const SPLITTER = Buffer.from('|')
const SPLITTER = Buffer.from('|', 'utf8')

function convertHexToBase64(hex: string): string {
return Buffer.from(hex, 'hex').toString('base64')
Expand All @@ -23,7 +23,7 @@ export class WithdrawStorage {
const amount_buf = Buffer.alloc(8)
amount_buf.writeBigInt64BE(tx.amount)

return sha3_256(
return sha3_256(sha3_256(
Buffer.concat([
bridge_id_buf,
sequence_buf,
Expand All @@ -35,7 +35,7 @@ export class WithdrawStorage {
SPLITTER,
amount_buf
])
)
))
})

this.tree = new MerkleTree(leaves, sha3_256, { sort: true })
Expand All @@ -57,7 +57,7 @@ export class WithdrawStorage {

return this.tree
.getHexProof(
sha3_256(
sha3_256(sha3_256(
Buffer.concat([
bridge_id_buf,
sequence_buf,
Expand All @@ -69,7 +69,7 @@ export class WithdrawStorage {
SPLITTER,
amount_buf
])
)
))
)
.map((v) => convertHexToBase64(v.replace('0x', '')))
}
Expand All @@ -94,7 +94,7 @@ export class WithdrawStorage {
const amount_buf = Buffer.alloc(8)
amount_buf.writeBigInt64BE(tx.amount)

let hashBuf = sha3_256(
let hashBuf = sha3_256(sha3_256(
Buffer.concat([
bridge_id_buf,
sequence_buf,
Expand All @@ -106,7 +106,7 @@ export class WithdrawStorage {
SPLITTER,
amount_buf
])
)
))

proof.forEach((proofElem) => {
const proofBuf = Buffer.from(proofElem, 'base64')
Expand Down

0 comments on commit 831b925

Please sign in to comment.