Skip to content

Commit

Permalink
client/asset: SignMessage hashes message
Browse files Browse the repository at this point in the history
Update Wallet.SignMessage impls to use an asset-specific hash function
with the provided message.  The resulting signatures will not be
accepted by legacy servers.
  • Loading branch information
chappjc committed Apr 19, 2022
1 parent 3904557 commit 8aaed77
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 6 deletions.
5 changes: 3 additions & 2 deletions client/asset/btc/btc.go
Original file line number Diff line number Diff line change
Expand Up @@ -2184,12 +2184,13 @@ func (btc *baseWallet) SignMessage(coin asset.Coin, msg dex.Bytes) (pubkeys, sig
return nil, nil, err
}
pk := privKey.PubKey()
sig, err := privKey.Sign(msg)
hash := chainhash.HashB(msg) // legacy servers will not accept this signature!
sig, err := privKey.Sign(hash)
if err != nil {
return nil, nil, err
}
pubkeys = append(pubkeys, pk.SerializeCompressed())
sigs = append(sigs, sig.Serialize())
sigs = append(sigs, sig.Serialize()) // DER format serialization
return
}

Expand Down
3 changes: 2 additions & 1 deletion client/asset/btc/btc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1862,8 +1862,9 @@ func testSignMessage(t *testing.T, segwit bool, walletType string) {
}

msg := randBytes(36)
msgHash := chainhash.HashB(msg)
pk := pubKey.SerializeCompressed()
signature, err := privKey.Sign(msg)
signature, err := privKey.Sign(msgHash)
if err != nil {
t.Fatalf("signature error: %v", err)
}
Expand Down
5 changes: 3 additions & 2 deletions client/asset/dcr/dcr.go
Original file line number Diff line number Diff line change
Expand Up @@ -1788,9 +1788,10 @@ func (dcr *ExchangeWallet) SignMessage(coin asset.Coin, msg dex.Bytes) (pubkeys,
return nil, nil, err
}
defer priv.Zero()
signature := ecdsa.Sign(priv, msg)
hash := chainhash.HashB(msg) // legacy servers will not accept this signature!
signature := ecdsa.Sign(priv, hash)
pubkeys = append(pubkeys, priv.PubKey().SerializeCompressed())
sigs = append(sigs, signature.Serialize())
sigs = append(sigs, signature.Serialize()) // DER format
return pubkeys, sigs, nil
}

Expand Down
3 changes: 2 additions & 1 deletion client/asset/dcr/dcr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1557,7 +1557,8 @@ func TestSignMessage(t *testing.T) {

msg := randBytes(36)
pk := pubKey.SerializeCompressed()
signature := ecdsa.Sign(privKey, msg)
msgHash := chainhash.HashB(msg)
signature := ecdsa.Sign(privKey, msgHash)
sig := signature.Serialize()

node.privWIF, err = dcrutil.NewWIF(privBytes, tChainParams.PrivateKeyID, dcrec.STEcdsaSecp256k1)
Expand Down

0 comments on commit 8aaed77

Please sign in to comment.