Skip to content

Commit

Permalink
Expose more function for testing
Browse files Browse the repository at this point in the history
  • Loading branch information
Senyoret1 committed Jun 5, 2018
1 parent 9210bfb commit 36a3cc2
Show file tree
Hide file tree
Showing 3 changed files with 149 additions and 0 deletions.
93 changes: 93 additions & 0 deletions liteclient/extras.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
package liteclient

import (
"github.com/skycoin/skycoin/src/cipher"
"github.com/skycoin/skycoin/src/cipher/secp256k1-go"
)

/*
Functions used mainly during test procedures.
*/

// VerifySignature verifies that hash was signed by PubKey
func VerifySignature(pubkey string, sig string, hash string) {
p := cipher.MustPubKeyFromHex(pubkey)
s := cipher.MustSigFromHex(sig)
h := cipher.MustSHA256FromHex(hash)

err := cipher.VerifySignature(p, s, h)
if err != nil {
panic(err)
}
}

// ChkSig checks whether PubKey corresponding to address hash signed hash
func ChkSig(address string, hash string, sig string) {
a := cipher.MustDecodeBase58Address(address)
h := cipher.MustSHA256FromHex(hash)
s := cipher.MustSigFromHex(sig)

err := cipher.ChkSig(a, h, s)
if err != nil {
panic(err)
}
}

// VerifySignedHash this only checks that the signature can be converted to a public key
// Since there is no pubkey or address argument, it cannot check that the
// signature is valid in that context.
func VerifySignedHash(sig string, hash string) {
s := cipher.MustSigFromHex(sig)
h := cipher.MustSHA256FromHex(hash)

err := cipher.VerifySignedHash(s, h)
if err != nil {
panic(err)
}
}

// VerifySeckey validate a private key
func VerifySeckey(seckey string) int {
s := cipher.MustSecKeyFromHex(seckey)
return secp256k1.VerifySeckey(s[:])
}

// VerifyPubkey validate a public key
func VerifyPubkey(pubkey string) int {
p := cipher.MustPubKeyFromHex(pubkey)
return secp256k1.VerifyPubkey(p[:])
}

// AddressFromPubKey creates Address from PubKey as ripemd160(sha256(sha256(pubkey)))
func AddressFromPubKey(pubkey string) string {
p := cipher.MustPubKeyFromHex(pubkey)
return cipher.AddressFromPubKey(p).String()
}

// AddressFromSecKey generates address from secret key
func AddressFromSecKey(seckey string) string {
s := cipher.MustSecKeyFromHex(seckey)
return cipher.AddressFromSecKey(s).String()
}

// PubKeyFromSig recovers the public key from a signed hash
func PubKeyFromSig(sig string, hash string) string {
s := cipher.MustSigFromHex(sig)
h := cipher.MustSHA256FromHex(hash)

pubKey, err := cipher.PubKeyFromSig(s, h)
if err != nil {
panic(err)
}

return pubKey.Hex()
}

// SignHash sign hash
func SignHash(hash string, seckey string) string {
h := cipher.MustSHA256FromHex(hash)
s := cipher.MustSecKeyFromHex(seckey)

sig := cipher.SignHash(h, s)
return sig.Hex()
}
12 changes: 12 additions & 0 deletions skycoin/skycoin.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,16 @@ func main() {
"GenerateAddresses": liteclient.GenerateAddress,
"PrepareTransaction": liteclient.PrepareTransaction,
})

js.Global.Set("CipherExtras", map[string]interface{}{
"VerifySignature": liteclient.VerifySignature,
"ChkSig": liteclient.ChkSig,
"VerifySignedHash": liteclient.VerifySignedHash,
"VerifySeckey": liteclient.VerifySeckey,
"VerifyPubkey": liteclient.VerifyPubkey,
"AddressFromPubKey": liteclient.AddressFromPubKey,
"AddressFromSecKey": liteclient.AddressFromSecKey,
"PubKeyFromSig": liteclient.PubKeyFromSig,
"SignHash": liteclient.SignHash,
})
}
44 changes: 44 additions & 0 deletions skycoin/skycoin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ import (
)

func TestGenerateAddress(t *testing.T) {

//client.go

stringSeed := "abcdefg"
seed := hex.EncodeToString([]byte(stringSeed))

Expand All @@ -30,4 +33,45 @@ func TestGenerateAddress(t *testing.T) {
if nextAddr.Public != "023ea091a649aef9d8fe5b956bd5cf2a9e47b8a7ba41f8b83997de0d1b1a851e73" {
t.Fatalf("GenerateAddress pubkey is invalid 2")
}

//extras.go

pubkey := "037c4cff096a7219b17f8502b9ed643c947d5d4929c1a141b3240f70b60a15a7b8"
seckey := "697c7cfba3c6d13dc6bd3f063c60ef4d25de903e50fe8c5e123e5efb08e21e29"
address := "XZ9S3QKN5tSRVswDNE6GLCtTfm8DqRthyA"
signature := "ba85034f675b8a284537a0c23e4d55d5f8b4cc1620eee077a478482b8f7bf9304d17c16dd5aaf70d24df262c531f37e58a57469d1161d6134075ed8c203f7cbc01"
hash := "72cd6e8422c407fb6d098690f1130b7ded7ec2f7f5e1d30bd9d521f015363793"

liteclient.VerifySignature(pubkey, signature, hash)

liteclient.ChkSig(address, hash, signature)

liteclient.VerifySignedHash(signature, hash)

if liteclient.VerifySeckey(seckey) != 1 {
t.Fatalf("secp256k1.VerifySeckey failed")
}

if liteclient.VerifyPubkey(pubkey) != 1 {
t.Fatalf("secp256k1.VerifyPubkey failed")
}

if liteclient.AddressFromPubKey(pubkey) != address {
t.Fatalf("cipher.AddressFromPubKey failed")
}

if liteclient.AddressFromSecKey(seckey) != address {
t.Fatalf("cipher.AddressFromPubKey failed")
}

pubkey2 := liteclient.PubKeyFromSig(signature, hash)
if pubkey2 != pubkey {
t.Fatalf("cipher.AddressFromPubKey failed")
}

signHash := liteclient.SignHash(hash, seckey)
if signHash == "" {
t.Fatalf("created signature is null")
}

}

0 comments on commit 36a3cc2

Please sign in to comment.