unisat-wallet / extension

The first open-source browser extension wallet for Bitcoin NFTs
Other
856 stars 334 forks source link

help me~ "Uncaught ReferenceError: global is not defined" when i using unisat wallet to verify sign #168

Open yimingWOW opened 6 months ago

yimingWOW commented 6 months ago

i want to use unisat wallet to sign a msg, that's my code:

import { verifyMessage } from "@unisat/wallet-utils";

const useWallet = async () => { try { if (typeof window.unisat !== 'undefined') { console.log('unisat Wallet is installed!'); const accounts = await window.unisat.requestAccounts(); const publicKey = await window.unisat.getPublicKey() const signature = await window.unisat.signMessage(accounts[0]); const result = verifyMessage(pubkey,message,signature);

then i got an error, when start my react project: chunk-Y5IW5QF7.js?v=45de4bcc:903 Uncaught ReferenceError: global is not defined at node_modules/bitcore-lib/index.js (@unisat_wallet-utils.js?v=39e9ba3b:28823:27) at __require (chunk-UV5CTPV7.js?v=45de4bcc:9:50) at @unisat_wallet-utils.js?v=39e9ba3b:28869:34

yimingWOW commented 6 months ago

i sended it, but haven't received any reply. Maybe someone else knows the solution to this problem

I've been working on implementing signature functionality in my application, and I'm currently using the Unisat wallet for signing operations. However, when attempting to verify these signatures on my backend server, I encountered some difficulties. I've included snippets of both my React code for signing the message and my Golang code for verification below:

// import { verifyMessage } from "@unisat/wallet-utils"; const useWallet = async () => { try { if (typeof window.unisat !== 'undefined') { console.log('unisat Wallet is installed!'); const accounts = await window.unisat.requestAccounts(); const publicKey = await window.unisat.getPublicKey() const currentUnixTimestamp = Math.floor(Date.now() / 1000); const todayTimestamp = currentUnixTimestamp - (currentUnixTimestamp % 86400); const signature = await window.unisat.signMessage(accounts[0] + "_" + todayTimestamp); // const result = verifyMessage(pubkey,message,signature); return { account: accounts ? accounts[0] : null, publicKey, signature }; } else { throw new Error('unisat Wallet not installed'); } } catch (error) { console.log('useWizz failed', error); // Use error instead of err return { account: null,publicKey:'', signature: '' }; // Return empty signature } }; export default useWallet;

package main

import ( "encoding/base64" "encoding/hex" "fmt"

"[github.com/btcsuite/btcd/btcec](http://github.com/btcsuite/btcd/btcec)"
"[github.com/btcsuite/btcd/chaincfg/chainhash](http://github.com/btcsuite/btcd/chaincfg/chainhash)"

)

func main() { // import private key, sign and verify pkBytes, err := hex.DecodeString("22a47fa09a223f2aa079edf85a7c2d4f8720ee63e502ee2869afab7de234b80c") if err != nil { fmt.Println(err) return } privKey, pubKey := btcec.PrivKeyFromBytes(btcec.S256(), pkBytes) pubKeyHex := hex.EncodeToString(pubKey.SerializeCompressed()) message := "bc1pkgpgdhlt5stnz4gn89d3h2vjm3shu90ysuk7dv8guw0ywrwppm9qa2l4qe_1711411200" messageHash := chainhash.DoubleHashB([]byte(message)) signature, err := privKey.Sign(messageHash) if err != nil { fmt.Println(err) return } serializedSignature := signature.Serialize() signatureBase64 := base64.StdEncoding.EncodeToString(serializedSignature) verified := signature.Verify(messageHash, pubKey) fmt.Printf("Signature Verified? %v\n", verified)

// Serialize and display the signature.
fmt.Println("Public Key:", pubKeyHex)
fmt.Println("sign message:", message)
fmt.Printf("Serialized Signature: %x\n", signature.Serialize())
fmt.Println("Base64 Signature:", signatureBase64)

fmt.Println("------------------------------------------------------")
fmt.Println("i signed same message using unisat wallet, and got signature in form of base64, but i fetch an error: no header magic")
// btc ecdsa's signature should using DER format, and the first byte should be 0x30
decodedSignature, err := base64.StdEncoding.DecodeString("H1YiOkR0zutMaKurwXV+3rRgjAIpk5x4HYOrlEbHotx4eyDvhqJzAvraY366M6tlV857DLDXC6gMicN0KVn7Buw=")
if err != nil {
    fmt.Println("Error decoding signature from Base64:", err)
    return
}
sig, err := btcec.ParseSignature(decodedSignature, btcec.S256())
if err != nil {
    fmt.Println("Error parsing signature:", err)
    return
}
verified = sig.Verify(messageHash, pubKey)
fmt.Printf("Signature Verified? %v\n", verified)

}

From my understanding, Bitcoin (BTC) ECDSA signatures should adhere to the DER format, with the first byte being 0x30. However, it seems that the signatures generated by the Unisat wallet may not be in this expected format.

GGG888GGG commented 6 months ago

@huanniangstudio

GGG888GGG commented 5 months ago

@abangZ

yimingWOW commented 5 months ago

😂