-
Notifications
You must be signed in to change notification settings - Fork 70
💥 introduced register-unregister wallet in identity #138
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
mohamadhammoud
wants to merge
19
commits into
develop
Choose a base branch
from
feature/BT-1447-global-identity-registry-id-factory
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 10 commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
ced8cb5
💥 introduced register-unregister wallet in identity
mohamadhammoud 8203ce4
🚨 fix lint
mohamadhammoud bc8e6bd
Merge branch 'develop' into feature/BT-1447-global-identity-registry-…
mohamadhammoud b11f323
💥 used require statement
mohamadhammoud 25c74c2
💥 test coverage
mohamadhammoud a4271e9
💥 resolve require statement
mohamadhammoud aad1cbc
🚨 fix lint
mohamadhammoud 1a7c009
added nonce per wallet
mohamadhammoud 777b4c9
🚨 fix linter validation
mohamadhammoud c0dc8fa
🔧 Merge branch 'develop' into feature/BT-1447-global-identity-registr…
mohamadhammoud c357fb9
♻️ added eip 712 and resolved unlink method
mohamadhammoud 9875667
Merge branch 'develop' into feature/BT-1447-global-identity-registry-…
mohamadhammoud 1dcc2ad
Merge branch 'develop' into feature/BT-1447-global-identity-registry-…
mohamadhammoud 7445cd6
💥 applied nonce for erc712, linked wallet just to the same ID
mohamadhammoud 25faad4
Merge branch 'develop' into feature/BT-1447-global-identity-registry-…
mohamadhammoud 9434b6f
return format section
mohamadhammoud ba2332f
fix lint
mohamadhammoud b88aff1
🔀 Merge branch 'develop' into feature/BT-1447-global-identity-registr…
mohamadhammoud 6c71d8a
🔀 Merge branch 'develop' into feature/BT-1447-global-identity-registr…
mohamadhammoud File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -23,3 +23,5 @@ artifacts | |
| coverage.json | ||
| cache | ||
| typechain-types | ||
|
|
||
| out | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,15 +2,20 @@ | |
| pragma solidity ^0.8.27; | ||
|
|
||
| import { Ownable } from "@openzeppelin/contracts/access/Ownable.sol"; | ||
| import { ECDSA } from "@openzeppelin/contracts/utils/cryptography/ECDSA.sol"; | ||
| import { MessageHashUtils } from "@openzeppelin/contracts/utils/cryptography/MessageHashUtils.sol"; | ||
|
|
||
| import { IdentityProxy } from "../proxy/IdentityProxy.sol"; | ||
| import { IIdFactory } from "./IIdFactory.sol"; | ||
| import { IERC734 } from "../interface/IERC734.sol"; | ||
| import { IIdentity } from "../interface/IIdentity.sol"; | ||
| import { Errors } from "../libraries/Errors.sol"; | ||
| import { KeyPurposes } from "../libraries/KeyPurposes.sol"; | ||
| import { KeyTypes } from "../libraries/KeyTypes.sol"; | ||
|
|
||
| contract IdFactory is IIdFactory, Ownable { | ||
| uint256 private constant _MAX_WALLETS_PER_IDENTITY = 101; | ||
|
|
||
| // address of the _implementationAuthority contract making the link to the implementation contract | ||
| address public immutable implementationAuthority; | ||
|
|
||
|
|
@@ -32,6 +37,9 @@ contract IdFactory is IIdFactory, Ownable { | |
| // token linked to an ONCHAINID | ||
| mapping(address => address) private _tokenAddress; | ||
|
|
||
| // nonce per wallet for signature replay protection | ||
| mapping(address => uint256) private _walletNonces; | ||
|
|
||
| // setting | ||
| constructor(address implementationAuthorityAddress) Ownable(msg.sender) { | ||
| require( | ||
|
|
@@ -186,7 +194,7 @@ contract IdFactory is IIdFactory, Ownable { | |
| ); | ||
| address identity = _userIdentity[msg.sender]; | ||
| require( | ||
| _wallets[identity].length < 101, | ||
| _wallets[identity].length < _MAX_WALLETS_PER_IDENTITY, | ||
| Errors.MaxWalletsPerIdentityExceeded() | ||
| ); | ||
| _userIdentity[_newWallet] = identity; | ||
|
|
@@ -220,6 +228,74 @@ contract IdFactory is IIdFactory, Ownable { | |
| emit WalletUnlinked(_oldWallet, _identity); | ||
| } | ||
|
|
||
| /** | ||
| * @dev See {IIdFactory-linkWalletWithSignature}. | ||
| */ | ||
| function linkWalletWithSignature( | ||
| address wallet, | ||
| bytes calldata signature, | ||
| uint256 nonce, | ||
| uint256 expiry | ||
| ) external override { | ||
| require(wallet != address(0), Errors.ZeroAddress()); | ||
| require(block.timestamp <= expiry, Errors.ExpiredSignature(signature)); | ||
| require(nonce == _walletNonces[wallet], Errors.InvalidNonce(nonce)); | ||
|
|
||
| address identity = msg.sender; | ||
| _verifyWalletSignature(wallet, identity, nonce, expiry, signature); | ||
|
|
||
| // require the wallet is a MANAGEMENT key on the identity | ||
| bytes32 key = keccak256(abi.encode(wallet)); | ||
| require( | ||
| IIdentity(identity).keyHasPurpose(key, KeyPurposes.MANAGEMENT), | ||
| Errors.KeyDoesNotHavePurpose(key, KeyPurposes.MANAGEMENT) | ||
| ); | ||
|
|
||
| // Check if wallet is already linked | ||
| require( | ||
| _userIdentity[wallet] == address(0), | ||
| Errors.WalletAlreadyLinkedToIdentity(wallet) | ||
| ); | ||
| require( | ||
| _tokenIdentity[wallet] == address(0), | ||
| Errors.TokenAlreadyLinked(wallet) | ||
| ); | ||
|
|
||
| // Check max wallets per identity | ||
| require( | ||
| _wallets[identity].length < _MAX_WALLETS_PER_IDENTITY, | ||
| Errors.MaxWalletsPerIdentityExceeded() | ||
| ); | ||
|
|
||
| _walletNonces[wallet]++; | ||
| _userIdentity[wallet] = identity; | ||
| _wallets[identity].push(wallet); | ||
| emit WalletLinked(wallet, identity); | ||
| } | ||
|
|
||
| /** | ||
| * @dev See {IIdFactory-unlinkWalletWithSignature}. | ||
| */ | ||
| function unlinkWalletWithSignature(address wallet) external override { | ||
pgonday marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| require(wallet != address(0), Errors.ZeroAddress()); | ||
| require( | ||
| _userIdentity[wallet] == msg.sender, | ||
| Errors.WalletNotLinkedToIdentity(wallet) | ||
| ); | ||
|
|
||
| address identity = _userIdentity[wallet]; | ||
| delete _userIdentity[wallet]; | ||
| uint256 length = _wallets[identity].length; | ||
| for (uint256 i = 0; i < length; i++) { | ||
| if (_wallets[identity][i] == wallet) { | ||
| _wallets[identity][i] = _wallets[identity][length - 1]; | ||
| _wallets[identity].pop(); | ||
| break; | ||
| } | ||
| } | ||
| emit WalletUnlinked(wallet, identity); | ||
| } | ||
|
|
||
| /** | ||
| * @dev See {IdFactory-getIdentity}. | ||
| */ | ||
|
|
@@ -260,6 +336,15 @@ contract IdFactory is IIdFactory, Ownable { | |
| return _tokenAddress[_identity]; | ||
| } | ||
|
|
||
| /** | ||
| * @dev See {IIdFactory-walletNonce}. | ||
| */ | ||
| function walletNonce( | ||
|
||
| address wallet | ||
| ) external view override returns (uint256) { | ||
| return _walletNonces[wallet]; | ||
| } | ||
|
|
||
| /** | ||
| * @dev See {IdFactory-isTokenFactory}. | ||
| */ | ||
|
|
@@ -303,4 +388,33 @@ contract IdFactory is IIdFactory, Ownable { | |
| bytes memory bytecode = abi.encodePacked(_code, _constructData); | ||
| return _deploy(_salt, bytecode); | ||
| } | ||
|
|
||
| function _verifyWalletSignature( | ||
| address wallet, | ||
| address identity, | ||
| uint256 nonce, | ||
| uint256 expiry, | ||
| bytes calldata signature | ||
| ) private view { | ||
| bytes32 structHash = keccak256( | ||
| abi.encode( | ||
| wallet, | ||
| identity, | ||
| nonce, | ||
| expiry, | ||
| address(this), | ||
| block.chainid | ||
| ) | ||
| ); | ||
|
|
||
| bytes32 digest = MessageHashUtils.toEthSignedMessageHash(structHash); | ||
pgonday marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| (address signer, ECDSA.RecoverError error, ) = ECDSA.tryRecover( | ||
| digest, | ||
| signature | ||
| ); | ||
| require( | ||
| error == ECDSA.RecoverError.NoError && signer == wallet, | ||
| Errors.InvalidSignature() | ||
| ); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Inherits OpenZeppelin Nonces instead of custom implementation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
c357fb9