Skip to content

Commit

Permalink
chore: internalise ethers utilities for hexlify, arrayify, `con…
Browse files Browse the repository at this point in the history
…cat` and `BytesLike` (FuelLabs#1775)

* feat: replaces ethers usages in abi-typegen

* feat: replaces ethers usages in address

* feat: replaces ethers usages in contract

* feat: replaces ethers usages in crypto

* feat: replaces ethers usages in fuels

* feat: replaces ethers usages in hasher

* eat: replaces ethers usages in hdwallet

* feat: replaces ethers usages in merkle

* feat: replaces ethers usages in mnemonic

* feat: replaces ethers usages in predicate

* feat: replaces ethers usages in program

* feat: replaces ethers usages in providers

* feat: replaces ethers usages in script

* feat: replaces ethers usages in transactions

* feat: replaces ethers usages in signer

* feat: replaces ethers usages in wallet

* chore: fix conflicts

* feat: pick up leftover ethers usages

* chore: linting

* chore: changeset

* chore: rebuild

* chore: remove buidl file

* chore: linting
  • Loading branch information
danielbate authored Feb 21, 2024
1 parent 59432a6 commit f5466b6
Show file tree
Hide file tree
Showing 92 changed files with 328 additions and 314 deletions.
21 changes: 21 additions & 0 deletions .changeset/fast-dragons-rescue.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
---
"@fuel-ts/abi-coder": patch
"@fuel-ts/abi-typegen": patch
"@fuel-ts/account": patch
"@fuel-ts/address": patch
"@fuel-ts/contract": patch
"@fuel-ts/crypto": patch
"@fuel-ts/errors": patch
"fuels": patch
"@fuel-ts/hasher": patch
"@fuel-ts/interfaces": patch
"@fuel-ts/math": patch
"@fuel-ts/merkle": patch
"@fuel-ts/program": patch
"@fuel-ts/script": patch
"@fuel-ts/transactions": patch
"@fuel-ts/utils": patch
"@fuel-ts/versions": patch
---

Use interal utilities for arrayify, hexlify, concat and BytesLike
1 change: 1 addition & 0 deletions packages/abi-coder/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
"license": "Apache-2.0",
"dependencies": {
"@fuel-ts/crypto": "workspace:*",
"@fuel-ts/interfaces": "workspace:*",
"@fuel-ts/math": "workspace:*",
"@fuel-ts/versions": "workspace:*",
"@fuel-ts/utils": "workspace:*",
Expand Down
2 changes: 1 addition & 1 deletion packages/abi-coder/src/coders/abstract-coder.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { BytesLike } from '@fuel-ts/interfaces';
import type { BN } from '@fuel-ts/math';
import type { BytesLike } from 'ethers';

import type { Option } from './v0/option';

Expand Down
4 changes: 2 additions & 2 deletions packages/abi-coder/src/coders/v0/b256.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { ErrorCode, FuelError } from '@fuel-ts/errors';
import { bn, toHex } from '@fuel-ts/math';
import { getBytesCopy } from 'ethers';
import { arrayify } from '@fuel-ts/utils';

import { WORD_SIZE } from '../../constants';
import { Coder } from '../abstract-coder';
Expand All @@ -13,7 +13,7 @@ export class B256Coder extends Coder<string, string> {
encode(value: string): Uint8Array {
let encodedValue;
try {
encodedValue = getBytesCopy(value);
encodedValue = arrayify(value);
} catch (error) {
throw new FuelError(ErrorCode.ENCODE_ERROR, `Invalid ${this.type}.`);
}
Expand Down
4 changes: 2 additions & 2 deletions packages/abi-coder/src/coders/v0/b512.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { ErrorCode, FuelError } from '@fuel-ts/errors';
import { bn, toHex } from '@fuel-ts/math';
import { getBytesCopy } from 'ethers';
import { arrayify } from '@fuel-ts/utils';

import { WORD_SIZE } from '../../constants';
import { Coder } from '../abstract-coder';
Expand All @@ -13,7 +13,7 @@ export class B512Coder extends Coder<string, string> {
encode(value: string): Uint8Array {
let encodedValue;
try {
encodedValue = getBytesCopy(value);
encodedValue = arrayify(value);
} catch (error) {
throw new FuelError(ErrorCode.ENCODE_ERROR, `Invalid ${this.type}.`);
}
Expand Down
9 changes: 5 additions & 4 deletions packages/abi-coder/src/function-fragment.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { bufferFromString } from '@fuel-ts/crypto';
import { ErrorCode, FuelError } from '@fuel-ts/errors';
import type { BytesLike } from '@fuel-ts/interfaces';
import { bn } from '@fuel-ts/math';
import type { BytesLike } from 'ethers';
import { sha256, getBytesCopy } from 'ethers';
import { arrayify } from '@fuel-ts/utils';
import { sha256 } from 'ethers';

import { AbiCoder } from './abi-coder';
import type { DecodedValue, InputValue } from './coders/abstract-coder';
Expand Down Expand Up @@ -151,7 +152,7 @@ export class FunctionFragment<
}

decodeArguments(data: BytesLike) {
const bytes = getBytesCopy(data);
const bytes = arrayify(data);
const nonEmptyInputs = this.jsonFn.inputs.filter(
(x) => findOrThrow(this.jsonAbi.types, (t) => t.typeId === x.type).type !== '()'
);
Expand Down Expand Up @@ -204,7 +205,7 @@ export class FunctionFragment<
return [undefined, 0];
}

const bytes = getBytesCopy(data);
const bytes = arrayify(data);
const coder = AbiCoder.getCoder(this.jsonAbi, this.jsonFn.output);

return coder.decode(bytes, 0) as [DecodedValue | undefined, number];
Expand Down
5 changes: 3 additions & 2 deletions packages/abi-coder/src/interface.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
import { ErrorCode, FuelError } from '@fuel-ts/errors';
import { getBytesCopy, type BytesLike } from 'ethers';
import type { BytesLike } from '@fuel-ts/interfaces';
import { arrayify } from '@fuel-ts/utils';

import { AbiCoder } from './abi-coder';
import type { InputValue } from './coders/abstract-coder';
Expand Down Expand Up @@ -99,7 +100,7 @@ export class Interface<TAbi extends JsonAbi = JsonAbi> {

const { loggedType } = findOrThrow(this.jsonAbi.loggedTypes, (type) => type.logId === logId);

return AbiCoder.decode(this.jsonAbi, loggedType, getBytesCopy(data), 0, {
return AbiCoder.decode(this.jsonAbi, loggedType, arrayify(data), 0, {
version: this.jsonAbi.encoding,
});
}
Expand Down
6 changes: 3 additions & 3 deletions packages/abi-coder/src/utilities.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { ErrorCode, FuelError } from '@fuel-ts/errors';
import { concat, concatBytes } from '@fuel-ts/utils';
import { getBytesCopy, type BytesLike } from 'ethers';
import type { BytesLike } from '@fuel-ts/interfaces';
import { concat, concatBytes, arrayify } from '@fuel-ts/utils';

import { U64Coder } from './coders/v0/u64';
import { BYTES_CODER_TYPE, VEC_CODER_TYPE, STD_STRING_CODER_TYPE, WORD_SIZE } from './constants';
Expand Down Expand Up @@ -33,7 +33,7 @@ export function concatWithDynamicData(items: ReadonlyArray<BytesLike>): Uint8Arr
});
}

const byteArray = getBytesCopy(item);
const byteArray = arrayify(item);
totalIndex += byteArray.byteLength / WORD_SIZE;

return byteArray;
Expand Down
2 changes: 1 addition & 1 deletion packages/abi-typegen/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,10 @@
"license": "Apache-2.0",
"dependencies": {
"@fuel-ts/errors": "workspace:*",
"@fuel-ts/interfaces": "workspace:^",
"@fuel-ts/utils": "workspace:*",
"@fuel-ts/versions": "workspace:*",
"commander": "^9.4.1",
"ethers": "^6.7.1",
"glob": "^10.2.6",
"handlebars": "^4.7.7",
"mkdirp": "^1.0.4",
Expand Down
2 changes: 1 addition & 1 deletion packages/abi-typegen/src/templates/contract/bytecode.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { BytesLike } from 'ethers';
import type { BytesLike } from '@fuel-ts/interfaces';

import { renderHbsTemplate } from '../renderHbsTemplate';

Expand Down
2 changes: 1 addition & 1 deletion packages/abi-typegen/src/utils/collectBinFilePaths.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { hexlify } from 'ethers';
import { hexlify } from '@fuel-ts/utils';
import { readFileSync } from 'fs';

import { AbiTypegenProjectsEnum, getTypegenForcProject } from '../../test/fixtures/forc-projects';
Expand Down
2 changes: 1 addition & 1 deletion packages/abi-typegen/src/utils/collectBinFilePaths.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { hexlify } from 'ethers';
import { hexlify } from '@fuel-ts/utils';
import { existsSync, readFileSync } from 'fs';

import type { ProgramTypeEnum } from '../types/enums/ProgramTypeEnum';
Expand Down
11 changes: 5 additions & 6 deletions packages/account/src/account.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@ import { Address } from '@fuel-ts/address';
import { BaseAssetId } from '@fuel-ts/address/configs';
import { ErrorCode, FuelError } from '@fuel-ts/errors';
import { AbstractAccount } from '@fuel-ts/interfaces';
import type { AbstractAddress } from '@fuel-ts/interfaces';
import type { AbstractAddress, BytesLike } from '@fuel-ts/interfaces';
import type { BigNumberish, BN } from '@fuel-ts/math';
import { bn } from '@fuel-ts/math';
import { getBytesCopy } from 'ethers';
import type { BytesLike } from 'ethers';
import { arrayify } from '@fuel-ts/utils';

import type { FuelConnector } from './connectors';
import type {
Expand Down Expand Up @@ -453,14 +452,14 @@ export class Account extends AbstractAccount {

const recipientAddress = Address.fromAddressOrString(recipient);
// add recipient and amount to the transaction script code
const recipientDataArray = getBytesCopy(
const recipientDataArray = arrayify(
'0x'.concat(recipientAddress.toHexString().substring(2).padStart(64, '0'))
);
const amountDataArray = getBytesCopy(
const amountDataArray = arrayify(
'0x'.concat(bn(amount).toHex().substring(2).padStart(16, '0'))
);
const script = new Uint8Array([
...getBytesCopy(withdrawScript.bytes),
...arrayify(withdrawScript.bytes),
...recipientDataArray,
...amountDataArray,
]);
Expand Down
26 changes: 11 additions & 15 deletions packages/account/src/hdwallet/hdwallet.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
import { ErrorCode, FuelError } from '@fuel-ts/errors';
import type { BytesLike } from '@fuel-ts/interfaces';
import { bn, toBytes, toHex } from '@fuel-ts/math';
import type { BytesLike } from 'ethers';
import { arrayify, hexlify, concat } from '@fuel-ts/utils';
import {
toBeHex,
dataSlice,
hexlify,
encodeBase58,
decodeBase58,
sha256,
computeHmac,
ripemd160,
getBytesCopy,
concat,
} from 'ethers';

import { Mnemonic } from '../mnemonic';
Expand Down Expand Up @@ -122,9 +120,9 @@ class HDWallet {
* @returns A new instance of HDWallet on the derived index
*/
deriveIndex(index: number) {
const privateKey = this.privateKey && getBytesCopy(this.privateKey);
const publicKey = getBytesCopy(this.publicKey);
const chainCode = getBytesCopy(this.chainCode);
const privateKey = this.privateKey && arrayify(this.privateKey);
const publicKey = arrayify(this.publicKey);
const chainCode = arrayify(this.chainCode);
const data = new Uint8Array(37);

if (index & HARDENED_INDEX) {
Expand All @@ -138,13 +136,13 @@ class HDWallet {
// 33 bytes: 0x00 || private key
data.set(privateKey, 1);
} else {
data.set(getBytesCopy(this.publicKey));
data.set(arrayify(this.publicKey));
}

// child number: ser32(i)
data.set(toBytes(index, 4), 33);

const bytes = getBytesCopy(computeHmac('sha512', chainCode, data));
const bytes = arrayify(computeHmac('sha512', chainCode, data));
const IL = bytes.slice(0, 32);
const IR = bytes.slice(32);

Expand Down Expand Up @@ -209,9 +207,7 @@ class HDWallet {
// first 32 bites from the key
const key =
this.privateKey != null && !isPublic ? concat(['0x00', this.privateKey]) : this.publicKey;
const extendedKey = getBytesCopy(
concat([prefix, depth, parentFingerprint, index, chainCode, key])
);
const extendedKey = arrayify(concat([prefix, depth, parentFingerprint, index, chainCode, key]));

return base58check(extendedKey);
}
Expand All @@ -226,14 +222,14 @@ class HDWallet {
const masterKey = Mnemonic.masterKeysFromSeed(seed);

return new HDWallet({
chainCode: getBytesCopy(masterKey.slice(32)),
privateKey: getBytesCopy(masterKey.slice(0, 32)),
chainCode: arrayify(masterKey.slice(32)),
privateKey: arrayify(masterKey.slice(0, 32)),
});
}

static fromExtendedKey(extendedKey: string) {
const decoded = toBeHex(decodeBase58(extendedKey));
const bytes = getBytesCopy(decoded);
const bytes = arrayify(decoded);
const validChecksum = base58check(bytes.slice(0, 78)) === extendedKey;

if (bytes.length !== 82 || !isValidExtendedKey(bytes)) {
Expand Down
24 changes: 8 additions & 16 deletions packages/account/src/mnemonic/mnemonic.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,8 @@
import { randomBytes } from '@fuel-ts/crypto';
import { ErrorCode, FuelError } from '@fuel-ts/errors';
import type { BytesLike } from 'ethers';
import {
concat,
hexlify,
dataSlice,
pbkdf2,
sha256,
computeHmac,
encodeBase58,
getBytesCopy,
} from 'ethers';
import type { BytesLike } from '@fuel-ts/interfaces';
import { arrayify, hexlify, concat } from '@fuel-ts/utils';
import { dataSlice, pbkdf2, sha256, computeHmac, encodeBase58 } from 'ethers';

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

Expand Down Expand Up @@ -113,7 +105,7 @@ class Mnemonic {
* @returns 64-byte array contains privateKey and chainCode as described on BIP39
*/
static entropyToMnemonic(entropy: BytesLike, wordlist: Array<string> = english): string {
const entropyBytes = getBytesCopy(entropy);
const entropyBytes = arrayify(entropy);

assertWordList(wordlist);
assertEntropy(entropyBytes);
Expand Down Expand Up @@ -200,7 +192,7 @@ class Mnemonic {
* @returns 64-byte array contains privateKey and chainCode as described on BIP39
*/
static masterKeysFromSeed(seed: string): Uint8Array {
const seedArray = getBytesCopy(seed);
const seedArray = arrayify(seed);

if (seedArray.length < 16 || seedArray.length > 64) {
throw new FuelError(
Expand All @@ -209,7 +201,7 @@ class Mnemonic {
);
}

return getBytesCopy(computeHmac('sha512', MasterSecret, seedArray));
return arrayify(computeHmac('sha512', MasterSecret, seedArray));
}

/**
Expand All @@ -221,7 +213,7 @@ class Mnemonic {
*/
static seedToExtendedKey(seed: string, testnet: boolean = false): string {
const masterKey = Mnemonic.masterKeysFromSeed(seed);
const prefix = getBytesCopy(testnet ? TestnetPRV : MainnetPRV);
const prefix = arrayify(testnet ? TestnetPRV : MainnetPRV);
const depth = '0x00';
const fingerprint = '0x00000000';
const index = '0x00000000';
Expand Down Expand Up @@ -256,7 +248,7 @@ class Mnemonic {
*/
static generate(size: number = 32, extraEntropy: BytesLike = '') {
const entropy = extraEntropy
? sha256(concat([randomBytes(size), getBytesCopy(extraEntropy)]))
? sha256(concat([randomBytes(size), arrayify(extraEntropy)]))
: randomBytes(size);
return Mnemonic.entropyToMnemonic(entropy);
}
Expand Down
11 changes: 6 additions & 5 deletions packages/account/src/mnemonic/utils.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { ErrorCode, FuelError } from '@fuel-ts/errors';
import { getBytesCopy, sha256 } from 'ethers';
import type { BytesLike } from 'ethers';
import type { BytesLike } from '@fuel-ts/interfaces';
import { arrayify } from '@fuel-ts/utils';
import { sha256 } from 'ethers';

/* 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>;
Expand Down Expand Up @@ -92,7 +93,7 @@ export function entropyToMnemonicIndices(entropy: Uint8Array): Array<number> {

// Compute the checksum bits
const checksumBits = entropy.length / 4;
const checksum = getBytesCopy(sha256(entropy))[0] & getUpperMask(checksumBits);
const checksum = arrayify(sha256(entropy))[0] & getUpperMask(checksumBits);

// Shift the checksum into the word indices
indices[indices.length - 1] <<= checksumBits;
Expand All @@ -103,7 +104,7 @@ export function entropyToMnemonicIndices(entropy: Uint8Array): Array<number> {

export function mnemonicWordsToEntropy(words: Array<string>, wordlist: Array<string>): BytesLike {
const size = Math.ceil((11 * words.length) / 8);
const entropy = getBytesCopy(new Uint8Array(size));
const entropy = arrayify(new Uint8Array(size));

let offset = 0;
for (let i = 0; i < words.length; i += 1) {
Expand All @@ -125,7 +126,7 @@ export function mnemonicWordsToEntropy(words: Array<string>, wordlist: Array<str
const entropyBits = (32 * words.length) / 3;
const checksumBits = words.length / 3;
const checksumMask = getUpperMask(checksumBits);
const checksum = getBytesCopy(sha256(entropy.slice(0, entropyBits / 8)))[0] & checksumMask;
const checksum = arrayify(sha256(entropy.slice(0, entropyBits / 8)))[0] & checksumMask;

if (checksum !== (entropy[entropy.length - 1] & checksumMask)) {
throw new FuelError(
Expand Down
4 changes: 2 additions & 2 deletions packages/account/src/predicate/predicate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { ErrorCode, FuelError } from '@fuel-ts/errors';
import type { AbstractAddress, BytesLike } from '@fuel-ts/interfaces';
import type { BigNumberish } from '@fuel-ts/math';
import { ByteArrayCoder, InputType } from '@fuel-ts/transactions';
import { getBytesCopy, hexlify } from 'ethers';
import { arrayify, hexlify } from '@fuel-ts/utils';

import { Account } from '../account';
import type { TxParamsType } from '../account';
Expand Down Expand Up @@ -181,7 +181,7 @@ export class Predicate<ARGS extends InputValue[]> extends Account {
jsonAbi?: JsonAbi,
configurableConstants?: { [name: string]: unknown }
) {
let predicateBytes = getBytesCopy(bytes);
let predicateBytes = arrayify(bytes);
let abiInterface: Interface | undefined;

if (jsonAbi) {
Expand Down
Loading

0 comments on commit f5466b6

Please sign in to comment.