Skip to content

Commit

Permalink
fix: wrong signature index at EVMWalletConnector (#112)
Browse files Browse the repository at this point in the history
Co-authored-by: LuizAsFight <felipebolsonigomes@gmail.com>
  • Loading branch information
Torres-ssf and LuizAsFight authored Jun 24, 2024
1 parent 204bf27 commit a87a9a4
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/old-eyes-worry.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@fuel-connectors/evm-connector": patch
---

fix: wrong signature index at `EVMWalletConnector`
5 changes: 4 additions & 1 deletion packages/evm-connector/src/EvmWalletConnector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import {
} from './types';
import type { EIP1193Provider } from './utils/eip-1193';
import { METAMASK_ICON } from './utils/metamask-icon';
import { getSignatureIndex } from './utils/predicate';

export class EVMWalletConnector extends FuelConnector {
name = 'Metamask';
Expand Down Expand Up @@ -351,11 +352,13 @@ export class EVMWalletConnector extends FuelConnector {
}
const transactionRequest = transactionRequestify(transaction);

const signatureIndex = getSignatureIndex(transactionRequest.witnesses);

// Create a predicate and set the witness index to call in predicate`
const predicate = this.predicateAccount.createPredicate(
evmAccount,
fuelProvider,
[transactionRequest.witnesses.length],
[signatureIndex],
);
predicate.connect(fuelProvider);

Expand Down
21 changes: 21 additions & 0 deletions packages/evm-connector/src/utils/predicate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,24 @@ export const createPredicate = memoize(function createPredicate<

return predicate;
});

/**
* Since the predicate resources were fetched and added to the TransactionRequest before the predicate
* was instantiated, it is very likely that they were fetched and added as normal account resources,
* resulting in a witness placeholder being added to the witnesses of the TransactionRequest to
* later be replaced with an actual signature. Since predicate resources do not require a signature,
* this placeholder witness will be removed when calling `Predicate.populateTransactionPredicateData`.
* However, we need to validate if this placeholder witness was added here in order to instantiate the
* predicate with the correct witness index argument.
*/
export const getSignatureIndex = (witnesses: BytesLike[]) => {
const hasPlaceholderWitness = witnesses.some(
(item) =>
item instanceof Uint8Array &&
item.length === 64 &&
item.every((value) => value === 0),
);

// if it is a placeholder witness, we can safely replace it, otherwise we will consider a new element.
return hasPlaceholderWitness ? witnesses.length - 1 : witnesses.length;
};
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import { ETHEREUM_ICON, TESTNET_URL } from './constants';
import type { Predicate, PredicateConfig, WalletConnectConfig } from './types';
import { PredicateAccount } from './utils/Predicate';
import { createModalConfig } from './utils/wagmiConfig';
import { getSignatureIndex } from './utils/witness';

export class WalletConnectConnector extends FuelConnector {
name = 'Ethereum Wallets';
Expand Down Expand Up @@ -306,7 +307,9 @@ export class WalletConnectConnector extends FuelConnector {
const transactionRequest = transactionRequestify(transaction);
const transactionFee = transactionRequest.maxFee.toNumber();

const predicateSignatureIndex = transactionRequest.witnesses.length - 1;
const predicateSignatureIndex = getSignatureIndex(
transactionRequest.witnesses,
);

// Create a predicate and set the witness index to call in predicate`
const predicate = this.predicateAccount.createPredicate(
Expand Down
22 changes: 22 additions & 0 deletions packages/walletconnect-connector/src/utils/witness.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import type { BytesLike } from 'fuels';

/**
* Since the predicate resources were fetched and added to the TransactionRequest before the predicate
* was instantiated, it is very likely that they were fetched and added as normal account resources,
* resulting in a witness placeholder being added to the witnesses of the TransactionRequest to
* later be replaced with an actual signature. Since predicate resources do not require a signature,
* this placeholder witness will be removed when calling `Predicate.populateTransactionPredicateData`.
* However, we need to validate if this placeholder witness was added here in order to instantiate the
* predicate with the correct witness index argument.
*/
export const getSignatureIndex = (witnesses: BytesLike[]) => {
const hasPlaceholderWitness = witnesses.some(
(item) =>
item instanceof Uint8Array &&
item.length === 64 &&
item.every((value) => value === 0),
);

// if it is a placeholder witness, we can safely replace it, otherwise we will consider a new element.
return hasPlaceholderWitness ? witnesses.length - 1 : witnesses.length;
};

0 comments on commit a87a9a4

Please sign in to comment.