Skip to content

Commit

Permalink
fix "signmessage" for bliss address, add "verifyblissmessage"
Browse files Browse the repository at this point in the history
  • Loading branch information
Fanlq committed Feb 2, 2018
1 parent 9b2775d commit 1f2747d
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 0 deletions.
34 changes: 34 additions & 0 deletions crypto/bliss/signature.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package bliss
import (
"github.com/LoCCS/bliss"
hcashcrypto "github.com/HcashOrg/hcashd/crypto"
"github.com/LoCCS/bliss/sampler"
"crypto/rand"
)

type Signature struct{
Expand All @@ -16,4 +18,36 @@ func (s Signature) GetType() int {

func (s Signature) Serialize() []byte{
return s.Signature.Serialize()
}

func SignCompact(key hcashcrypto.PrivateKey, hash []byte)([]byte, error) {

seed := make([]byte, sampler.SHA_512_DIGEST_LENGTH)
rand.Read(seed)
entropy, err := sampler.NewEntropy(seed)
if err != nil{
return nil, err
}
var sig *bliss.Signature
switch pv := key.(type){
case PrivateKey:
sig, err = pv.Sign(hash, entropy)
case *PrivateKey:
sig, err = pv.Sign(hash, entropy)
}

if err != nil{
return nil, err
}

result := sig.Serialize()
return result, err
}

func VerifyCompact(key hcashcrypto.PublicKey, messageHash, sign []byte) (bool, error){

sig,_ := bliss.DeserializeBlissSignature(sign)
result, err := key.(*PublicKey).Verify(messageHash, sig)

return result, err
}
18 changes: 18 additions & 0 deletions hcashjson/chainsvrcmds.go
Original file line number Diff line number Diff line change
Expand Up @@ -744,6 +744,23 @@ func NewVerifyMessageCmd(address, signature, message string) *VerifyMessageCmd {
}
}

// VerifyBlissMessageCmd defines the verifyblissmessage JSON-RPC command.
type VerifyBlissMessageCmd struct {
PubKey string
Signature string
Message string
}

// NewVerifyBlissMessageCmd returns a new instance which can be used to issue a
// verifyblissmessage JSON-RPC command.
func NewVerifyBlissMessageCmd(pubkey, signature, message string) *VerifyBlissMessageCmd {
return &VerifyBlissMessageCmd{
PubKey: pubkey,
Signature: signature,
Message: message,
}
}

func init() {
// No special flags for commands in this file.
flags := UsageFlag(0)
Expand Down Expand Up @@ -792,4 +809,5 @@ func init() {
MustRegisterCmd("validateaddress", (*ValidateAddressCmd)(nil), flags)
MustRegisterCmd("verifychain", (*VerifyChainCmd)(nil), flags)
MustRegisterCmd("verifymessage", (*VerifyMessageCmd)(nil), flags)
MustRegisterCmd("verifyblissmessage", (*VerifyBlissMessageCmd)(nil), flags)
}
36 changes: 36 additions & 0 deletions rpcserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ import (
"github.com/HcashOrg/hcashd/txscript"
"github.com/HcashOrg/hcashd/wire"
"github.com/HcashOrg/hcashutil"
"github.com/HcashOrg/hcashd/crypto/bliss"
)

// API version constants
Expand Down Expand Up @@ -245,6 +246,7 @@ var rpcHandlersBeforeInit = map[string]commandHandler{
"validateaddress": handleValidateAddress,
"verifychain": handleVerifyChain,
"verifymessage": handleVerifyMessage,
"verifyblissmessage": handleVerifyBlissMessage,
"version": handleVersion,
}

Expand Down Expand Up @@ -345,6 +347,7 @@ var rpcLimited = map[string]struct{}{
"submitblock": {},
"validateaddress": {},
"verifymessage": {},
"verifyblissmessage": {},
"version": {},
}

Expand Down Expand Up @@ -6063,6 +6066,39 @@ func handleVerifyMessage(s *rpcServer, cmd interface{}, closeChan <-chan struct{
return address.EncodeAddress() == c.Address, nil
}

func handleVerifyBlissMessage(s *rpcServer, cmd interface{}, closeChan <-chan struct{}) (interface{}, error) {

icmd := cmd.(*hcashjson.VerifyBlissMessageCmd)
var valid bool

pubkey,err := hex.DecodeString(icmd.PubKey)
if err != nil {
return nil, err
}
key,err := bliss.Bliss.ParsePubKey(pubkey)
if err != nil {
return nil, err
}

var buf bytes.Buffer
wire.WriteVarString(&buf, 0, "Hypercash Signed Message:\n")
wire.WriteVarString(&buf, 0, icmd.Message)
messageHash := chainhash.HashB(buf.Bytes())

sig, err := base64.StdEncoding.DecodeString(icmd.Signature)
fmt.Println("sign : " , sig)
if err != nil {
return nil, err
}

valid, err = bliss.VerifyCompact(key,messageHash,sig)
if err != nil {
return nil, err
}

return valid, nil
}

// handleVersion implements the version command.
func handleVersion(s *rpcServer, cmd interface{}, closeChan <-chan struct{}) (interface{}, error) {
result := map[string]hcashjson.VersionResult{
Expand Down
8 changes: 8 additions & 0 deletions rpcserverhelp.go
Original file line number Diff line number Diff line change
Expand Up @@ -752,6 +752,13 @@ var helpDescsEnUS = map[string]string{
"verifymessage-message": "The signed message",
"verifymessage--result0": "Whether or not the signature verified",

// VerifyBlissMessageCmd help.
"verifyblissmessage--synopsis": "Verify a signed message.",
"verifyblissmessage-pubKey": "The hypercash bliss public key to use for the signature",
"verifyblissmessage-signature": "The base-64 encoded signature provided by the signer",
"verifyblissmessage-message": "The signed message",
"verifyblissmessage--result0": "Whether or not the signature verified",

// -------- Websocket-specific help --------

// Session help.
Expand Down Expand Up @@ -978,6 +985,7 @@ var rpcResultTypes = map[string][]interface{}{
"validateaddress": {(*hcashjson.ValidateAddressChainResult)(nil)},
"verifychain": {(*bool)(nil)},
"verifymessage": {(*bool)(nil)},
"verifyblissmessage": {(*bool)(nil)},
"version": {(*map[string]hcashjson.VersionResult)(nil)},

// Websocket commands.
Expand Down

0 comments on commit 1f2747d

Please sign in to comment.