Skip to content

Commit 727c5b9

Browse files
authored
Merge branch 'master' into db/feat/implement-ethers-utilities
2 parents 1350232 + 4d1f623 commit 727c5b9

File tree

17 files changed

+169
-23
lines changed

17 files changed

+169
-23
lines changed

.changeset/gold-falcons-nail.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@fuel-ts/program": patch
3+
---
4+
5+
Add transaction id helper function to base invocation scope

.changeset/selfish-rocks-tap.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"fuels": patch
3+
---
4+
5+
Updated npm keywords
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
"@fuel-ts/address": minor
3+
"@fuel-ts/contract": minor
4+
"@fuel-ts/errors": patch
5+
---
6+
7+
Remove hexlify logic on values that are not hex

apps/docs-snippets/src/guide/scripts/script-with-configurable.test.ts

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,23 @@
1-
import type { WalletUnlocked } from 'fuels';
1+
import type { WalletUnlocked, Provider } from 'fuels';
22
import { Script, BN } from 'fuels';
33

4-
import { DocSnippetProjectsEnum, getDocsSnippetsForcProject } from '../../../test/fixtures/forc-projects';
4+
import {
5+
DocSnippetProjectsEnum,
6+
getDocsSnippetsForcProject,
7+
} from '../../../test/fixtures/forc-projects';
58
import { getTestWallet } from '../../utils';
69

710
describe(__filename, () => {
811
let wallet: WalletUnlocked;
912
let gasPrice: BN;
13+
let provider: Provider;
1014
const { abiContents, binHexlified } = getDocsSnippetsForcProject(
1115
DocSnippetProjectsEnum.SUM_SCRIPT
1216
);
1317

1418
beforeAll(async () => {
1519
wallet = await getTestWallet();
20+
provider = wallet.provider;
1621
({ minGasPrice: gasPrice } = wallet.provider.getGasConfig());
1722
});
1823

@@ -35,4 +40,35 @@ describe(__filename, () => {
3540
expect(new BN(value as number).toNumber()).toEqual(expectedTotal);
3641
// #endregion script-with-configurable-contants-2
3742
});
43+
44+
it('prepares a script and retrieves the id before submission', async () => {
45+
const argument = 10;
46+
const expected = 20;
47+
48+
// #region preparing-scripts
49+
const script = new Script(binHexlified, abiContents, wallet);
50+
const { minGasPrice, maxGasPerTx } = provider.getGasConfig();
51+
52+
const tx = await script.functions.main(argument);
53+
54+
// Set the call parameters
55+
tx.callParams({ gasLimit: 100 });
56+
57+
// Set the transaction parameters
58+
tx.txParams({ gasPrice: minGasPrice, gasLimit: maxGasPerTx });
59+
60+
// Get the entire transaction request prior to
61+
const txRequest = await tx.getTransactionRequest();
62+
63+
// Get the transaction ID
64+
const txId = await tx.getTransactionId();
65+
66+
// Retrieve the value of the call and the actual gas used
67+
const { value, gasUsed } = await tx.call();
68+
// #endregion preparing-scripts
69+
expect(txRequest).toBeDefined();
70+
expect(txId).toBeDefined();
71+
expect(new BN(value as number).toNumber()).toEqual(expected);
72+
expect(new BN(gasUsed).toNumber()).toBeGreaterThan(0);
73+
});
3874
});

