Skip to content

Commit

Permalink
Merge branch 'master' of github.com:FuelLabs/fuels-ts into ps/fix/lin…
Browse files Browse the repository at this point in the history
…ting-markdown-links
  • Loading branch information
petertonysmith94 committed May 14, 2024
2 parents 62b8bd9 + 0651a5f commit 8b789ea
Show file tree
Hide file tree
Showing 33 changed files with 475 additions and 177 deletions.
4 changes: 4 additions & 0 deletions .changeset/curly-schools-obey.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
---

build: upgrade `turbo` to v1.13.3
4 changes: 4 additions & 0 deletions .changeset/green-houses-applaud.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
---

build: remove dependabot need to report coverage
5 changes: 5 additions & 0 deletions .changeset/loud-fans-hunt.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"fuels": patch
---

fix: ignore libraries in `fuels` CLI
2 changes: 2 additions & 0 deletions .changeset/rotten-bananas-explode.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
---
---
7 changes: 7 additions & 0 deletions .changeset/tough-cows-hide.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"@fuel-ts/abi-coder": patch
"@fuel-ts/account": patch
"@fuel-ts/utils": patch
---

chore: remove `ethers` from `abi-coder`
3 changes: 1 addition & 2 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ jobs:

- name: Report Coverage
uses: thollander/actions-comment-pull-request@v2
if: ${{ steps.findPr.outputs.number }}
if: ${{ steps.findPr.outputs.number }} && github.actor != 'dependabot[bot]'
with:
filePath: coverage/report/coverage-diff.txt
pr_number: ${{ (steps.findPr.outputs.number) }}
Expand All @@ -129,7 +129,6 @@ jobs:
create_if_not_exists: true

create-changeset:
needs: [test]
name: Create Changeset
runs-on: ubuntu-latest
if: github.actor == 'dependabot[bot]'
Expand Down
42 changes: 23 additions & 19 deletions apps/docs-snippets/src/guide/types/vector.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,27 +55,31 @@ describe(__filename, () => {
expect(value.isActive).toEqual(employees[1].isActive);
});

// TODO: Unskip test after sway-libs become compatible with latest forc (0.52+)
it.skip('should successfully execute a contract call with a bytecode input', async () => {
const bytecodeContract = await createAndDeployContractFromProject(
DocSnippetProjectsEnum.BYTECODE_INPUT
);
const bytecodePath = join(
__dirname,
'../../../test/fixtures/forc-projects/bytecode-input/out/release/bytecode-input.bin'
);
it(
'should successfully execute a contract call with a bytecode input',
async () => {
const bytecodeContract = await createAndDeployContractFromProject(
DocSnippetProjectsEnum.BYTECODE_INPUT
);
const bytecodePath = join(
__dirname,
'../../../test/fixtures/forc-projects/bytecode-input/out/release/bytecode-input.bin'
);

// #region vector-bytecode-input-ts
// #import { arrayify, readFile };
// #region vector-bytecode-input-ts
// #import { arrayify, readFile };

const bytecode = await readFile(bytecodePath);
const bytecode = await readFile(bytecodePath);
const bytecodeAsVecU8 = arrayify(bytecode);

const { value: bytecodeRoot } = await bytecodeContract.functions
.compute_bytecode_root(arrayify(bytecode))
.call();
// #endregion vector-bytecode-input-ts
const { value: bytecodeRoot } = await bytecodeContract.functions
.compute_bytecode_root(bytecodeAsVecU8)
.call();
// #endregion vector-bytecode-input-ts

expect(bytecodeRoot).toBeDefined();
expect(bytecodeRoot.length).toBe(66);
});
expect(bytecodeRoot).toBeDefined();
expect(bytecodeRoot.length).toBe(66);
},
{ timeout: 10000 }
);
});
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,4 @@ authors = ["Fuel Labs <[email protected]>"]
license = "Apache-2.0"
name = "bytecode-input"

