Sign and verify using ed25519 in near protocol

一笑奈何 提交于 2020-06-27 18:37:37

问题


I am trying to sign the message in javascript using signMessage and verify it with python for implementing jwt authentication.

This is the code for signing message in javascript using near-api-js

window.signer = await new nearlib.InMemorySigner(window.walletAccount._keyStore)
const mysign = await window.signer.signMessage("Amiya", 
window.walletAccount._authData.accountId, window.walletAccount._networkId)
let mypubdata = ""
mysign.publicKey.data.forEach( function (item,index){
  if(item < 16) {
    mypubdata = mypubdata + '0' + item.toString(16)
  }
  else {
    mypubdata = mypubdata + item.toString(16)
  }

})
console.log("public key", mypubdata)
let mysignature = ""
mysign.signature.forEach( function (item,index){
  if(item < 16) {
    mysignature = mysignature + '0' + item.toString(16)
  }
  else {
    mysignature = mysignature + item.toString(16)
  }

})
console.log("signature", mysignature)

Output gives:
public key fe20d3e271876c8329c74dcdbe95e32586ee5cf67def1c0cc9e0b8d0e4285813
signature 61d864f40667075da6f920f811def3b83330a6cce49b7bd24eb4711f29abcf55d6d2eaf6f67bf74f20a2f79598f7fd42b4f70db41446d73d596b58d31825710c

This is my python code for backend:

import ed25519
import hashlib
pubKey = ed25519.VerifyingKey(b"fe20d3e271876c8329c74dcdbe95e32586ee5cf67def1c0cc9e0b8d0e4285813", encoding="hex")

print("Public key (32 bytes): ", pubKey.to_ascii(encoding='hex'))
signature = "61d864f40667075da6f920f811def3b83330a6cce49b7bd24eb4711f29abcf55d6d2eaf6f67bf74f20a2f79598f7fd42b4f70db41446d73d596b58d31825710c"
msg = hashlib.sha256(b"Amiya").hexdigest()
print(msg)

try:
    pubKey.verify(signature, msg, encoding='hex')
    print("The signature is valid.")
except:
    print("Invalid signature!")

But I am unable to make it work, it gives an invalid signature.


回答1:


Formalizing the answer given by @topaco in the comment above:

Try digest() instead of hexdigest() – Topaco May 27 at 18:13



来源:https://stackoverflow.com/questions/62049349/sign-and-verify-using-ed25519-in-near-protocol

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!