Skip to content

Commit

Permalink
fix: ledger signature verification fallback
Browse files Browse the repository at this point in the history
  • Loading branch information
amarinkovic committed May 12, 2023
1 parent eec4700 commit f8b7766
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 10 deletions.
51 changes: 44 additions & 7 deletions src/diamonds/nayms/libs/LibEntity.sol
Original file line number Diff line number Diff line change
Expand Up @@ -143,13 +143,18 @@ library LibEntity {
}

if (LibObject._getParentFromAddress(signer) != _stakeholders.entityIds[i]) {
revert SimplePolicyStakeholderSignatureInvalid(
signingHash,
_stakeholders.signatures[i],
LibHelpers._getIdForAddress(signer),
LibObject._getParentFromAddress(signer),
_stakeholders.entityIds[i]
);
// default implementation didn't match the signer
signer = getSignerLedgerFallback(signingHash, _stakeholders.signatures[i]); // fallback ledger implementation

if (LibObject._getParentFromAddress(signer) != _stakeholders.entityIds[i]) {
revert SimplePolicyStakeholderSignatureInvalid(
signingHash,
_stakeholders.signatures[i],
LibHelpers._getIdForAddress(signer),
LibObject._getParentFromAddress(signer),
_stakeholders.entityIds[i]
);
}
}
LibACL._assignRole(_stakeholders.entityIds[i], _policyId, _stakeholders.roles[i]);
}
Expand All @@ -158,6 +163,38 @@ library LibEntity {
emit SimplePolicyCreated(_policyId, _entityId);
}

function getSignerLedgerFallback(bytes32 signingHash, bytes memory signature) private returns (address) {
bytes32 r;
bytes32 s;
uint8 v;

// ecrecover takes the signature parameters, and the only way to get them
if (signature.length == 65) {
// currently is to use assembly.
/// @solidity memory-safe-assembly
assembly {
r := mload(add(signature, 0x20))
s := mload(add(signature, 0x40))
v := byte(0, mload(add(signature, 0x60)))
}
v = _adjustV(v);
}

(address signer, ) = ECDSA.tryRecover(ECDSA.toEthSignedMessageHash(signingHash), v, r, s);

return signer;
}

function _adjustV(uint8 v) private returns (uint8) {
if (v == 0) {
return 27;
} else if (v == 1) {
return 28;
} else {
return v;
}
}

/// @param _amount the amount of entity token that is minted and put on sale
/// @param _totalPrice the buy amount
function _startTokenSale(
Expand Down
5 changes: 2 additions & 3 deletions test/T05TokenWrapper.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -100,19 +100,18 @@ contract T05TokenWrapper is D03ProtocolDefaults {
vm.startPrank(signer1);
wrapper.increaseAllowance(account0, type(uint256).max);
assertEq(wrapper.allowance(signer1, account0), type(uint256).max, "allowance should have increased");

vm.expectRevert("ERC20: allowance overflow");
wrapper.increaseAllowance(account0, 1);
vm.stopPrank();

vm.startPrank(signer1);
wrapper.decreaseAllowance(account0, type(uint256).max);
assertEq(wrapper.allowance(signer1, account0), 0, "allowance should have decreased");

vm.expectRevert("ERC20: decreased allowance below zero");
wrapper.decreaseAllowance(account0, 1);
vm.stopPrank();

}

function testPermit() public {
Expand Down

0 comments on commit f8b7766

Please sign in to comment.