Skip to content

Commit

Permalink
Merge pull request #24 from initia-labs/fix/bug-additionalNamedAddresses
Browse files Browse the repository at this point in the history
fix: use base64 encode instead of utf8 in cString
  • Loading branch information
SeUkKim authored Dec 6, 2024
2 parents 3537ae2 + 983fe76 commit 2f690dd
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 40 deletions.
5 changes: 5 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ jobs:
RUST_BACKTRACE: 1
steps:
- uses: actions/checkout@v4
- name: Setup Node.js
uses: actions/setup-node@v4
with:
node-version: '20'
cache: 'npm'
- run: npm install
- run: npm run build
- run: npm run test
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@initia/builder.js",
"version": "0.2.5",
"version": "0.2.6",
"description": "The JavaScript Move Builder for Initia",
"license": "MIT",
"author": "Initia Foundation",
Expand Down
10 changes: 5 additions & 5 deletions src/builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ export class MoveBuilder {
*/
private makeRawBuildConfig = () => {
const additionalNamedAddresses: [string, Uint8Array][] = this.buildOptions
.addtionalNamedAddresses
? this.buildOptions.addtionalNamedAddresses.map(([name, address]) => {
.additionalNamedAddresses
? this.buildOptions.additionalNamedAddresses.map(([name, address]) => {
if (address.startsWith('0x')) {
address = address.slice(2).padStart(64, '0')
}
Expand Down Expand Up @@ -71,8 +71,8 @@ export class MoveBuilder {
rawCompilerPayload.is_nil = false
rawCompilerPayload.len = compilerPayloadBytes.length
rawCompilerPayload.ptr = ref.allocCString(
compilerPayloadBytes.toString(),
'utf-8'
compilerPayloadBytes.toString('base64'),
'base64'
)

return rawCompilerPayload
Expand Down Expand Up @@ -206,7 +206,7 @@ export class MoveBuilder {
const rawTestOpt = testOpt.deref()
rawTestOpt.is_nil = false
rawTestOpt.len = testOptBytes.length
rawTestOpt.ptr = ref.allocCString(testOptBytes.toString(), 'utf-8')
rawTestOpt.ptr = ref.allocCString(testOptBytes.toString('base64'), 'base64')

return handleResponse(
libcompiler.test_move_package.async,
Expand Down
2 changes: 1 addition & 1 deletion src/types/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ export interface BuildOptions {
/**
* Additional named address mapping. Useful for tools in Rust.
*/
addtionalNamedAddresses?: [string, string][]
additionalNamedAddresses?: [string, string][]
}

export interface CleanOptions {
Expand Down
99 changes: 66 additions & 33 deletions test/build.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ describe('build move package', () => {
bytecodeVersion: 7,
compilerVersion: '2',
languageVersion: '1',
addtionalNamedAddresses: [
additionalNamedAddresses: [
['test', '0x4'],
['test2', '0x5'],
['test2', '0x4c4e8f7def3c24453ae7eee7d9aee9b7556a26a0'],
],
})
const dummyModulePath = path.join(
Expand All @@ -35,44 +35,77 @@ describe('build move package', () => {
})

it('decodes module bytes correctly', async () => {
const binary = await builder.get('dummy')
const expectedDecoded = JSON.stringify({
address: '0x4',
name: 'dummy',
friends: [],
exposed_functions: [
{
name: 'return_0',
visibility: 'public',
is_entry: false,
is_view: false,
generic_type_params: [],
params: [],
return: ['u32'],
},
{
name: 'return_10',
visibility: 'public',
is_entry: false,
is_view: false,
generic_type_params: [],
params: [],
return: ['u32'],
},
],
structs: [],
})
const dummy = await builder.get('dummy')

expect(await MoveBuilder.decode_module_bytes(binary)).toEqual(
expectedDecoded
expect(await MoveBuilder.decode_module_bytes(dummy)).toEqual(
JSON.stringify({
address: '0x4',
name: 'dummy',
friends: [],
exposed_functions: [
{
name: 'return_0',
visibility: 'public',
is_entry: false,
is_view: false,
generic_type_params: [],
params: [],
return: ['u32'],
},
{
name: 'return_10',
visibility: 'public',
is_entry: false,
is_view: false,
generic_type_params: [],
params: [],
return: ['u32'],
},
],
structs: [],
})
)
const hihi = await builder.get('hihi')

expect(await MoveBuilder.decode_module_bytes(hihi)).toEqual(
JSON.stringify({
address: '0x4c4e8f7def3c24453ae7eee7d9aee9b7556a26a0',
name: 'hihi',
friends: [],
exposed_functions: [
{
name: 'return_0',
visibility: 'public',
is_entry: false,
is_view: false,
generic_type_params: [],
params: [],
return: ['u32'],
},
{
name: 'return_10',
visibility: 'public',
is_entry: false,
is_view: false,
generic_type_params: [],
params: [],
return: ['u32'],
},
],
structs: [],
})
)
})

it('reads module info correctly', async () => {
const binary = await builder.get('dummy')
expect(await MoveBuilder.read_module_info(binary)).toEqual(
const dummy = await builder.get('dummy')
expect(await MoveBuilder.read_module_info(dummy)).toEqual(
'{"address":[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,4],"name":"dummy"}'
)
const hihi = await builder.get('hihi')
expect(await MoveBuilder.read_module_info(hihi)).toEqual(
'{"address":[0,0,0,0,0,0,0,0,0,0,0,0,76,78,143,125,239,60,36,69,58,231,238,231,217,174,233,183,85,106,38,160],"name":"hihi"}'
)
})

it('reads module info correctly', async () => {
Expand Down

0 comments on commit 2f690dd

Please sign in to comment.