apps/docs/.vitepress/config.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -363,6 +363,10 @@ export default defineConfig({
363363
text: 'Calling A Script',
364364
link: '/guide/scripts/calling-a-script',
365365
},
366+
{
367+
text: 'Preparing A Script',
368+
link: '/guide/scripts/preparing-a-script',
369+
},
366370
{
367371
text: 'Script With Configurable Constants',
368372
link: '/guide/scripts/script-with-configurable-constants',
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Preparing a Script Transaction
2+
3+
Akin to Contracts, we can configure the [call parameters](../contracts/call-parameters.md) and [transaction parameters](../contracts/transaction-parameters.md) for Scripts, as well as retrieve the entire transaction request or transaction ID prior to submission.
4+
5+
<<< @/../../docs-snippets/src/guide/scripts/script-with-configurable.test.ts#preparing-scripts{ts:line-numbers}

packages/address/src/address.test.ts

Lines changed: 36 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -87,9 +87,9 @@ describe('Address utils', () => {
8787
});
8888

8989
test('isB256 (no hex prefix)', () => {
90-
const result = utils.isB256('ef86afa9696cf0dc6385e2c407a6e159a1103cefb7e2ae0636fb33d3cb2a9e4a');
90+
const result = utils.isB256(ADDRESS_B256.slice(2));
9191

92-
expect(result).toBeTruthy();
92+
expect(result).toBeFalsy();
9393
});
9494

9595
test('isB256 (using toB256)', () => {
@@ -123,11 +123,9 @@ describe('Address utils', () => {
123123
});
124124

125125
test('isPublicKey (no hex prefix)', () => {
126-
const result = utils.isPublicKey(
127-
'2f34bc0df4db0ec391792cedb05768832b49b1aa3a2dd8c30054d1af00f67d00b74b7acbbf3087c8e0b1a4c343db50aa471d21f278ff5ce09f07795d541fb47e'
128-
);
126+
const result = utils.isPublicKey(PUBLIC_KEY.slice(2));
129127

130-
expect(result).toBeTruthy();
128+
expect(result).toBeFalsy();
131129
});
132130

133131
test('isEvmAddress (EvmAddress)', () => {
@@ -155,9 +153,9 @@ describe('Address utils', () => {
155153
});
156154

157155
test('isEvmAddress (no hex prefix)', () => {
158-
const result = utils.isEvmAddress('07a6e159a1103cefb7e2ae0636fb33d3cb2a9e4a');
156+
const result = utils.isEvmAddress(ADDRESS_EVM.slice(2));
159157

160-
expect(result).toBeTruthy();
158+
expect(result).toBeFalsy();
161159
});
162160

163161
test('getBytesFromBech32 (bech32 to Uint8Array)', () => {
@@ -256,13 +254,33 @@ describe('Address class', () => {
256254
expect(address.toB256()).toEqual(signMessageTest.b256Address);
257255
});
258256

257+
test('create an Address class using invalid public key (no hex prefix)', async () => {
258+
const address = PUBLIC_KEY.slice(2);
259+
260+
const expectedError = new FuelError(
261+
FuelError.CODES.INVALID_PUBLIC_KEY,
262+
`Invalid Public Key: ${address}.`
263+
);
264+
await expectToThrowFuelError(() => Address.fromPublicKey(address), expectedError);
265+
});
266+
259267
test('create an Address class using b256Address', () => {
260268
const address = Address.fromB256(ADDRESS_B256);
261269

262270
expect(address.toAddress()).toEqual(ADDRESS_BECH32);
263271
expect(address.toB256()).toEqual(ADDRESS_B256);
264272
});
265273

274+
test('create an Address class using invalid b256Address (no hex prefix)', async () => {
275+
const address = ADDRESS_B256.slice(2);
276+
277+
const expectedError = new FuelError(
278+
FuelError.CODES.INVALID_B256_ADDRESS,
279+
`Invalid B256 Address: ${address}.`
280+
);
281+
await expectToThrowFuelError(() => Address.fromB256(address), expectedError);
282+
});
283+
266284
test('when parsing to JSON it should show the bech32 address', () => {
267285
const result = Address.fromB256(signMessageTest.b256Address);
268286
expect(JSON.stringify(result)).toEqual(`"${signMessageTest.address}"`);
@@ -334,4 +352,14 @@ describe('Address class', () => {
334352
expect(address.toEvmAddress()).toMatchObject(evmAddressWrapped);
335353
expect(address.toB256()).toEqual(ADDRESS_B256_EVM_PADDED);
336354
});
355+
356+
test('create an Address class using invalid Evm Address (no hex prefix)', async () => {
357+
const address = ADDRESS_EVM.slice(2);
358+
359+
const expectedError = new FuelError(
360+
FuelError.CODES.INVALID_EVM_ADDRESS,
361+
`Invalid Evm Address: ${address}.`
362+
);
363+
await expectToThrowFuelError(() => Address.fromEvmAddress(address), expectedError);
364+
});
337365
});

packages/address/src/address.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,10 @@ export default class Address extends AbstractAddress {
134134
* @returns A new `Address` instance
135135
*/
136136
static fromPublicKey(publicKey: string): Address {
137+
if (!isPublicKey(publicKey)) {
138+
throw new FuelError(FuelError.CODES.INVALID_PUBLIC_KEY, `Invalid Public Key: ${publicKey}.`);
139+
}
140+
137141
const b256Address = sha256(hexlify(getBytesCopy(publicKey)));
138142
return new Address(toBech32(b256Address));
139143
}
@@ -145,6 +149,13 @@ export default class Address extends AbstractAddress {
145149
* @returns A new `Address` instance
146150
*/
147151
static fromB256(b256Address: string): Address {
152+
if (!isB256(b256Address)) {
153+
throw new FuelError(
154+
FuelError.CODES.INVALID_B256_ADDRESS,
155+
`Invalid B256 Address: ${b256Address}.`
156+
);
157+
}
158+
148159
return new Address(toBech32(b256Address));
149160
}
150161

@@ -218,6 +229,13 @@ export default class Address extends AbstractAddress {
218229
* @returns A new `Address` instance
219230
*/
220231
static fromEvmAddress(evmAddress: string): Address {
232+
if (!isEvmAddress(evmAddress)) {
233+
throw new FuelError(
234+
FuelError.CODES.INVALID_EVM_ADDRESS,
235+
`Invalid Evm Address: ${evmAddress}.`
236+
);
237+
}
238+
221239
const paddedAddress = padFirst12BytesOfEvmAddress(evmAddress);
222240

223241
return new Address(toBech32(paddedAddress));

packages/address/src/utils.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ export function isBech32(address: BytesLike): boolean {
6161
* @hidden
6262
*/
6363
export function isB256(address: string): boolean {
64-
return (address.length === 66 || address.length === 64) && /(0x)?[0-9a-f]{64}$/i.test(address);
64+
return address.length === 66 && /(0x)[0-9a-f]{64}$/i.test(address);
6565
}
6666

6767
/**
@@ -70,7 +70,7 @@ export function isB256(address: string): boolean {
7070
* @hidden
7171
*/
7272
export function isPublicKey(address: string): boolean {
73-
return (address.length === 130 || address.length === 128) && /(0x)?[0-9a-f]{128}$/i.test(address);
73+
return address.length === 130 && /(0x)[0-9a-f]{128}$/i.test(address);
7474
}
7575

7676
/**
@@ -79,7 +79,7 @@ export function isPublicKey(address: string): boolean {
7979
* @hidden
8080
*/
8181
export function isEvmAddress(address: string): boolean {
82-
return (address.length === 42 || address.length === 40) && /(0x)?[0-9a-f]{40}$/i.test(address);
82+
return address.length === 42 && /(0x)[0-9a-f]{40}$/i.test(address);
8383
}
8484

8585
/**
@@ -187,6 +187,5 @@ export const padFirst12BytesOfEvmAddress = (address: string): B256AddressEvm =>
187187
throw new FuelError(FuelError.CODES.INVALID_EVM_ADDRESS, 'Invalid EVM address format.');
188188
}
189189

190-
const prefixedAddress = address.startsWith('0x') ? address : `0x${address}`;
191-
return prefixedAddress.replace('0x', '0x000000000000000000000000') as B256AddressEvm;
190+
return address.replace('0x', '0x000000000000000000000000') as B256AddressEvm;
192191
};

packages/contract/src/contract-factory.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,8 +92,8 @@ export default class ContractFactory {
9292
createTransactionRequest(deployContractOptions?: DeployContractOptions) {
9393
const storageSlots = deployContractOptions?.storageSlots
9494
?.map(({ key, value }) => ({
95-
key: hexlifyWithPrefix(key),
96-
value: hexlifyWithPrefix(value),
95+
key: hexlifyWithPrefix(key, true),
96+
value: hexlifyWithPrefix(value, true),
9797
}))
9898
.sort(({ key: keyA }, { key: keyB }) => keyA.localeCompare(keyB));
9999

0 commit comments

Comments
 (0)