Skip to content

Commit d503177

Browse files
committed
test: add tests to instantiate2 address prediction
1 parent cadb381 commit d503177

7 files changed

+281
-128
lines changed

package.json

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
"changeset:release": "turbo build && changeset publish",
1010
"changeset:version": "changeset version && pnpm install --lockfile-only",
1111
"cjs:release": "node -r esbuild-register ./scripts/cjs.ts",
12+
"test": "vitest",
1213
"format": "biome format . --write",
1314
"lint": "biome check .",
1415
"lint:fix": "pnpm lint --apply",
@@ -48,5 +49,9 @@
4849
"simple-git-hooks": {
4950
"pre-commit": "pnpm format && pnpm lint:fix && git add -u"
5051
},
51-
"packageManager": "[email protected]"
52+
"packageManager": "[email protected]",
53+
"dependencies": {
54+
"happy-dom": "^13.3.8",
55+
"vitest": "^1.2.2"
56+
}
5257
}

packages/core/package.json

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -93,11 +93,6 @@
9393
},
9494
"./package.json": "./package.json"
9595
},
96-
"files": [
97-
"/clients",
98-
"/utils",
99-
"/legacy",
100-
"/dist"
101-
],
96+
"files": ["/clients", "/utils", "/legacy", "/dist"],
10297
"sideEffects": false
10398
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import { describe, expect, it } from 'vitest'
2+
import { stringToAccountId } from '../account-id'
3+
import { getInstantiate2AccountAddress } from './get-instantiate2-account-address' // Update the path to your logic file
4+
5+
describe('getInstantiate2AccountAddress', () => {
6+
it('returns the correct address', async () => {
7+
const address = 'juno1jzyqffltm2s5wxmnjyze5hzrpcady0gmltw6ka'
8+
const accountFactoryAddress =
9+
'juno1qtl43hzk7xd9xly9wc4qxnmtjmj0hdtymsnldsl736yh7vnfm3ys7t4a4y'
10+
const checksum = new Uint8Array([
11+
1, 2, 3, 4, 5, 6, 7, 8, 1, 2, 3, 4, 5, 6, 7, 8, 1, 2, 3, 4, 5, 6, 7, 8, 1,
12+
2, 3, 4, 5, 6, 7, 8,
13+
])
14+
const accountId = stringToAccountId('juno-5', 'juno')
15+
16+
const result = await getInstantiate2AccountAddress(
17+
address,
18+
accountFactoryAddress,
19+
checksum,
20+
accountId,
21+
)
22+
23+
expect(result).toBe(
24+
'juno1m8lz9cru30zugeas5tx9245kn6w4rv92y7t96gvcsl0ktve378tqkv60qs',
25+
)
26+
})
27+
})

packages/core/src/utils/account-factory/get-instantiate2-account-address.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,15 @@ import { bech32 } from 'bech32'
33
import { AccountId, accountIdToString } from '../account-id'
44

55
async function getSalt(accountId: AccountId) {
6+
const encoder = new TextEncoder()
67
const hash = await crypto.subtle.digest(
7-
'sha256',
8-
new TextEncoder().encode(accountIdToString(accountId)),
8+
'SHA-256',
9+
encoder.encode(accountIdToString(accountId)),
910
)
10-
return new Uint8Array(hash)
11+
return new Uint8Array([
12+
...new Uint8Array(hash),
13+
...encoder.encode('abstract'),
14+
])
1115
}
1216

1317
export async function getInstantiate2AccountAddress(

0 commit comments

Comments
 (0)