Skip to content
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

Myso token manager sig #610

Merged
merged 2 commits into from
Apr 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
16 changes: 12 additions & 4 deletions contracts/tokenManager/MysoTokenManager.sol
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ contract MysoTokenManager is Ownable2Step, IMysoTokenManager {
address public stMysoToken;
address public accessSigner;
mapping(address => RewardInfo) public rewardInfos;

mapping(bytes32 => bool) public alreadyClaimed;
event RewardInfoSet(
address indexed collToken,
uint128 collThreshold,
Expand Down Expand Up @@ -247,13 +247,21 @@ contract MysoTokenManager is Ownable2Step, IMysoTokenManager {
if (_signer == address(0)) {
return true;
} else {
bytes32 payloadHash = keccak256(abi.encode(loan.borrower));
(bytes32 r, bytes32 vs) = Helpers.splitSignature(
borrowInstructions.mysoTokenManagerData
(bytes memory compactSig, uint256 nonce) = abi.decode(
borrowInstructions.mysoTokenManagerData,
(bytes, uint256)
);
bytes32 payloadHash = keccak256(
abi.encode(loan.borrower, nonce)
);
if (alreadyClaimed[payloadHash]) {
return false;
}
(bytes32 r, bytes32 vs) = Helpers.splitSignature(compactSig);
bytes32 messageHash = ECDSA.toEthSignedMessageHash(payloadHash);
address recoveredSigner = messageHash.recover(r, vs);
if (recoveredSigner == _signer) {
alreadyClaimed[payloadHash] = true;
return true;
}
return false;
Expand Down
18 changes: 14 additions & 4 deletions test/peer-to-peer/local-tests-tokenmanager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -267,14 +267,19 @@ describe('Peer-to-Peer: Local Tests', function () {
} = await setupTest()

// create signature for borrower
const nonce = 0
const payload = ethers.utils.defaultAbiCoder.encode(
['address'],
[borrower1.address]
['address', 'uint256'],
[borrower1.address, nonce]
)
const payloadHash = ethers.utils.keccak256(payload)
const signature = await deployer.signMessage(ethers.utils.arrayify(payloadHash))
const sig = ethers.utils.splitSignature(signature)
const compactSig = sig.compact
let mysoTokenManagerData = ethers.utils.defaultAbiCoder.encode(
['bytes', 'uint256'],
[compactSig, nonce]
)

// borrowers approves gateway
await usdc.connect(borrower1).approve(borrowerGateway.address, MAX_UINT256)
Expand All @@ -301,17 +306,22 @@ describe('Peer-to-Peer: Local Tests', function () {
// check revert if no sig
await expect(borrowerGateway
.connect(borrower1)
.borrowWithOnChainQuote(iooVault.address, borrowInstructions, iooOnChainQuote, quoteTupleIdx)).to.be.revertedWith("invalid signature length")
.borrowWithOnChainQuote(iooVault.address, borrowInstructions, iooOnChainQuote, quoteTupleIdx)).to.be.reverted

// check pass if valid sig
borrowInstructions.mysoTokenManagerData = compactSig
borrowInstructions.mysoTokenManagerData = mysoTokenManagerData
await borrowerGateway
.connect(borrower1)
.borrowWithOnChainQuote(iooVault.address, borrowInstructions, iooOnChainQuote, quoteTupleIdx)
let totalMysoLoanAmount1 = await mysoTokenManager.totalMysoLoanAmount()
let borrower1MytBal = await myt.balanceOf(borrower1.address)
expect(borrower1MytBal).to.be.equal(totalMysoLoanAmount1)

// check revert that user can only borrow once with sig
await expect(borrowerGateway
.connect(borrower1)
.borrowWithOnChainQuote(iooVault.address, borrowInstructions, iooOnChainQuote, quoteTupleIdx)).to.be.revertedWithCustomError(mysoTokenManager, "NotAllowed")

// check revert if unauthorized borrower
await expect(borrowerGateway
.connect(borrower2)
Expand Down