add testnet and regtest address validation
This commit is contained in:
parent
37a8c4cae1
commit
4cff2dd233
11 changed files with 144 additions and 84 deletions
|
@ -2,12 +2,11 @@ package address
|
||||||
|
|
||||||
import "testing"
|
import "testing"
|
||||||
|
|
||||||
|
func TestDecodeAddressLBRYCrdMain(t *testing.T) {
|
||||||
func TestDecodeAddress(t *testing.T) {
|
|
||||||
addr := "bUc9gyCJPKu2CBYpTvJ98MdmsLb68utjP6"
|
addr := "bUc9gyCJPKu2CBYpTvJ98MdmsLb68utjP6"
|
||||||
correct := [25]byte{85, 174, 41, 64, 245, 110, 91, 239, 43, 208, 32, 73, 115, 20, 70, 204, 83, 199, 3,
|
correct := [25]byte{85, 174, 41, 64, 245, 110, 91, 239, 43, 208, 32, 73, 115, 20, 70, 204, 83, 199, 3,
|
||||||
206, 210, 176, 194, 188, 193}
|
206, 210, 176, 194, 188, 193}
|
||||||
result, err := DecodeAddress(addr)
|
result, err := DecodeAddress(addr, "lbrycrd_main")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Error(err)
|
t.Error(err)
|
||||||
}
|
}
|
||||||
|
@ -16,11 +15,11 @@ func TestDecodeAddress(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestEncodeAddress(t *testing.T) {
|
func TestEncodeAddressLBRYCrdMain(t *testing.T) {
|
||||||
addr := [25]byte{85, 174, 41, 64, 245, 110, 91, 239, 43, 208, 32, 73, 115, 20, 70, 204, 83, 199, 3,
|
addr := [25]byte{85, 174, 41, 64, 245, 110, 91, 239, 43, 208, 32, 73, 115, 20, 70, 204, 83, 199, 3,
|
||||||
206, 210, 176, 194, 188, 193}
|
206, 210, 176, 194, 188, 193}
|
||||||
correct := "bUc9gyCJPKu2CBYpTvJ98MdmsLb68utjP6"
|
correct := "bUc9gyCJPKu2CBYpTvJ98MdmsLb68utjP6"
|
||||||
result, err := EncodeAddress(addr)
|
result, err := EncodeAddress(addr, "lbrycrd_main")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Error(err)
|
t.Error(err)
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,11 +1,11 @@
|
||||||
package address
|
package address
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
|
||||||
"./base58"
|
"./base58"
|
||||||
|
"errors"
|
||||||
)
|
)
|
||||||
|
|
||||||
func DecodeAddress(address string) ([address_length]byte, error) {
|
func DecodeAddress(address string, blockchainName string) ([address_length]byte, error) {
|
||||||
decoded, err := base58.DecodeBase58(address, address_length)
|
decoded, err := base58.DecodeBase58(address, address_length)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return [address_length]byte{}, errors.New("failed to decode")
|
return [address_length]byte{}, errors.New("failed to decode")
|
||||||
|
@ -14,5 +14,5 @@ func DecodeAddress(address string) ([address_length]byte, error) {
|
||||||
for i, b := range decoded {
|
for i, b := range decoded {
|
||||||
buf[i] = b
|
buf[i] = b
|
||||||
}
|
}
|
||||||
return ValidateAddress(buf)
|
return ValidateAddress(buf, blockchainName)
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,8 +4,8 @@ import (
|
||||||
"./base58"
|
"./base58"
|
||||||
)
|
)
|
||||||
|
|
||||||
func EncodeAddress(address [address_length]byte) (string, error) {
|
func EncodeAddress(address [address_length]byte, blockchainName string) (string, error) {
|
||||||
buf, err := ValidateAddress(address)
|
buf, err := ValidateAddress(address, blockchainName)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,22 +1,37 @@
|
||||||
package address
|
package address
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
|
||||||
"crypto/sha256"
|
"crypto/sha256"
|
||||||
|
"errors"
|
||||||
)
|
)
|
||||||
|
|
||||||
const pubkey_prefix = byte(85)
|
const lbrycrd_main_pubkey_prefix = byte(85)
|
||||||
const script_prefix = byte(122)
|
const lbrycrd_main_script_prefix = byte(122)
|
||||||
|
const lbrycrd_testnet_pubkey_prefix = byte(111)
|
||||||
|
const lbrycrd_testnet_script_prefix = byte(196)
|
||||||
|
const lbrycrd_regtest_pubkey_prefix = byte(111)
|
||||||
|
const lbrycrd_regtest_script_prefix = byte(196)
|
||||||
|
|
||||||
const prefix_length = 1
|
const prefix_length = 1
|
||||||
const pubkey_length = 20
|
const pubkey_length = 20
|
||||||
const checksum_length = 4
|
const checksum_length = 4
|
||||||
const address_length = prefix_length + pubkey_length + checksum_length
|
const address_length = prefix_length + pubkey_length + checksum_length
|
||||||
var address_prefixes = [2]byte {pubkey_prefix, script_prefix}
|
const lbrycrd_main = "lbrycrd_main"
|
||||||
|
const lbrycrd_testnet = "lbrycrd_testnet"
|
||||||
|
const lbrycrd_regtest = "lbrycrd_regtest"
|
||||||
|
|
||||||
|
var address_prefixes = map[string][2]byte{}
|
||||||
|
|
||||||
func PrefixIsValid(address [address_length]byte) bool {
|
func SetPrefixes() {
|
||||||
|
address_prefixes[lbrycrd_main] = [2]byte{lbrycrd_main_pubkey_prefix, lbrycrd_main_script_prefix}
|
||||||
|
address_prefixes[lbrycrd_testnet] = [2]byte{lbrycrd_testnet_pubkey_prefix, lbrycrd_testnet_script_prefix}
|
||||||
|
address_prefixes[lbrycrd_regtest] = [2]byte{lbrycrd_regtest_pubkey_prefix, lbrycrd_regtest_script_prefix}
|
||||||
|
}
|
||||||
|
|
||||||
|
func PrefixIsValid(address [address_length]byte, blockchainName string) bool {
|
||||||
|
SetPrefixes()
|
||||||
prefix := address[0]
|
prefix := address[0]
|
||||||
for _, addr_prefix := range address_prefixes {
|
for _, addr_prefix := range address_prefixes[blockchainName] {
|
||||||
if addr_prefix == prefix {
|
if addr_prefix == prefix {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
@ -25,7 +40,7 @@ func PrefixIsValid(address [address_length]byte) bool {
|
||||||
}
|
}
|
||||||
|
|
||||||
func PubKeyIsValid(address [address_length]byte) bool {
|
func PubKeyIsValid(address [address_length]byte) bool {
|
||||||
pubkey := address[prefix_length:pubkey_length+prefix_length]
|
pubkey := address[prefix_length : pubkey_length+prefix_length]
|
||||||
// TODO: validate this for real
|
// TODO: validate this for real
|
||||||
if len(pubkey) != pubkey_length {
|
if len(pubkey) != pubkey_length {
|
||||||
return false
|
return false
|
||||||
|
@ -35,7 +50,9 @@ func PubKeyIsValid(address [address_length]byte) bool {
|
||||||
|
|
||||||
func ChecksumIsValid(address [address_length]byte) bool {
|
func ChecksumIsValid(address [address_length]byte) bool {
|
||||||
checksum := [checksum_length]byte{}
|
checksum := [checksum_length]byte{}
|
||||||
for i := range checksum {checksum[i] = address[prefix_length+pubkey_length+i]}
|
for i := range checksum {
|
||||||
|
checksum[i] = address[prefix_length+pubkey_length+i]
|
||||||
|
}
|
||||||
real_checksum := sha256.Sum256(address[:prefix_length+pubkey_length])
|
real_checksum := sha256.Sum256(address[:prefix_length+pubkey_length])
|
||||||
real_checksum = sha256.Sum256(real_checksum[:])
|
real_checksum = sha256.Sum256(real_checksum[:])
|
||||||
for i, c := range checksum {
|
for i, c := range checksum {
|
||||||
|
@ -46,8 +63,11 @@ func ChecksumIsValid(address [address_length]byte) bool {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
func ValidateAddress(address [address_length]byte) ([address_length]byte, error) {
|
func ValidateAddress(address [address_length]byte, blockchainName string) ([address_length]byte, error) {
|
||||||
if !PrefixIsValid(address) {
|
if blockchainName != lbrycrd_main && blockchainName != lbrycrd_testnet && blockchainName != lbrycrd_regtest {
|
||||||
|
return address, errors.New("invalid blockchain name")
|
||||||
|
}
|
||||||
|
if !PrefixIsValid(address, blockchainName) {
|
||||||
return address, errors.New("invalid prefix")
|
return address, errors.New("invalid prefix")
|
||||||
}
|
}
|
||||||
if !PubKeyIsValid(address) {
|
if !PubKeyIsValid(address) {
|
||||||
|
|
|
@ -1,20 +1,21 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"C"
|
|
||||||
"../claim"
|
"../claim"
|
||||||
|
"C"
|
||||||
)
|
)
|
||||||
|
|
||||||
//export VerifySignature
|
//export VerifySignature
|
||||||
func VerifySignature(claimHex string, certificateHex string, claimAddress string, certificateId string) bool {
|
func VerifySignature(claimHex string, certificateHex string, claimAddress string, certificateId string, blockchainName string) bool {
|
||||||
decodedClaim, err := claim.DecodeClaimHex(claimHex)
|
decodedClaim, err := claim.DecodeClaimHex(claimHex, blockchainName)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
decodedCertificate, err := claim.DecodeClaimHex(certificateHex)
|
decodedCertificate, err := claim.DecodeClaimHex(certificateHex, blockchainName)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
result, err := decodedClaim.ValidateClaimSignature(decodedCertificate, claimAddress, certificateId)
|
result, err := decodedClaim.ValidateClaimSignature(decodedCertificate, claimAddress, certificateId, blockchainName)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
@ -25,8 +26,8 @@ func VerifySignature(claimHex string, certificateHex string, claimAddress string
|
||||||
}
|
}
|
||||||
|
|
||||||
//export DecodeClaimHex
|
//export DecodeClaimHex
|
||||||
func DecodeClaimHex(claimHex string) *C.char {
|
func DecodeClaimHex(claimHex string, blockchainName string) *C.char {
|
||||||
decodedClaim, err := claim.DecodeClaimHex(claimHex)
|
decodedClaim, err := claim.DecodeClaimHex(claimHex, blockchainName)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return C.CString("decode error")
|
return C.CString("decode error")
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,18 +1,41 @@
|
||||||
package claim
|
package claim
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/golang/protobuf/proto"
|
"../address"
|
||||||
"encoding/hex"
|
|
||||||
"../pb"
|
"../pb"
|
||||||
|
"encoding/hex"
|
||||||
"errors"
|
"errors"
|
||||||
|
"github.com/golang/protobuf/proto"
|
||||||
)
|
)
|
||||||
|
|
||||||
type ClaimHelper struct {
|
type ClaimHelper struct {
|
||||||
*pb.Claim
|
*pb.Claim
|
||||||
}
|
}
|
||||||
|
|
||||||
func (claim *ClaimHelper) LoadFromBytes(raw_claim []byte) (error) {
|
func (c *ClaimHelper) ValidateAddresses(blockchainName string) error {
|
||||||
if claim.String() != "" {
|
// check the validity of a fee address
|
||||||
|
if c.GetClaimType() == pb.Claim_streamType {
|
||||||
|
fee := c.GetStream().GetMetadata().GetFee()
|
||||||
|
if fee.String() != "" {
|
||||||
|
tmp_addr := fee.GetAddress()
|
||||||
|
if len(tmp_addr) != 25 {
|
||||||
|
return errors.New("invalid address length")
|
||||||
|
}
|
||||||
|
addr := [25]byte{}
|
||||||
|
for i := range addr {
|
||||||
|
addr[i] = tmp_addr[i]
|
||||||
|
}
|
||||||
|
_, err := address.EncodeAddress(addr, blockchainName)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *ClaimHelper) LoadFromBytes(raw_claim []byte, blockchainName string) error {
|
||||||
|
if c.String() != "" {
|
||||||
return errors.New("already initialized")
|
return errors.New("already initialized")
|
||||||
}
|
}
|
||||||
if len(raw_claim) < 1 {
|
if len(raw_claim) < 1 {
|
||||||
|
@ -24,53 +47,53 @@ func (claim *ClaimHelper) LoadFromBytes(raw_claim []byte) (error) {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
*claim = ClaimHelper{claim_pb}
|
*c = ClaimHelper{claim_pb}
|
||||||
|
c.ValidateAddresses(blockchainName)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (claim *ClaimHelper) LoadFromHexString(claim_hex string) (error) {
|
func (c *ClaimHelper) LoadFromHexString(claim_hex string, blockchainName string) error {
|
||||||
buf, err := hex.DecodeString(claim_hex)
|
buf, err := hex.DecodeString(claim_hex)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
return claim.LoadFromBytes(buf)
|
return c.LoadFromBytes(buf, blockchainName)
|
||||||
}
|
}
|
||||||
|
|
||||||
func DecodeClaimBytes(serialized []byte) (*ClaimHelper, error) {
|
func DecodeClaimBytes(serialized []byte, blockchainName string) (*ClaimHelper, error) {
|
||||||
claim := &ClaimHelper{&pb.Claim{}}
|
claim := &ClaimHelper{&pb.Claim{}}
|
||||||
err := claim.LoadFromBytes(serialized)
|
err := claim.LoadFromBytes(serialized, blockchainName)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
return claim, nil
|
return claim, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func DecodeClaimHex(serialized string) (*ClaimHelper, error) {
|
func DecodeClaimHex(serialized string, blockchainName string) (*ClaimHelper, error) {
|
||||||
claim_bytes, err := hex.DecodeString(serialized)
|
claim_bytes, err := hex.DecodeString(serialized)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
return DecodeClaimBytes(claim_bytes)
|
return DecodeClaimBytes(claim_bytes, blockchainName)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *ClaimHelper) GetStream() *pb.Stream {
|
func (c *ClaimHelper) GetStream() *pb.Stream {
|
||||||
if m != nil {
|
if c != nil {
|
||||||
return m.Stream
|
return c.Stream
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *ClaimHelper) GetCertificate() *pb.Certificate {
|
func (c *ClaimHelper) GetCertificate() *pb.Certificate {
|
||||||
if m != nil {
|
if c != nil {
|
||||||
return m.Certificate
|
return c.Certificate
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *ClaimHelper) GetPublisherSignature() *pb.Signature {
|
func (c *ClaimHelper) GetPublisherSignature() *pb.Signature {
|
||||||
if m != nil {
|
if c != nil {
|
||||||
return m.PublisherSignature
|
return c.PublisherSignature
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,20 +1,19 @@
|
||||||
package claim
|
package claim
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"testing"
|
|
||||||
"encoding/hex"
|
"encoding/hex"
|
||||||
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
var raw_claims = []string{
|
var raw_claims = []string{
|
||||||
"08011002225e0801100322583056301006072a8648ce3d020106052b8104000a03420004d015365a40f3e5c03c87227168e5851f44659837bcf6a3398ae633bc37d04ee19baeb26dc888003bd728146dbea39f5344bf8c52cedaf1a3a1623a0166f4a367",
|
"08011002225e0801100322583056301006072a8648ce3d020106052b8104000a03420004d015365a40f3e5c03c87227168e5851f44659837bcf6a3398ae633bc37d04ee19baeb26dc888003bd728146dbea39f5344bf8c52cedaf1a3a1623a0166f4a367",
|
||||||
"080110011ad7010801128f01080410011a0c47616d65206f66206c696665221047616d65206f66206c696665206769662a0b4a6f686e20436f6e776179322e437265617469766520436f6d6d6f6e73204174747269627574696f6e20342e3020496e7465726e6174696f6e616c38004224080110011a195569c917f18bf5d2d67f1346aa467b218ba90cdbf2795676da250000803f4a0052005a001a41080110011a30b6adf6e2a62950407ea9fb045a96127b67d39088678d2f738c359894c88d95698075ee6203533d3c204330713aa7acaf2209696d6167652f6769662a5c080110031a40c73fe1be4f1743c2996102eec6ce0509e03744ab940c97d19ddb3b25596206367ab1a3d2583b16c04d2717eeb983ae8f84fee2a46621ffa5c4726b30174c6ff82214251305ca93d4dbedb50dceb282ebcb7b07b7ac65",
|
"080110011ad7010801128f01080410011a0c47616d65206f66206c696665221047616d65206f66206c696665206769662a0b4a6f686e20436f6e776179322e437265617469766520436f6d6d6f6e73204174747269627574696f6e20342e3020496e7465726e6174696f6e616c38004224080110011a195569c917f18bf5d2d67f1346aa467b218ba90cdbf2795676da250000803f4a0052005a001a41080110011a30b6adf6e2a62950407ea9fb045a96127b67d39088678d2f738c359894c88d95698075ee6203533d3c204330713aa7acaf2209696d6167652f6769662a5c080110031a40c73fe1be4f1743c2996102eec6ce0509e03744ab940c97d19ddb3b25596206367ab1a3d2583b16c04d2717eeb983ae8f84fee2a46621ffa5c4726b30174c6ff82214251305ca93d4dbedb50dceb282ebcb7b07b7ac65",
|
||||||
"080110011ad7010801128f01080410011a0c47616d65206f66206c696665221047616d65206f66206c696665206769662a0b4a6f686e20436f6e776179322e437265617469766520436f6d6d6f6e73204174747269627574696f6e20342e3020496e7465726e6174696f6e616c38004224080110011a195569c917f18bf5d2d67f1346aa467b218ba90cdbf2795676da250000803f4a0052005a001a41080110011a30b6adf6e2a62950407ea9fb045a96127b67d39088678d2f738c359894c88d95698075ee6203533d3c204330713aa7acaf2209696d6167652f676966",
|
"080110011ad7010801128f01080410011a0c47616d65206f66206c696665221047616d65206f66206c696665206769662a0b4a6f686e20436f6e776179322e437265617469766520436f6d6d6f6e73204174747269627574696f6e20342e3020496e7465726e6174696f6e616c38004224080110011a195569c917f18bf5d2d67f1346aa467b218ba90cdbf2795676da250000803f4a0052005a001a41080110011a30b6adf6e2a62950407ea9fb045a96127b67d39088678d2f738c359894c88d95698075ee6203533d3c204330713aa7acaf2209696d6167652f676966",
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
func TestDecodeClaims(t *testing.T) {
|
func TestDecodeClaims(t *testing.T) {
|
||||||
for _, claim_hex := range(raw_claims) {
|
for _, claim_hex := range raw_claims {
|
||||||
claim, err := DecodeClaimHex(claim_hex)
|
claim, err := DecodeClaimHex(claim_hex, "lbrycrd_main")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Error(err)
|
t.Error(err)
|
||||||
}
|
}
|
||||||
|
@ -30,7 +29,7 @@ func TestDecodeClaims(t *testing.T) {
|
||||||
|
|
||||||
func TestStripSignature(t *testing.T) {
|
func TestStripSignature(t *testing.T) {
|
||||||
claim_hex := raw_claims[1]
|
claim_hex := raw_claims[1]
|
||||||
claim, err := DecodeClaimHex(claim_hex)
|
claim, err := DecodeClaimHex(claim_hex, "lbrycrd_main")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Error(err)
|
t.Error(err)
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,16 +1,28 @@
|
||||||
package claim
|
package claim
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
"github.com/golang/protobuf/jsonpb"
|
"github.com/golang/protobuf/jsonpb"
|
||||||
)
|
)
|
||||||
|
|
||||||
func marshalToString(claim *ClaimHelper) (string, error) {
|
func marshalToString(c *ClaimHelper) (string, error) {
|
||||||
m_pb := &jsonpb.Marshaler{}
|
m_pb := &jsonpb.Marshaler{}
|
||||||
return m_pb.MarshalToString(claim)
|
return m_pb.MarshalToString(c)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (claim *ClaimHelper) RenderJSON() (string, error) {
|
func (c *ClaimHelper) RenderJSON() (string, error) {
|
||||||
return marshalToString(claim)
|
r, err := marshalToString(c)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println("err")
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
var dat map[string]interface{}
|
||||||
|
err = json.Unmarshal([]byte(r), &dat)
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
return r, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
//TODO: encode byte arrays with b58 for addresses and b16 for source hashes instead of the default of b64
|
//TODO: encode byte arrays with b58 for addresses and b16 for source hashes instead of the default of b64
|
|
@ -2,14 +2,14 @@ package claim
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"../address"
|
"../address"
|
||||||
|
"crypto/ecdsa"
|
||||||
"crypto/sha256"
|
"crypto/sha256"
|
||||||
"errors"
|
|
||||||
"encoding/asn1"
|
|
||||||
"crypto/x509/pkix"
|
"crypto/x509/pkix"
|
||||||
|
"encoding/asn1"
|
||||||
|
"encoding/hex"
|
||||||
|
"errors"
|
||||||
"github.com/btcsuite/btcd/btcec"
|
"github.com/btcsuite/btcd/btcec"
|
||||||
"math/big"
|
"math/big"
|
||||||
"crypto/ecdsa"
|
|
||||||
"encoding/hex"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type publicKeyInfo struct {
|
type publicKeyInfo struct {
|
||||||
|
@ -21,10 +21,16 @@ type publicKeyInfo struct {
|
||||||
const SECP256k1 = "SECP256k1"
|
const SECP256k1 = "SECP256k1"
|
||||||
|
|
||||||
func GetClaimSignatureDigest(claimAddress [25]byte, certificateId [20]byte, serializedNoSig []byte) [32]byte {
|
func GetClaimSignatureDigest(claimAddress [25]byte, certificateId [20]byte, serializedNoSig []byte) [32]byte {
|
||||||
combined := []byte{}
|
var combined []byte
|
||||||
for _, c := range claimAddress {combined = append(combined, c)}
|
for _, c := range claimAddress {
|
||||||
for _, c := range serializedNoSig {combined = append(combined, c)}
|
combined = append(combined, c)
|
||||||
for _, c := range certificateId {combined = append(combined, c)}
|
}
|
||||||
|
for _, c := range serializedNoSig {
|
||||||
|
combined = append(combined, c)
|
||||||
|
}
|
||||||
|
for _, c := range certificateId {
|
||||||
|
combined = append(combined, c)
|
||||||
|
}
|
||||||
digest := sha256.Sum256(combined)
|
digest := sha256.Sum256(combined)
|
||||||
return [32]byte(digest)
|
return [32]byte(digest)
|
||||||
}
|
}
|
||||||
|
@ -57,7 +63,7 @@ func (claim *ClaimHelper) VerifyDigest(certificate *ClaimHelper, signature [64]b
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
func (claim *ClaimHelper) ValidateClaimSignatureBytes(certificate *ClaimHelper, claimAddress [25]byte, certificateId [20]byte) (bool, error) {
|
func (claim *ClaimHelper) ValidateClaimSignatureBytes(certificate *ClaimHelper, claimAddress [25]byte, certificateId [20]byte, blockchainName string) (bool, error) {
|
||||||
signature := claim.GetPublisherSignature()
|
signature := claim.GetPublisherSignature()
|
||||||
if signature == nil {
|
if signature == nil {
|
||||||
return false, errors.New("claim does not have a signature")
|
return false, errors.New("claim does not have a signature")
|
||||||
|
@ -68,7 +74,7 @@ func (claim *ClaimHelper) ValidateClaimSignatureBytes(certificate *ClaimHelper,
|
||||||
signatureBytes[i] = signatureSlice[i]
|
signatureBytes[i] = signatureSlice[i]
|
||||||
}
|
}
|
||||||
|
|
||||||
claimAddress, err := address.ValidateAddress(claimAddress)
|
claimAddress, err := address.ValidateAddress(claimAddress, blockchainName)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return false, errors.New("invalid address")
|
return false, errors.New("invalid address")
|
||||||
}
|
}
|
||||||
|
@ -82,8 +88,8 @@ func (claim *ClaimHelper) ValidateClaimSignatureBytes(certificate *ClaimHelper,
|
||||||
return claim.VerifyDigest(certificate, signatureBytes, claimDigest), nil
|
return claim.VerifyDigest(certificate, signatureBytes, claimDigest), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (claim *ClaimHelper) ValidateClaimSignature(certificate *ClaimHelper, claimAddress string, certificateId string) (bool, error) {
|
func (claim *ClaimHelper) ValidateClaimSignature(certificate *ClaimHelper, claimAddress string, certificateId string, blockchainName string) (bool, error) {
|
||||||
addressBytes, err := address.DecodeAddress(claimAddress)
|
addressBytes, err := address.DecodeAddress(claimAddress, blockchainName)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return false, err
|
return false, err
|
||||||
}
|
}
|
||||||
|
@ -95,5 +101,5 @@ func (claim *ClaimHelper) ValidateClaimSignature(certificate *ClaimHelper, claim
|
||||||
for i := range certificateIdBytes {
|
for i := range certificateIdBytes {
|
||||||
certificateIdBytes[i] = certificateIdSlice[i]
|
certificateIdBytes[i] = certificateIdSlice[i]
|
||||||
}
|
}
|
||||||
return claim.ValidateClaimSignatureBytes(certificate, addressBytes, certificateIdBytes)
|
return claim.ValidateClaimSignatureBytes(certificate, addressBytes, certificateIdBytes, blockchainName)
|
||||||
}
|
}
|
||||||
|
|
|
@ -8,13 +8,13 @@ func TestValidateClaimSignature(t *testing.T) {
|
||||||
cert_claim_hex := "08011002225e0801100322583056301006072a8648ce3d020106052b8104000a03420004d015365a40f3e5c03c87227168e5851f44659837bcf6a3398ae633bc37d04ee19baeb26dc888003bd728146dbea39f5344bf8c52cedaf1a3a1623a0166f4a367"
|
cert_claim_hex := "08011002225e0801100322583056301006072a8648ce3d020106052b8104000a03420004d015365a40f3e5c03c87227168e5851f44659837bcf6a3398ae633bc37d04ee19baeb26dc888003bd728146dbea39f5344bf8c52cedaf1a3a1623a0166f4a367"
|
||||||
signed_claim_hex := "080110011ad7010801128f01080410011a0c47616d65206f66206c696665221047616d65206f66206c696665206769662a0b4a6f686e20436f6e776179322e437265617469766520436f6d6d6f6e73204174747269627574696f6e20342e3020496e7465726e6174696f6e616c38004224080110011a195569c917f18bf5d2d67f1346aa467b218ba90cdbf2795676da250000803f4a0052005a001a41080110011a30b6adf6e2a62950407ea9fb045a96127b67d39088678d2f738c359894c88d95698075ee6203533d3c204330713aa7acaf2209696d6167652f6769662a5c080110031a40c73fe1be4f1743c2996102eec6ce0509e03744ab940c97d19ddb3b25596206367ab1a3d2583b16c04d2717eeb983ae8f84fee2a46621ffa5c4726b30174c6ff82214251305ca93d4dbedb50dceb282ebcb7b07b7ac65"
|
signed_claim_hex := "080110011ad7010801128f01080410011a0c47616d65206f66206c696665221047616d65206f66206c696665206769662a0b4a6f686e20436f6e776179322e437265617469766520436f6d6d6f6e73204174747269627574696f6e20342e3020496e7465726e6174696f6e616c38004224080110011a195569c917f18bf5d2d67f1346aa467b218ba90cdbf2795676da250000803f4a0052005a001a41080110011a30b6adf6e2a62950407ea9fb045a96127b67d39088678d2f738c359894c88d95698075ee6203533d3c204330713aa7acaf2209696d6167652f6769662a5c080110031a40c73fe1be4f1743c2996102eec6ce0509e03744ab940c97d19ddb3b25596206367ab1a3d2583b16c04d2717eeb983ae8f84fee2a46621ffa5c4726b30174c6ff82214251305ca93d4dbedb50dceb282ebcb7b07b7ac65"
|
||||||
|
|
||||||
signed_claim, _ := DecodeClaimHex(signed_claim_hex)
|
signed_claim, _ := DecodeClaimHex(signed_claim_hex, "lbrycrd_main")
|
||||||
cert_claim, _ := DecodeClaimHex(cert_claim_hex)
|
cert_claim, _ := DecodeClaimHex(cert_claim_hex, "lbrycrd_main")
|
||||||
|
|
||||||
claim_addr := "bSkUov7HMWpYBiXackDwRnR5ishhGHvtJt"
|
claim_addr := "bSkUov7HMWpYBiXackDwRnR5ishhGHvtJt"
|
||||||
cert_id := "251305ca93d4dbedb50dceb282ebcb7b07b7ac65"
|
cert_id := "251305ca93d4dbedb50dceb282ebcb7b07b7ac65"
|
||||||
|
|
||||||
result, err := signed_claim.ValidateClaimSignature(cert_claim, claim_addr, cert_id)
|
result, err := signed_claim.ValidateClaimSignature(cert_claim, claim_addr, cert_id, "lbrycrd_main")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Error(err)
|
t.Error(err)
|
||||||
}
|
}
|
||||||
|
@ -27,13 +27,13 @@ func TestFailToValidateClaimSignature(t *testing.T) {
|
||||||
cert_claim_hex := "08011002225e0801100322583056301006072a8648ce3d020106052b8104000a03420004d015365a40f3e5c03c87227168e5851f44659837bcf6a3398ae633bc37d04ee19baeb26dc888003bd728146dbea39f5344bf8c52cedaf1a3a1623a0166f4a367"
|
cert_claim_hex := "08011002225e0801100322583056301006072a8648ce3d020106052b8104000a03420004d015365a40f3e5c03c87227168e5851f44659837bcf6a3398ae633bc37d04ee19baeb26dc888003bd728146dbea39f5344bf8c52cedaf1a3a1623a0166f4a367"
|
||||||
signed_claim_hex := "080110011ad7010801128f01080410011a0c47616d65206f66206c696665221047616d65206f66206c696665206769662a0b4a6f686e20436f6e776179322e437265617469766520436f6d6d6f6e73204174747269627574696f6e20342e3020496e7465726e6174696f6e616c38004224080110011a195569c917f18bf5d2d67f1346aa467b218ba90cdbf2795676da250000803f4a0052005a001a41080110011a30b6adf6e2a62950407ea9fb045a96127b67d39088678d2f738c359894c88d95698075ee6203533d3c204330713aa7acaf2209696d6167652f6769662a5c080110031a40c73fe1be4f1743c2996102eec6ce0509e03744ab940c97d19ddb3b25596206367ab1a3d2583b16c04d2717eeb983ae8f84fee2a46621ffa5c4726b30174c6ff82214251305ca93d4dbedb50dceb282ebcb7b07b7ac65"
|
signed_claim_hex := "080110011ad7010801128f01080410011a0c47616d65206f66206c696665221047616d65206f66206c696665206769662a0b4a6f686e20436f6e776179322e437265617469766520436f6d6d6f6e73204174747269627574696f6e20342e3020496e7465726e6174696f6e616c38004224080110011a195569c917f18bf5d2d67f1346aa467b218ba90cdbf2795676da250000803f4a0052005a001a41080110011a30b6adf6e2a62950407ea9fb045a96127b67d39088678d2f738c359894c88d95698075ee6203533d3c204330713aa7acaf2209696d6167652f6769662a5c080110031a40c73fe1be4f1743c2996102eec6ce0509e03744ab940c97d19ddb3b25596206367ab1a3d2583b16c04d2717eeb983ae8f84fee2a46621ffa5c4726b30174c6ff82214251305ca93d4dbedb50dceb282ebcb7b07b7ac65"
|
||||||
|
|
||||||
signed_claim, _ := DecodeClaimHex(signed_claim_hex)
|
signed_claim, _ := DecodeClaimHex(signed_claim_hex, "lbrycrd_main")
|
||||||
cert_claim, _ := DecodeClaimHex(cert_claim_hex)
|
cert_claim, _ := DecodeClaimHex(cert_claim_hex, "lbrycrd_main")
|
||||||
|
|
||||||
claim_addr := "bSkUov7HMWpYBiXackDwRnR5ishhGHvtJt"
|
claim_addr := "bSkUov7HMWpYBiXackDwRnR5ishhGHvtJt"
|
||||||
cert_id := "251305ca93d4dbedb50dceb282ebcb7b07b7ac64"
|
cert_id := "251305ca93d4dbedb50dceb282ebcb7b07b7ac64"
|
||||||
|
|
||||||
result, err := signed_claim.ValidateClaimSignature(cert_claim, claim_addr, cert_id)
|
result, err := signed_claim.ValidateClaimSignature(cert_claim, claim_addr, cert_id, "lbrycrd_main")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Error(err)
|
t.Error(err)
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,16 +1,16 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"os"
|
|
||||||
"../claim"
|
"../claim"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"os"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
args := os.Args[1:]
|
args := os.Args[1:]
|
||||||
if len(args) == 1 {
|
if len(args) == 1 {
|
||||||
claim_bytes := []byte(args[0])
|
claim_bytes := []byte(args[0])
|
||||||
decoded, err := claim.DecodeClaimBytes(claim_bytes)
|
decoded, err := claim.DecodeClaimBytes(claim_bytes, "lbrycrd_main")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println("Decoding error:", err)
|
fmt.Println("Decoding error:", err)
|
||||||
return
|
return
|
||||||
|
@ -24,7 +24,7 @@ func main() {
|
||||||
return
|
return
|
||||||
} else if (len(args) == 2) && (args[1] == "--decode_hex") {
|
} else if (len(args) == 2) && (args[1] == "--decode_hex") {
|
||||||
claim_hex := args[0]
|
claim_hex := args[0]
|
||||||
decoded, err := claim.DecodeClaimHex(claim_hex)
|
decoded, err := claim.DecodeClaimHex(claim_hex, "lbrycrd_main")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println("Decoding error:", err)
|
fmt.Println("Decoding error:", err)
|
||||||
return
|
return
|
||||||
|
|
Loading…
Reference in a new issue