10 生成签名——贴出来的代码是solidity进行签名的验证方法,如何用java生成能通过此验证方法的签名

struct TokenPair {
        address token; // token contract address
        uint256 tokenId; // token id (if applicable)
        uint256 amount; // token amount (if applicable)
        uint8 kind; // token kind (721/1151/mint)
        bytes mintData; // mint data (if applicable)
    }

    struct Detail {                
        bytes32 intentionHash;     
        address signer;            
        uint256 txDeadline;        
        bytes32 salt;
        uint256 id;                
        uint8 opcode;              
        address caller;            
        IERC20 currency;
        uint256 price;
        uint256 incentiveRate;     
        Settlement settlement;     
        TokenPair[] bundle;        
        uint256 deadline;          
    }

    struct Settlement {             
        uint256[] coupons;          
        uint256 feeRate;            
        uint256 royaltyRate;        
        uint256 buyerCashbackRate;  
        address feeAddress;         
        address royaltyAddress;     
    }

    function run(
        Detail calldata detail,
        bytes calldata sigDetail
    ) public pure returns (bool) {
        return isSignatureValid(sigDetail, keccak256(abi.encode(detail)), detail.signer);
    }

    function isSignatureValid(
        bytes memory signature,
        bytes32 hash,
        address signer
    ) public pure returns (bool) {
        // verify hash signed via `personal_sign`
        return ECDSA.recover(ECDSA.toEthSignedMessageHash(hash), signature) == signer;
    }
请先 登录 后评论

最佳答案 2023-05-05 21:10

我自己解决了,贴了下代码,开袋即食

String inputstr = TypeEncoder.encode(new DynamicStruct(new Address("address"), new Uint256(2)));
        byte[] decode = Hash.sha3(Hex.decode(inputstr));
        ECKeyPair keyPair = ECKeyPair.create(Hex.decode("privateKey"));
        Sign.SignatureData signatureData = Sign.signPrefixedMessage(decode, keyPair);
        String sign = "0x" + new String(Hex.encode(signatureData.getR())) + new String(Hex.encode(signatureData.getS())) + new String(Hex.encode(signatureData.getV()));
        System.out.println(sign);
请先 登录 后评论

其它 1 个回答

用户_15137
请先 登录 后评论
  • 2 关注
  • 0 收藏,2028 浏览
  • kevin_wang 提出于 2023-05-04 15:55