Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a helper for the conversion: bytecode file contents -> Vec<u8> #1950

Closed
Dhaiwat10 opened this issue Mar 26, 2024 · 6 comments · Fixed by #2018
Closed

Add a helper for the conversion: bytecode file contents -> Vec<u8> #1950

Dhaiwat10 opened this issue Mar 26, 2024 · 6 comments · Fixed by #2018
Assignees
Labels
feat Issue is a feature

Comments

@Dhaiwat10
Copy link
Member

We need a helper function to read bytecode from a file and convert that to a bytearray that users can pass into our sway-libs functions related to bytecode: https://github.com/FuelLabs/sway-libs/tree/master/libs/bytecode

This conversion is required because the bytecode from a file is a string, but the type that these Sway functions accept for the bytecode is a Vec<u8> - which in JavaScript-land is a Vec<BigNumberish>.

This helper would convert the string to the proper type and ensure all the data is correctly packed.

Possible API

const bytecodeFromFile = readFile(pathToBytecode); // string

const bytecodeVec = generateBytecodeVecFromString(bytecodeFromFile); // our helper -> returns `Vec<BigNumberish>`

await contract.functions.verify_bytecode(bytecodeVec);

Incoming from feedback from the development work being done on the Fuelnaut project: FuelLabs/docs-hub#203

The Fuelnaut project is blocked for now until we figure this out.

@Dhaiwat10 Dhaiwat10 added the feat Issue is a feature label Mar 26, 2024
@Dhaiwat10
Copy link
Member Author

cc @sarahschwartz

@arboleya
Copy link
Member

@Dhaiwat10 You mean the contract bytecode needs to be sent as an argument to another contract/function call?

@Dhaiwat10
Copy link
Member Author

@arboleya precisely

@nedsalk
Copy link
Contributor

nedsalk commented Mar 28, 2024

If we were to treat Vec<u8> as number[] | Uint8Array then we can do simply do this:

import { arrayify } from 'fuels';

const bytecodeFromFile = readFile(pathToBytecode); // string

await contract.functions.verify_bytecode(arrayify(bytecodeFromFile));

On first look, it seems to me that this wouldn't even require any change to the runtime behavior in VecCoder:

pointer.dynamicData = {
  0: concatWithDynamicData(Array.from(value).map((v) => this.coder.encode(v))),
};

Array.from can accept Uint8Array out of the box the same way it'd accept a regular number[]:

Array.from(new Uint8Array([1,2,3]));
// Array(3) [1, 2, 3]

Array.from([1,2,3]);
// Array(3) [1, 2, 3]

@nedsalk
Copy link
Contributor

nedsalk commented Mar 31, 2024

@sarahschwartz Could you try my proposal from above? You'd have to do something like this:

await contract.functions.verify_bytecode(arrayify(bytecodeFromFile) as unknown as number[]);

@sarahschwartz
Copy link
Contributor

@sarahschwartz Could you try my proposal from above? You'd have to do something like this:

await contract.functions.verify_bytecode(arrayify(bytecodeFromFile) as unknown as number[]);

I tried this, but i get this error:

FuelnautLevel.tsx:136 Error deploying new instance: FuelError: Expected array value.
    at VecCoder.encode (index.mjs:723:13)
    at eval (index.mjs:687:31)
    at Array.map (<anonymous>)
    at TupleCoder.encode (index.mjs:686:19)
    at FunctionFragment.encodeArguments (index.mjs:1226:27)
    at createContractCall (index.mjs:805:21)

Here is my code for reference:

Sway Contract:

  fn verify_bytecode_test(bytecode_input: Vec<u8>){
        let mut bytecode = bytecode_input;
        let root = compute_bytecode_root(bytecode);
        log(root);
    }

Next.js app:

// server side
const byteCodePath = join(
      fuelnautPath,
      `${levelKey}/out/debug/${levelKey}.bin`,
    );
const bytecode = readFileSync(byteCodePath);
const base64Bytecode = bytecode.toString('base64');

// client side
const bytecodeBuffer = Buffer.from(base64Bytecode, 'base64');
const input = arrayify(bytecodeBuffer) as unknown as number[];
console.log("INPUT:", input)
await contract.functions
    .verify_bytecode_test(input)
    .txParams({ gasPrice: 1, gasLimit: 1_000_000 })
    .call();

I can log the input though:

INPUT: Uint8Array(1260) [116, 0, 0, 3, 71, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 100, 93, 252, 192, 1, 16, 255, 243, 0, 93, 64, 96, 73, 93, 71, 240, 14, 19, 73, 4, 64, 118, 72, 0, 5, 93, 71, 240, 15, 19, 73, 4, 64, 118, 72, 0, 123, 114, 240, 0, 123, 54, 240, 0, 0, 26, 236, 80, 0, 145, 0, 2, 24, 93, 67, 240, 16, 16, 65, 3, 0, 93, 71, 240, 16, 16, 69, 19, 0, 114, 72, 0, 32, 40, 237, 4, 128, 95, 236, 0, 4, 80, 75, 176, 40, …]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feat Issue is a feature
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants