Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
111 commits
Select commit Hold shift + click to select a range
dd378a1
created interfaces for Account recovery feature
Oct 16, 2023
37e2c9d
guardian interface and contract
Oct 17, 2023
1b09442
added test suite for Guardian.sol
Oct 20, 2023
3f4e74c
Merge branch 'thirdweb-dev:main' into account-social-recovery
alfheimrShiven Oct 20, 2023
a3cc320
AccountGuardian contract
Oct 20, 2023
f22fb72
added test suite for AccountGuardian contract
Oct 21, 2023
d3e6fe0
dependency injection for social recovery feature
Oct 21, 2023
3dba350
account lock req creation, signature validation and acceptance check
Oct 24, 2023
2ee0e32
Chainlink automation for lock request evaluation and lock account fea…
Oct 26, 2023
c642267
refactored deploy script for smart account util contracts
Oct 27, 2023
c476031
Deploy script for util contracts and its test
Oct 28, 2023
d7bd5d6
added link to feature doc in Readme
Oct 28, 2023
5bd148f
Merge pull request #1 from alfheimrShiven/account-social-recovery
alfheimrShiven Oct 30, 2023
14dfa8f
fixing..
Oct 30, 2023
8395b06
brought back DeployGuardian as DeploySmartAccountUtilContracts script…
Oct 30, 2023
b106090
Merge pull request #2 from alfheimrShiven/account-social-recovery
alfheimrShiven Oct 30, 2023
8001b8b
moved account util contract deployments to Account.sol, commented out…
Nov 1, 2023
c72f748
switched AccountLock to single instance deployment (as per the system…
Nov 2, 2023
a8ca25f
Merge pull request #3 from alfheimrShiven/account-social-recovery
alfheimrShiven Nov 2, 2023
19f18e3
restoring to util contract deployments to createAccount()
Nov 4, 2023
0ddb444
Changed the approach of calling Account.sol from AccountLock.sol
Nov 8, 2023
9c15308
added modifier to restrict an Account's lock status change by Account…
Nov 8, 2023
493001e
Merge pull request #4 from alfheimrShiven/account-social-recovery
alfheimrShiven Nov 8, 2023
f6b4843
made AccountGuardianTest compatible with deployment of AccountGuardia…
Nov 8, 2023
53b9588
replaced DeployGuardian script with DeploySmartAccountUtilContracts s…
Nov 10, 2023
38e568d
Merge pull request #5 from alfheimrShiven/account-social-recovery
alfheimrShiven Nov 10, 2023
3f763ed
sending AccountLock address while initilizing Account clones for acco…
Nov 14, 2023
665f243
Merge pull request #6 from alfheimrShiven/account-social-recovery
alfheimrShiven Nov 14, 2023
bb6c8ba
AccountLock tests: createLockRequest() unit tests
Nov 14, 2023
dc9a859
[Buggy] ECDSA.recover(hash, signature) throwing error
Nov 16, 2023
5a7d6aa
Merge pull request #7 from alfheimrShiven/account-social-recovery
alfheimrShiven Nov 16, 2023
a6f7e33
foundry error
codypharm Nov 17, 2023
82c9281
depencies added but errors persist
codypharm Nov 17, 2023
598de03
error comntinues
codypharm Nov 17, 2023
3be95eb
build error persists
codypharm Nov 17, 2023
896717d
updated chainlink remappings
Nov 17, 2023
b15ce17
ECDSA.recover(...) bug fixed, made lockRequest hash EIP 191 compatibl…
Nov 17, 2023
fe87435
Merge pull request #8 from alfheimrShiven/account-social-recovery
alfheimrShiven Nov 17, 2023
819a0a7
unlock request feature, refactored concensys evaluation to both lock/…
Nov 18, 2023
a764396
Merge pull request #9 from alfheimrShiven/account-social-recovery
alfheimrShiven Nov 18, 2023
4ae52bb
added unit tests for account unlock feature
Nov 18, 2023
a50e537
Merge pull request #10 from alfheimrShiven/account-social-recovery
alfheimrShiven Nov 19, 2023
0aee37b
Makefile
Nov 21, 2023
ad2a9b1
Merge pull request #11 from alfheimrShiven/account-social-recovery
alfheimrShiven Nov 22, 2023
cb6691e
cross chain transaction now includes estimated amount deposit
codypharm Nov 28, 2023
11c2173
slippage calvualtion added
codypharm Nov 29, 2023
1687b2f
bugs fixed
codypharm Nov 30, 2023
0bc9b51
stopped at this stage
codypharm Dec 4, 2023
20e1477
added IAccountRecovery
Dec 5, 2023
ecab36c
native payment intiation done
codypharm Dec 5, 2023
c3c5a31
link transfer initiator done
codypharm Dec 5, 2023
e822483
Account Recovery interface
Dec 5, 2023
42322c5
stock with userOp
codypharm Dec 5, 2023
0f95847
external signing implemented
codypharm Dec 5, 2023
37c3a36
recovery request guardian signature collection & concensus evaluation
Dec 7, 2023
78bb350
used isValidSignature but review is needed
codypharm Dec 7, 2023
7ceeb42
comments added and code made more dynamic
codypharm Dec 7, 2023
0bf4596
private key restoration
Dec 8, 2023
fc7af00
now using just one estimate function
codypharm Dec 9, 2023
4d4a248
proceed now has _
codypharm Dec 9, 2023
5c6ada8
Merge pull request #13 from alfheimrShiven/account-social-recovery
alfheimrShiven Dec 9, 2023
aaee738
Merge branch 'main' into cross-chain-transactions
codypharm Dec 9, 2023
b81a628
cross chain contracts deployed with accessible address
codypharm Dec 10, 2023
921b5c8
merge added
codypharm Dec 10, 2023
706d43e
getter added for account lock
codypharm Dec 10, 2023
c48ef73
Merge pull request #12 from alfheimrShiven/cross-chain-transactions
codypharm Dec 10, 2023
b818a8b
cross chain script sample added
codypharm Dec 10, 2023
faed719
Merge pull request #14 from alfheimrShiven/cross-chain-script
codypharm Dec 10, 2023
67ab98b
interface changes
Dec 10, 2023
e838c6d
Updated contract deploy script, fixed backward compatibility issues d…
Dec 10, 2023
318e840
Code optimized
codypharm Dec 10, 2023
7ec5c55
restructured functions
codypharm Dec 10, 2023
c036372
link address also added to constructot
codypharm Dec 10, 2023
3ad8a0b
master deployment updated
codypharm Dec 11, 2023
648be6c
one step skipped
codypharm Dec 11, 2023
e426334
last trial
codypharm Dec 11, 2023
21c1db4
Merge pull request #16 from alfheimrShiven/Fix-compilation
alfheimrShiven Dec 11, 2023
13454b1
Merge pull request #15 from alfheimrShiven/account-social-recovery
alfheimrShiven Dec 11, 2023
9994ac3
Update README.md
alfheimrShiven Dec 11, 2023
ddf5634
building cancelling
codypharm Dec 16, 2023
4e07d7c
resolved conflicts with main
Dec 18, 2023
4d11bd3
added AccountRecovery to deploy script
Dec 19, 2023
d968a8b
commented out both CCIP contracts [build still failing]
Dec 19, 2023
b17bd89
[FIX]: Fixed the stack too deep error by removing the _router, _link …
Dec 19, 2023
4b0133d
updating owner of smart account
Dec 22, 2023
89d12a4
Email verification to generate recovery request
Dec 24, 2023
669d16f
uncommented the CCIP contracts
Dec 24, 2023
15718b2
moved the deployment of CCIP contracts from BaseAccountFactory to non…
Dec 24, 2023
6a28d89
Merge pull request #17 from alfheimrShiven/revert/debugging
alfheimrShiven Dec 24, 2023
6413321
merged with main
Dec 24, 2023
14cc533
Merge pull request #18 from alfheimrShiven/account-social-recovery
alfheimrShiven Dec 24, 2023
a91ba30
updated account util deploy script and test
Dec 25, 2023
e12640a
Moved smart account creation to deploy script, updated deployment scr…
Dec 27, 2023
ab94b18
Merge pull request #19 from alfheimrShiven/account-social-recovery
alfheimrShiven Dec 27, 2023
17ed4de
generateRecoveryReq(..) tests
Dec 31, 2023
b9b472e
collectGuardianSignaturesOnRecoveryRequest(..) unit tests
Dec 31, 2023
b5490d4
refactored AccountRecovery unit tests
Dec 31, 2023
6abbb81
Consensus evaluation tests
Dec 31, 2023
f694b4f
Merge pull request #20 from alfheimrShiven/account-social-recovery
alfheimrShiven Dec 31, 2023
abbeeff
fixed Account::updateAdmin() function signature and call from Account…
Jan 3, 2024
6454a6c
Merge pull request #21 from alfheimrShiven/account-social-recovery
alfheimrShiven Jan 3, 2024
625340b
removed _generateSalt dependency on account admin, as it can get upda…
Jan 4, 2024
b0d399a
finished AccountRecovery unit tests. Coverage: 92%
Jan 4, 2024
af976db
Merge pull request #22 from alfheimrShiven/account-social-recovery
alfheimrShiven Jan 4, 2024
faf0a62
reduced params for commitEmailVerificationHash()
Jan 5, 2024
41099ca
Merge pull request #23 from alfheimrShiven/account-social-recovery
alfheimrShiven Jan 5, 2024
047eac0
updated Readme: Problem statement, Architecture
Jan 6, 2024
548776e
Merge pull request #24 from alfheimrShiven/account-social-recovery
alfheimrShiven Jan 6, 2024
d4162c5
updated Readme: Benefits of smart wallets
Jan 8, 2024
d54fdb5
Readme touchups
Jan 8, 2024
8b9536a
Merge pull request #25 from alfheimrShiven/account-social-recovery
alfheimrShiven Jan 8, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
foundry error
  • Loading branch information
codypharm committed Nov 17, 2023
commit a6f7e33ceebe1dabc9b3db45691212552e14b0cd
267 changes: 267 additions & 0 deletions contracts/prebuilts/account/utils/CrossChainTokenTransfer.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,267 @@
// SPDX-License-Identifier: MIT
pragma solidity 0.8.19;

import {IRouterClient} from "@chainlink/contracts-ccip/src/v0.8/ccip/interfaces/IRouterClient.sol";
import {OwnerIsCreator} from "@chainlink/contracts-ccip/src/v0.8/shared/access/OwnerIsCreator.sol";
import {Client} from "@chainlink/contracts-ccip/src/v0.8/ccip/libraries/Client.sol";
import {IERC20} from "@chainlink/contracts-ccip/src/v0.8/vendor/openzeppelin-solidity/v4.8.0/token/ERC20/IERC20.sol";



/// @title - A simple contract for transferring tokens across chains.
contract CrossChainTokenTransfer is OwnerIsCreator {
// Custom errors to provide more descriptive revert messages.
error NotEnoughBalance(uint256 currentBalance, uint256 calculatedFees); // Used to make sure contract has enough balance to cover the fees.
error NothingToWithdraw(); // Used when trying to withdraw Ether but there's nothing to withdraw.
error FailedToWithdrawEth(address owner, address target, uint256 value); // Used when the withdrawal of Ether fails.
error DestinationChainNotAllowlisted(uint64 destinationChainSelector); // Used when the destination chain has not been allowlisted by the contract owner.
// Event emitted when the tokens are transferred to an account on another chain.
event TokensTransferred(
bytes32 indexed messageId, // The unique ID of the message.
uint64 indexed destinationChainSelector, // The chain selector of the destination chain.
address receiver, // The address of the receiver on the destination chain.
address token, // The token address that was transferred.
uint256 tokenAmount, // The token amount that was transferred.
address feeToken, // the token address used to pay CCIP fees.
uint256 fees // The fees paid for sending the message.
);

// Mapping to keep track of allowlisted destination chains.
mapping(uint64 => bool) public allowlistedChains;

IRouterClient private s_router;

IERC20 private s_linkToken;

/// @notice Constructor initializes the contract with the router address.
/// @param _router The address of the router contract.
/// @param _link The address of the link contract.
constructor(address _router, address _link) {
s_router = IRouterClient(_router);
s_linkToken = IERC20(_link);
}

/// @dev Modifier that checks if the chain with the given destinationChainSelector is allowlisted.
/// @param _destinationChainSelector The selector of the destination chain.
modifier onlyAllowlistedChain(uint64 _destinationChainSelector) {
if (!allowlistedChains[_destinationChainSelector])
revert DestinationChainNotAllowlisted(_destinationChainSelector);
_;
}

/// @dev Updates the allowlist status of a destination chain for transactions.
/// @notice This function can only be called by the owner.
/// @param _destinationChainSelector The selector of the destination chain to be updated.
/// @param allowed The allowlist status to be set for the destination chain.
function allowlistDestinationChain(
uint64 _destinationChainSelector,
bool allowed
) external onlyOwner {
allowlistedChains[_destinationChainSelector] = allowed;
}

/// @notice Transfer tokens to receiver on the destination chain.
/// @notice pay in LINK.
/// @notice the token must be in the list of supported tokens.
/// @notice This function can only be called by the owner.
/// @dev Assumes your contract has sufficient LINK tokens to pay for the fees.
/// @param _destinationChainSelector The identifier (aka selector) for the destination blockchain.
/// @param _receiver The address of the recipient on the destination blockchain.
/// @param _token token address.
/// @param _amount token amount.
/// @return messageId The ID of the message that was sent.
function transferTokensPayLINK(
uint64 _destinationChainSelector,
address _receiver,
address _token,
uint256 _amount
)
external
onlyOwner
onlyAllowlistedChain(_destinationChainSelector)
returns (bytes32 messageId)
{
// Create an EVM2AnyMessage struct in memory with necessary information for sending a cross-chain message
// address(linkToken) means fees are paid in LINK
Client.EVM2AnyMessage memory evm2AnyMessage = _buildCCIPMessage(
_receiver,
_token,
_amount,
address(s_linkToken)
);

// Get the fee required to send the message
uint256 fees = s_router.getFee(
_destinationChainSelector,
evm2AnyMessage
);

if (fees > s_linkToken.balanceOf(address(this)))
revert NotEnoughBalance(s_linkToken.balanceOf(address(this)), fees);

// approve the Router to transfer LINK tokens on contract's behalf. It will spend the fees in LINK
s_linkToken.approve(address(s_router), fees);

// approve the Router to spend tokens on contract's behalf. It will spend the amount of the given token
IERC20(_token).approve(address(s_router), _amount);

// Send the message through the router and store the returned message ID
messageId = s_router.ccipSend(
_destinationChainSelector,
evm2AnyMessage
);

// Emit an event with message details
emit TokensTransferred(
messageId,
_destinationChainSelector,
_receiver,
_token,
_amount,
address(s_linkToken),
fees
);

// Return the message ID
return messageId;
}

/// @notice Transfer tokens to receiver on the destination chain.
/// @notice Pay in native gas such as ETH on Ethereum or MATIC on Polgon.
/// @notice the token must be in the list of supported tokens.
/// @notice This function can only be called by the owner.
/// @dev Assumes your contract has sufficient native gas like ETH on Ethereum or MATIC on Polygon.
/// @param _destinationChainSelector The identifier (aka selector) for the destination blockchain.
/// @param _receiver The address of the recipient on the destination blockchain.
/// @param _token token address.
/// @param _amount token amount.
/// @return messageId The ID of the message that was sent.
function transferTokensPayNative(
uint64 _destinationChainSelector,
address _receiver,
address _token,
uint256 _amount
)
external
onlyOwner
onlyAllowlistedChain(_destinationChainSelector)
returns (bytes32 messageId)
{
// Create an EVM2AnyMessage struct in memory with necessary information for sending a cross-chain message
// address(0) means fees are paid in native gas
Client.EVM2AnyMessage memory evm2AnyMessage = _buildCCIPMessage(
_receiver,
_token,
_amount,
address(0)
);

// Get the fee required to send the message
uint256 fees = s_router.getFee(
_destinationChainSelector,
evm2AnyMessage
);

if (fees > address(this).balance)
revert NotEnoughBalance(address(this).balance, fees);

// approve the Router to spend tokens on contract's behalf. It will spend the amount of the given token
IERC20(_token).approve(address(s_router), _amount);

// Send the message through the router and store the returned message ID
messageId = s_router.ccipSend{value: fees}(
_destinationChainSelector,
evm2AnyMessage
);

// Emit an event with message details
emit TokensTransferred(
messageId,
_destinationChainSelector,
_receiver,
_token,
_amount,
address(0),
fees
);

// Return the message ID
return messageId;
}

/// @notice Construct a CCIP message.
/// @dev This function will create an EVM2AnyMessage struct with all the necessary information for tokens transfer.
/// @param _receiver The address of the receiver.
/// @param _token The token to be transferred.
/// @param _amount The amount of the token to be transferred.
/// @param _feeTokenAddress The address of the token used for fees. Set address(0) for native gas.
/// @return Client.EVM2AnyMessage Returns an EVM2AnyMessage struct which contains information for sending a CCIP message.
function _buildCCIPMessage(
address _receiver,
address _token,
uint256 _amount,
address _feeTokenAddress
) internal pure returns (Client.EVM2AnyMessage memory) {
// Set the token amounts
Client.EVMTokenAmount[]
memory tokenAmounts = new Client.EVMTokenAmount[](1);
tokenAmounts[0] = Client.EVMTokenAmount({
token: _token,
amount: _amount
});

// Create an EVM2AnyMessage struct in memory with necessary information for sending a cross-chain message
return
Client.EVM2AnyMessage({
receiver: abi.encode(_receiver), // ABI-encoded receiver address
data: "", // No data
tokenAmounts: tokenAmounts, // The amount and type of token being transferred
extraArgs: Client._argsToBytes(
// Additional arguments, setting gas limit to 0 as we are not sending any data and non-strict sequencing mode
Client.EVMExtraArgsV1({gasLimit: 0, strict: false})
),
// Set the feeToken to a feeTokenAddress, indicating specific asset will be used for fees
feeToken: _feeTokenAddress
});
}

/// @notice Fallback function to allow the contract to receive Ether.
/// @dev This function has no function body, making it a default function for receiving Ether.
/// It is automatically called when Ether is transferred to the contract without any data.
receive() external payable {}

/// @notice Allows the contract owner to withdraw the entire balance of Ether from the contract.
/// @dev This function reverts if there are no funds to withdraw or if the transfer fails.
/// It should only be callable by the owner of the contract.
/// @param _beneficiary The address to which the Ether should be transferred.
function withdraw(address _beneficiary) public onlyOwner {
// Retrieve the balance of this contract
uint256 amount = address(this).balance;

// Revert if there is nothing to withdraw
if (amount == 0) revert NothingToWithdraw();

// Attempt to send the funds, capturing the success status and discarding any return data
(bool sent, ) = _beneficiary.call{value: amount}("");

// Revert if the send failed, with information about the attempted transfer
if (!sent) revert FailedToWithdrawEth(msg.sender, _beneficiary, amount);
}

/// @notice Allows the owner of the contract to withdraw all tokens of a specific ERC20 token.
/// @dev This function reverts with a 'NothingToWithdraw' error if there are no tokens to withdraw.
/// @param _beneficiary The address to which the tokens will be sent.
/// @param _token The contract address of the ERC20 token to be withdrawn.
function withdrawToken(
address _beneficiary,
address _token
) public onlyOwner {
// Retrieve the balance of this contract
uint256 amount = IERC20(_token).balanceOf(address(this));

// Revert if there is nothing to withdraw
if (amount == 0) revert NothingToWithdraw();

IERC20(_token).transfer(_beneficiary, amount);
}
}
6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"/contracts/**/*.sol"
],
"devDependencies": {
"@chainlink/contracts": "^0.6.1",
"@chainlink/contracts": "^0.8.0",
"@openzeppelin/contracts": "4.7.3",
"@openzeppelin/contracts-upgradeable": "4.7.3",
"@thirdweb-dev/chains": "^0.1.54",
Expand Down Expand Up @@ -61,5 +61,7 @@
"gas": "forge test --mc Benchmark --gas-report > gasreport.txt",
"forge:snapshot": "forge snapshot --check"
},
"dependencies": {}
"dependencies": {
"@chainlink/contracts-ccip": "^0.7.6"
}
}
23 changes: 22 additions & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,16 @@
chalk "^2.0.0"
js-tokens "^4.0.0"

"@chainlink/contracts-ccip@^0.7.6":
version "0.7.6"
resolved "https://registry.yarnpkg.com/@chainlink/contracts-ccip/-/contracts-ccip-0.7.6.tgz#5bf4568a0bbf4e29d2e8c32348e5ecc6ced006d2"
integrity sha512-yNbCBFpLs3R+ALymto9dQYKz3vatnjqYGu1pnMD0i2fHEMthiXe0+otaNCGNht6n8k7ruNaA0DNpz3F+2jHQXw==
dependencies:
"@eth-optimism/contracts" "^0.5.21"
"@openzeppelin/contracts" "~4.3.3"
"@openzeppelin/contracts-upgradeable-4.7.3" "npm:@openzeppelin/[email protected]"
"@openzeppelin/contracts-v0.7" "npm:@openzeppelin/[email protected]"

"@chainlink/contracts@^0.6.1":
version "0.6.1"
resolved "https://registry.yarnpkg.com/@chainlink/contracts/-/contracts-0.6.1.tgz#8842b57e755793cbdbcbc45277fb5d179c993e19"
Expand All @@ -33,6 +43,16 @@
"@openzeppelin/contracts-upgradeable" "^4.7.3"
"@openzeppelin/contracts-v0.7" "npm:@openzeppelin/[email protected]"