# TODO: Uncomment bytecode-related stuff
# This requires sway-libs to be compatible with latest forc (0.52+)

# [dependencies]
# bytecode = { git = "https://github.com/FuelLabs/sway-libs", tag = "v0.19.0" }
[dependencies]
Original file line number Diff line number Diff line change
@@ -1,21 +1,14 @@
contract;

// TODO: Uncomment bytecode-related stuff
// This requires sway-libs to be compatible with latest forc (0.52+)
// use bytecode::*;


abi MyContract {
// fn compute_bytecode_root(bytecode_input: Vec<u8>) -> b256;
fn compute_bytecode_root(bytecode_input: Vec<u8>) -> bool;
fn compute_bytecode_root(bytecode_input: Vec<u8>) -> b256;
}

impl MyContract for Contract {
// #region vector-bytecode-input-sway
fn compute_bytecode_root(bytecode_input: Vec<u8>) -> bool {
// let root = compute_bytecode_root(bytecode_input);
// return root;
return true;
fn compute_bytecode_root(bytecode_input: Vec<u8>) -> b256 {
//simply return a fixed b256 value created from a hexadecimal string from testing purposes
return 0x0102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f20;
}
// #endregion vector-bytecode-input-sway
}
27 changes: 3 additions & 24 deletions apps/docs/src/guide/types/vectors.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,32 +20,11 @@ The code snippet below demonstrates how to call this Sway contract method, which

<<< @/../../docs-snippets/src/guide/types/vector.test.ts#vector-4{ts:line-numbers}

## Working with Bytecode in the SDK
## Converting Bytecode to Vectors

Some Sway functions require you to pass in bytecode to the function. The type of the bytecode parameter is usually `Vec<u8>`.
Some functions require you to pass in bytecode to the function. The type of the bytecode parameter is usually `Vec<u8>`, here's an example of how to pass bytecode to a function:

Take the `compute_bytecode_root` function from the [`bytecode` Sway library](https://github.com/FuelLabs/sway-libs/tree/master/libs/src/bytecode.sw), for example.

<!-- <<< @/../../docs-snippets/test/fixtures/forc-projects/bytecode-input/src/main.sw#vector-bytecode-input-sway{ts:line-numbers} -->

<!-- TODO: Uncomment swap hardcoded snippet -->

```rust
contract;

use bytecode::*;

abi MyContract {
fn compute_bytecode_root(bytecode_input: Vec<u8>) -> b256;
}

impl MyContract for Contract {
fn compute_bytecode_root(bytecode_input: Vec<u8>) -> bool {
let root = compute_bytecode_root(bytecode_input);
return root;
}
}
```
<<< @/../../docs-snippets/test/fixtures/forc-projects/bytecode-input/src/main.sw#vector-bytecode-input-sway{ts:line-numbers}

To pass bytecode to this function, you can make use of the `arrayify` function to convert the bytecode file contents into a `UInt8Array`, the TS compatible type for Sway's `Vec<u8>` type and pass it the function like so:

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@
"ts-generator": "^0.1.1",
"tsup": "^6.7.0",
"tsx": "^4.7.0",
"turbo": "^1.8.8",
"turbo": "^1.13.3",
"typescript": "~5.2.2",
"vite": "^5.2.10",
"vite-plugin-node-polyfills": "^0.21.0",
Expand Down
1 change: 0 additions & 1 deletion packages/abi-coder/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
"@fuel-ts/interfaces": "workspace:*",
"@fuel-ts/math": "workspace:*",
"@fuel-ts/utils": "workspace:*",
"ethers": "^6.7.1",
"type-fest": "^3.1.0"
}
}
3 changes: 1 addition & 2 deletions packages/abi-coder/src/encoding/coders/v0/StdStringCoder.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { ErrorCode, FuelError } from '@fuel-ts/errors';
import { bn } from '@fuel-ts/math';
import { concat } from '@fuel-ts/utils';
import { toUtf8Bytes, toUtf8String } from 'ethers';
import { concat, toUtf8String, toUtf8Bytes } from '@fuel-ts/utils';

