Coder Social home page Coder Social logo

Comments (6)

Dhaiwat10 avatar Dhaiwat10 commented on September 26, 2024 1

cc @sarahschwartz

from fuels-ts.

nedsalk avatar nedsalk commented on September 26, 2024 1

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]

from fuels-ts.

nedsalk avatar nedsalk commented on September 26, 2024 1

@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[]);

from fuels-ts.

arboleya avatar arboleya commented on September 26, 2024

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

from fuels-ts.

Dhaiwat10 avatar Dhaiwat10 commented on September 26, 2024

@arboleya precisely

from fuels-ts.

sarahschwartz avatar sarahschwartz commented on September 26, 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[]);

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, …]

from fuels-ts.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.