如何验证交易是否由NEAR协议中的给定帐户ID签名?

时间:2020-03-24 13:12:12

标签: nearprotocol

我有一个已签名的交易,我想验证它是否由NEAR上存在的帐户ID签名。

1 个答案:

答案 0 :(得分:2)

问题是三个方面:

  1. 检查交易哈希。使用Borsh序列化事务并计算sha256,它应与transaction.hash相匹配。
  2. 验证签名对于给定的公钥有效。使用可以验证给定签名类型的所有可用工具。 Python中有ed25519包,因此示例如下:

    import ed25519
    import base58
    
    transaction = {
        "hash": "EzvvJEqdxKA62oAjG32y5herDtRGYEdmZXHDDyxorJ48",
        "public_key": "ed25519:oNCFEmRotRHTySKqmAwifNZ8VRpYS973p8cL61y7eiE",
        "signature": "ed25519:46WsywQYe31isWcHASrnR2pYCja4Mtjzs4n87isrTHWyDdX5uwrUaUn4SbhY9BMwWKvWwdcuyCjMsXdkXdPJv8ko",
    }
    
    pk_bytes = base58.b58decode(transaction['public_key'][len('ed25519:'):])
    pk = ed25519.VerifyingKey(pk_bytes)
    
    # OK:
    pk.verify(
        sig=base58.b58decode(transaction['signature'][len('ed25519:'):]),
        msg=base58.b58decode(transaction['hash'])
    )
    
    # Exception BadSignatureError:
    pk.verify(
        sig=base58.b58decode('66666666' + transaction['signature'][len('ed25519:') + 8:]),
        msg=base58.b58decode(transaction['hash'])
    )
    
  3. 使用Near-shell,Near-api- *或原始RPC调用检查给定帐户ID的密钥是否存在:

    $ http post https://rpc.nearprotocol.com method=query params:='["access_key/test/ed25519:oNCFEmRotRHTySKqmAwifNZ8VRpYS973p8cL61y7eiE", ""]' id=123 jsonrpc=2.0
    
    {
        ...
        "result": {
            "block_height": 3297678,
            "nonce": 10493,
            "permission": "FullAccess"
        }
    }