import { WORD_SIZE } from '../../../utils/constants';
import type { Uint8ArrayWithDynamicData } from '../../../utils/utilities';
Expand Down
3 changes: 1 addition & 2 deletions packages/abi-coder/src/encoding/coders/v0/StringCoder.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { ErrorCode, FuelError } from '@fuel-ts/errors';
import { concat } from '@fuel-ts/utils';
import { toUtf8Bytes, toUtf8String } from 'ethers';
import { concat, toUtf8Bytes, toUtf8String } from '@fuel-ts/utils';

import { Coder } from '../AbstractCoder';

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { ErrorCode, FuelError } from '@fuel-ts/errors';
import { bn } from '@fuel-ts/math';
import { toUtf8Bytes, toUtf8String } from 'ethers';
import { toUtf8Bytes, toUtf8String } from '@fuel-ts/utils';

import { WORD_SIZE } from '../../../utils/constants';
import { Coder } from '../AbstractCoder';
Expand Down
2 changes: 1 addition & 1 deletion packages/abi-coder/src/encoding/coders/v1/StrSliceCoder.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { ErrorCode, FuelError } from '@fuel-ts/errors';
import { bn } from '@fuel-ts/math';
import { toUtf8Bytes, toUtf8String } from 'ethers';
import { toUtf8Bytes, toUtf8String } from '@fuel-ts/utils';

import { WORD_SIZE } from '../../../utils/constants';
import { Coder } from '../AbstractCoder';
Expand Down
2 changes: 1 addition & 1 deletion packages/abi-coder/src/encoding/coders/v1/StringCoder.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { ErrorCode, FuelError } from '@fuel-ts/errors';
import { toUtf8Bytes, toUtf8String } from 'ethers';
import { toUtf8Bytes, toUtf8String } from '@fuel-ts/utils';

import { Coder } from '../AbstractCoder';

Expand Down
10 changes: 2 additions & 8 deletions packages/account/src/mnemonic/mnemonic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,12 @@ import { randomBytes, pbkdf2, computeHmac } from '@fuel-ts/crypto';
import { ErrorCode, FuelError } from '@fuel-ts/errors';
import { sha256 } from '@fuel-ts/hasher';
import type { BytesLike } from '@fuel-ts/interfaces';
import { arrayify, hexlify, concat, dataSlice, encodeBase58 } from '@fuel-ts/utils';
import { arrayify, hexlify, concat, dataSlice, encodeBase58, toUtf8Bytes } from '@fuel-ts/utils';

import { english } from '../wordlists';

import type { MnemonicPhrase } from './utils';
import {
entropyToMnemonicIndices,
getWords,
getPhrase,
mnemonicWordsToEntropy,
toUtf8Bytes,
} from './utils';
import { entropyToMnemonicIndices, getWords, getPhrase, mnemonicWordsToEntropy } from './utils';

//
// Constants
Expand Down
39 changes: 0 additions & 39 deletions packages/account/src/mnemonic/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,45 +6,6 @@ import { arrayify } from '@fuel-ts/utils';
/* Mnemonic phrase composed by words from the provided wordlist it can be a text or a array of words */
export type MnemonicPhrase = string | Array<string>;

