// SPDX-License-Identifier: Apache-2.0 pragma solidity 0.8.18; import "../../../../utils/SignatureValidator.sol"; import "../../../../utils/LibBytesPointer.sol"; import "../../../../utils/LibBytes.sol"; import "../../../../utils/LibOptim.sol"; /** * @title SequenceBaseSig Library * @author Agustin Aguilar (aa@horizon.io) * @notice A Solidity implementation for handling signatures in the Sequence protocol. */ library SequenceBaseSig { using LibBytesPointer for bytes; uint256 internal constant FLAG_SIGNATURE = 0; uint256 internal constant FLAG_ADDRESS = 1; uint256 internal constant FLAG_DYNAMIC_SIGNATURE = 2; uint256 internal constant FLAG_NODE = 3; uint256 internal constant FLAG_BRANCH = 4; uint256 internal constant FLAG_SUBDIGEST = 5; uint256 internal constant FLAG_NESTED = 6; error InvalidNestedSignature(bytes32 _hash, address _addr, bytes _signature); error InvalidSignatureFlag(uint256 _flag); /** * @notice Generates a subdigest for the input digest (unique for this wallet and network). * @param _digest The input digest to generate the subdigest from. * @return bytes32 The subdigest generated from the input digest. */ function subdigest( bytes32 _digest ) internal view returns (bytes32) { return keccak256( abi.encodePacked( "\x19\x01", block.chainid, address(this), _digest ) ); } /** * @notice Generates the leaf for an address and weight. * @dev The leaf is generated by concatenating the address and weight. * * @param _addr The address to generate the leaf for. * @param _weight The weight to generate the leaf for. * @return bytes32 The leaf generated from the address and weight. */ function _leafForAddressAndWeight( address _addr, uint96 _weight ) internal pure returns (bytes32) { unchecked { return bytes32(uint256(_weight) << 160 | uint256(uint160(_addr))); } } /** * @notice Generates the leaf for a hardcoded subdigest. * @dev The leaf is generated by hashing 'Sequence static digest:\n' and the subdigest. * @param _subdigest The subdigest to generate the leaf for. * @return bytes32 The leaf generated from the hardcoded subdigest. */ function _leafForHardcodedSubdigest( bytes32 _subdigest ) internal pure returns (bytes32) { return keccak256(abi.encodePacked('Sequence static digest:\n', _subdigest)); } /** * @notice Generates the leaf for a nested tree node. * @dev The leaf is generated by hashing 'Sequence nested config:\n', the node, the threshold and the weight. * * @param _node The root of the node to generate the leaf for. * @param _threshold The internal threshold of the tree. * @param _weight The external weight of the tree. * @return bytes32 The leaf generated from the nested tree. */ function _leafForNested( bytes32 _node, uint256 _threshold, uint256 _weight ) internal pure returns (bytes32) { return keccak256(abi.encodePacked('Sequence nested config:\n', _node, _threshold, _weight)); } /** * @notice Returns the weight and root of a signature branch. * @dev If the signature contains a hardcoded subdigest, and it matches the input digest, then the weight is set to 2 ** 256 - 1. * * @param _subdigest The digest to verify the signature against. * @param _signature The signature branch to recover. * @return weight The total weight of the recovered signatures. * @return root The root hash of the recovered configuration. */ function recoverBranch( bytes32 _subdigest, bytes calldata _signature ) internal view returns ( uint256 weight, bytes32 root ) { unchecked { uint256 rindex; // Iterate until the image is completed while (rindex < _signature.length) { // Read next item type uint256 flag; (flag, rindex) = _signature.readUint8(rindex); if (flag == FLAG_ADDRESS) { // Read plain address uint8 addrWeight; address addr; (addrWeight, addr, rindex) = _signature.readUint8Address(rindex); // Write weight and address to image bytes32 node = _leafForAddressAndWeight(addr, addrWeight); root = root != bytes32(0) ? LibOptim.fkeccak256(root, node) : node; continue; } if (flag == FLAG_SIGNATURE) { // Read weight uint8 addrWeight; (addrWeight, rindex) = _signature.readUint8(rindex); // Read single signature and recover signer uint256 nrindex = rindex + 66; address addr = SignatureValidator.recoverSigner(_subdigest, _signature[rindex:nrindex]); rindex = nrindex; // Acumulate total weight of the signature weight += addrWeight; // Write weight and address to image bytes32 node = _leafForAddressAndWeight(addr, addrWeight); root = root != bytes32(0) ? LibOptim.fkeccak256(root, node) : node; continue; } if (flag == FLAG_DYNAMIC_SIGNATURE) { // Read signer and weight uint8 addrWeight; address addr; (addrWeight, addr, rindex) = _signature.readUint8Address(rindex); // Read signature size uint256 size; (size, rindex) = _signature.readUint24(rindex); // Read dynamic size signature uint256 nrindex = rindex + size; if (!SignatureValidator.isValidSignature(_subdigest, addr, _signature[rindex:nrindex])) { revert InvalidNestedSignature(_subdigest, addr, _signature[rindex:nrindex]); } rindex = nrindex; // Acumulate total weight of the signature weight += addrWeight; // Write weight and address to image bytes32 node = _leafForAddressAndWeight(addr, addrWeight); root = root != bytes32(0) ? LibOptim.fkeccak256(root, node) : node; continue; } if (flag == FLAG_NODE) { // Read node hash bytes32 node; (node, rindex) = _signature.readBytes32(rindex); root = root != bytes32(0) ? LibOptim.fkeccak256(root, node) : node; continue; } if (flag == FLAG_BRANCH) { // Enter a branch of the signature merkle tree uint256 size; (size, rindex) = _signature.readUint24(rindex); uint256 nrindex = rindex + size; uint256 nweight; bytes32 node; (nweight, node) = recoverBranch(_subdigest, _signature[rindex:nrindex]); weight += nweight; root = LibOptim.fkeccak256(root, node); rindex = nrindex; continue; } if (flag == FLAG_NESTED) { // Enter a branch of the signature merkle tree // but with an internal threshold and an external fixed weight uint256 externalWeight; (externalWeight, rindex) = _signature.readUint8(rindex); uint256 internalThreshold; (internalThreshold, rindex) = _signature.readUint16(rindex); uint256 size; (size, rindex) = _signature.readUint24(rindex); uint256 nrindex = rindex + size; uint256 internalWeight; bytes32 internalRoot; (internalWeight, internalRoot) = recoverBranch(_subdigest, _signature[rindex:nrindex]); rindex = nrindex; if (internalWeight >= internalThreshold) { weight += externalWeight; } bytes32 node = _leafForNested(internalRoot, internalThreshold, externalWeight); root = root != bytes32(0) ? LibOptim.fkeccak256(root, node) : node; continue; } if (flag == FLAG_SUBDIGEST) { // A hardcoded always accepted digest // it pushes the weight to the maximum bytes32 hardcoded; (hardcoded, rindex) = _signature.readBytes32(rindex); if (hardcoded == _subdigest) { weight = type(uint256).max; } bytes32 node = _leafForHardcodedSubdigest(hardcoded); root = root != bytes32(0) ? LibOptim.fkeccak256(root, node) : node; continue; } revert InvalidSignatureFlag(flag); } } } /** * @notice Returns the threshold, weight, root, and checkpoint of a signature. * @dev To verify the signature, the weight must be greater than or equal to the threshold, and the root * must match the expected `imageHash` of the wallet. * * @param _subdigest The digest to verify the signature against. * @param _signature The signature to recover. * @return threshold The minimum weight required for the signature to be valid. * @return weight The total weight of the recovered signatures. * @return imageHash The root hash of the recovered configuration * @return checkpoint The checkpoint of the signature. */ function recover( bytes32 _subdigest, bytes calldata _signature ) internal view returns ( uint256 threshold, uint256 weight, bytes32 imageHash, uint256 checkpoint ) { unchecked { (weight, imageHash) = recoverBranch(_subdigest, _signature[6:]); // Threshold & checkpoint are the top nodes // (but they are first on the signature) threshold = LibBytes.readFirstUint16(_signature); checkpoint = LibBytes.readUint32(_signature, 2); imageHash = LibOptim.fkeccak256(imageHash, bytes32(threshold)); imageHash = LibOptim.fkeccak256(imageHash, bytes32(checkpoint)); } } }