"@chainlink/contracts@^0.8.0":
version "0.8.0"
resolved "https://registry.yarnpkg.com/@chainlink/contracts/-/contracts-0.8.0.tgz#4050c83c8b1603ffb0fd6ab99f1d9ea9db2c37de"
integrity sha512-nUv1Uxw5Mn92wgLs2bgPYmo8hpdQ3s9jB/lcbdU0LmNOVu0hbfmouVnqwRLa28Ll50q6GczUA+eO0ikNIKLZsA==
dependencies:
"@eth-optimism/contracts" "^0.5.21"
"@openzeppelin/contracts" "~4.3.3"
"@openzeppelin/contracts-upgradeable-4.7.3" "npm:@openzeppelin/[email protected]"
"@openzeppelin/contracts-v0.7" "npm:@openzeppelin/[email protected]"

"@cspotcode/[email protected]":
version "0.8.0"
resolved "https://registry.npmjs.org/@cspotcode/source-map-consumer/-/source-map-consumer-0.8.0.tgz"
Expand Down Expand Up @@ -473,7 +493,8 @@
"@nodelib/fs.scandir" "2.1.5"
fastq "^1.6.0"

"@openzeppelin/[email protected]":
"@openzeppelin/contracts-upgradeable-4.7.3@npm:@openzeppelin/[email protected]", "@openzeppelin/[email protected]":
name "@openzeppelin/contracts-upgradeable-4.7.3"
version "4.7.3"
resolved "https://registry.yarnpkg.com/@openzeppelin/contracts-upgradeable/-/contracts-upgradeable-4.7.3.tgz#f1d606e2827d409053f3e908ba4eb8adb1dd6995"
integrity sha512-+wuegAMaLcZnLCJIvrVUDzA9z/Wp93f0Dla/4jJvIhijRrPabjQbZe6fWiECLaJyfn5ci9fqf9vTw3xpQOad2A==
Expand Down