export function toUtf8Bytes(stri: string): Uint8Array {
const str = stri.normalize('NFKD');

const result = [];
for (let i = 0; i < str.length; i += 1) {
const c = str.charCodeAt(i);

if (c < 0x80) {
result.push(c);
} else if (c < 0x800) {
result.push((c >> 6) | 0xc0);
result.push((c & 0x3f) | 0x80);
} else if ((c & 0xfc00) === 0xd800) {
i += 1;
const c2 = str.charCodeAt(i);

if (i >= str.length || (c2 & 0xfc00) !== 0xdc00) {
throw new FuelError(
ErrorCode.INVALID_INPUT_PARAMETERS,
'Invalid UTF-8 in the input string.'
);
}

// Surrogate Pair
const pair = 0x10000 + ((c & 0x03ff) << 10) + (c2 & 0x03ff);
result.push((pair >> 18) | 0xf0);
result.push(((pair >> 12) & 0x3f) | 0x80);
result.push(((pair >> 6) & 0x3f) | 0x80);
result.push((pair & 0x3f) | 0x80);
} else {
result.push((c >> 12) | 0xe0);
result.push(((c >> 6) & 0x3f) | 0x80);
result.push((c & 0x3f) | 0x80);
}
}

return Uint8Array.from(result);
}

// Returns a byte with the LSB bits set
function getLowerMask(bits: number): number {
return (1 << bits) - 1;
Expand Down
7 changes: 3 additions & 4 deletions packages/fuel-gauge/src/bytecode-sway-lib.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@ import { getSetupContract } from './utils';
/**
* @group node
*/
// TODO: Unskip test after sway-libs become compatible with latest forc (0.52+)
describe.skip('bytecode computations', () => {
describe('bytecode computations', () => {
test('compute_bytecode_root', async () => {
const { binHexlified: bytecodeFromFile } = getFuelGaugeForcProject(
FuelGaugeProjectsEnum.CALL_TEST_CONTRACT
Expand Down Expand Up @@ -42,7 +41,7 @@ describe.skip('bytecode computations', () => {
{
bits: contract.id.toB256(),
},
arrayify(bytecodeFromFile)
Array.from(arrayify(bytecodeFromFile))
)
.call();

Expand All @@ -64,7 +63,7 @@ describe.skip('bytecode computations', () => {
const contract = await setupContract();

const { value } = await contract.functions
.compute_predicate_address(arrayify(defaultPredicateBytecode))
.compute_predicate_address(Array.from(arrayify(defaultPredicateBytecode)))
.call();

expect(value.bits).toEqual(address.toB256());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,4 @@ authors = ["Fuel Labs <[email protected]>"]
license = "Apache-2.0"
name = "bytecode-sway-lib"

# TODO: Uncomment bytecode-related stuff
# This requires sway-libs to be compatible with latest forc (0.52+)

# [dependencies]
# bytecode = { git = "https://github.com/FuelLabs/sway-libs", tag = "v0.19.0" }
[dependencies]
Original file line number Diff line number Diff line change
@@ -1,33 +1,24 @@
contract;

// TODO: Uncomment bytecode-related stuff
// This requires sway-libs to be compatible with latest forc (0.52+)
// use bytecode::*;


abi MyContract {
fn compute_bytecode_root(bytecode_input: Vec<u8>);

fn verify_contract_bytecode(contract_id: ContractId, bytecode: Vec<u8>) -> bool;

// fn compute_predicate_address(bytecode: Vec<u8>) -> Address;
fn compute_predicate_address(bytecode: Vec<u8>) -> bool;
fn compute_predicate_address(bytecode: Vec<u8>) -> Address;
}

impl MyContract for Contract {
fn compute_bytecode_root(bytecode_input: Vec<u8>) {
// let mut bytecode = bytecode_input;
// let root = compute_bytecode_root(bytecode);
// log(root);
// simply logs the hexidecimal b256 string of the bytecode input for testing purposes
log(0x0102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f20);
}

fn verify_contract_bytecode(contract_id: ContractId, bytecode: Vec<u8>) -> bool {
// verify_contract_bytecode(contract_id, bytecode);
return true;
}

fn compute_predicate_address(bytecode: Vec<u8>) -> bool {
// return compute_predicate_address(bytecode);
return true;
fn compute_predicate_address(bytecode: Vec<u8>) -> Address {
return Address::from(0x6b6ef590390f0a7de75f8275ab5d7877c17236caba2514039c6565ec15f79111);
}
}
Loading

0 comments on commit 8b789ea

Please sign in to comment.