EExcel 丞燕快速查詢2

EExcel 丞燕快速查詢2
EExcel 丞燕快速查詢2 https://sandk.ffbizs.com/

ethereum sign verify ECDSA part 1

呼叫web3的部份,都需要使用ethereum geth,這部份有點麻煩

Call web3 must use ethereum, this mean need to run ganache or geth. No ok.


jsrsasign is offline to compute

ECDSA 相關的是 https://kjur.github.io/jsrsasign/sample/sample-ecdsa.html

=====
https://medium.com/@angellopozo/ethereum-signing-and-validating-13a2d7cb0ee3
https://dzone.com/articles/signing-and-verifying-ethereum-signatures

public address 只是 verify後拿來驗證是否相同

另一句話 verify後會產生public address,主要是拿sign後的值產生 r s v ,然後再用 合約的功能 ecrecover 處理


public address only for after verify product check

Other way to explain is

After verify get public address. Take signatures to make r s v, then use r s v with contract ecrecover(). ecrecover() run finish get public address.

=====

Use npm secp256k1. When you run code


Web3 = require("web3")
var web3 = new Web3(Web3.givenProvider || 'ws://some.local-or-remote.node:8546');

const { randomBytes } = require('crypto')
const secp256k1 = require('secp256k1')
// or require('secp256k1/elliptic')
//   if you want to use pure js implementation in node

// generate message to sign
const msg = Buffer.alloc(32, "Hello") //randomBytes(32)

// generate privKey
let privKey
do {
  privKey = randomBytes(32)
} while (!secp256k1.privateKeyVerify(privKey))

var buf_privatekey = Buffer.from(web3.utils.hexToBytes('0x75b25b96be4313c5a102bd4daa6bbeb71414f23e0ae15c0f93fa6d17866003da'))
console.log("privatekey: %s", web3.utils.bytesToHex(buf_privatekey))

// get the public key in a compressed format
const pubKey = secp256k1.publicKeyCreate(privKey)
console.log("pubKey: %s", web3.utils.bytesToHex(pubKey))

// sign the message
const sigObj = secp256k1.sign(msg, privKey)

//Message Signature Hash 長度
console.log(Buffer.from(web3.utils.hexToBytes("0xa05ac71b16172777f683edbc48e9709cffd713a82630232d7c98e0f0df5201d60329658dba83b53fed49307e03d9663c0d2e4476c8b7925c2ed02cc7f881f5271b")).length)

//public key
pubkey = Buffer.from(web3.utils.hexToBytes("0x034e17dc4aef81e0ce6d16686be5e194274795375fc5525f1cdc46fe0b4643d5d6"))

//標準signature 是64
console.log(sigObj.signature.length)
console.log("secp256k1 sign finish hex: ", web3.utils.bytesToHex(sigObj.signature))

signature = Buffer.from(web3.utils.hexToBytes("0xa05ac71b16172777f683edbc48e9709cffd713a82630232d7c98e0f0df5201d60329658dba83b53fed49307e03d9663c0d2e4476c8b7925c2ed02cc7f881f5271b"))
console.log(web3.utils.bytesToHex(signature))
console.log(secp256k1.verify(msg, signature, pubKey))

// verify the signature
//console.log(secp256k1.verify(msg, sigObj.signature, pubKey))
// => true


https://etherscan.io/verifySig

Address
0xAD44A8ea9A9Bb5eF66F041BB921A687331729eB4

Message Signature Hash
0xa05ac71b16172777f683edbc48e9709cffd713a82630232d7c98e0f0df5201d60329658dba83b53fed49307e03d9663c0d2e4476c8b7925c2ed02cc7f881f5271b

Enter the original message that was signed
Hello

verify ok

Go back see code.
Message Signature Hash 長度 is 65
sigObj.signature.length is 64

So https://github.com/ethereum/go-ethereum/blob/dbb03fe9893dd19f6b1de1ee3b768317f22fd135/crypto/secp256k1/secp256.go#L159

This is Why. And

https://github.com/ethereum/go-ethereum/blob/dbb03fe9893dd19f6b1de1ee3b768317f22fd135/crypto/secp256k1/secp256.go#L114