formatting and imports

This commit is contained in:
Jack Robison 2018-02-15 14:51:51 -05:00
parent cdaf3ac682
commit dd814b834b
No known key found for this signature in database
GPG key ID: DF25C68FE0239BB2
13 changed files with 75 additions and 71 deletions

View file

@ -5,7 +5,7 @@ import (
"math/big"
)
var b58_characters = [58]byte{0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x41, 0x42, 0x43, 0x44, 0x45,
var b58Characters = [58]byte{0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x41, 0x42, 0x43, 0x44, 0x45,
0x46, 0x47, 0x48, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x50, 0x51, 0x52, 0x53, 0x54, 0x55,
0x56, 0x57, 0x58, 0x59, 0x5a, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69,
0x6a, 0x6b, 0x6d, 0x6e, 0x6f, 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78,
@ -13,7 +13,7 @@ var b58_characters = [58]byte{0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x
func CharacterIndex(character byte) (*big.Int, error) {
for i := 0; i < 58; i++ {
if b58_characters[i] == character {
if b58Characters[i] == character {
return big.NewInt(int64(i)), nil
}
}

View file

@ -2,14 +2,14 @@ package base58
import "crypto/sha256"
const checksum_length = 4
const checksumLength = 4
func VerifyBase58Checksum(v []byte) bool {
checksum := [checksum_length]byte{}
checksum := [checksumLength]byte{}
for i := range checksum {
checksum[i] = v[len(v)-checksum_length+i]
checksum[i] = v[len(v)-checksumLength+i]
}
real_checksum := sha256.Sum256(v[:len(v)-checksum_length])
real_checksum := sha256.Sum256(v[:len(v)-checksumLength])
real_checksum = sha256.Sum256(real_checksum[:])
for i, c := range checksum {
if c != real_checksum[i] {

View file

@ -7,7 +7,7 @@ import (
func DecodeBase58(value string, size int64) ([]byte, error) {
buf := []byte(value)
long_value := big.NewInt(0)
longValue := big.NewInt(0)
result := make([]byte, size)
for i := int64(len(buf) - 1); i >= 0; i-- {
to_add := big.NewInt(0)
@ -17,11 +17,11 @@ func DecodeBase58(value string, size int64) ([]byte, error) {
return result, err
}
to_add = to_add.Mul(c, to_add)
long_value = long_value.Add(to_add, long_value)
longValue = longValue.Add(to_add, longValue)
}
for i := size - 1; i >= 0; i-- {
m := big.NewInt(0)
long_value, m = long_value.DivMod(long_value, big.NewInt(256), m)
longValue, m = longValue.DivMod(longValue, big.NewInt(256), m)
bs := m.Bytes()
if len(bs) == 0 {
bs = append(bs, 0x00)
@ -29,7 +29,7 @@ func DecodeBase58(value string, size int64) ([]byte, error) {
b := byte(bs[0])
result[i] = b
}
if long_value.Int64() != 0 {
if longValue.Int64() != 0 {
return result, errors.New("cannot decode to the given size")
}
if size != int64(len(result)) {

View file

@ -5,25 +5,25 @@ import (
)
func EncodeBase58(data []byte) string {
long_value := big.NewInt(0)
longValue := big.NewInt(0)
result := ""
for i := 0; i < len(data); i++ {
to_add := big.NewInt(0)
to_add = to_add.Exp(big.NewInt(256), big.NewInt(int64(i)), to_add)
to_add = to_add.Mul(big.NewInt(int64(data[24-i])), to_add)
long_value = long_value.Add(to_add, long_value)
longValue = longValue.Add(to_add, longValue)
}
i := 0
for {
m := big.NewInt(0)
long_value, m = long_value.DivMod(long_value, big.NewInt(58), m)
longValue, m = longValue.DivMod(longValue, big.NewInt(58), m)
bs := m.Bytes()
if len(bs) == 0 {
bs = append(bs, 0x00)
}
b := b58_characters[bs[0]]
b := b58Characters[bs[0]]
result = string(b) + result
if long_value.Int64() == 0 {
if longValue.Int64() == 0 {
break
}
i += 1

View file

@ -1,16 +1,16 @@
package address
import (
"./base58"
"errors"
"github.com/lbryio/lbryschema.go/address/base58"
)
func DecodeAddress(address string, blockchainName string) ([address_length]byte, error) {
decoded, err := base58.DecodeBase58(address, address_length)
func DecodeAddress(address string, blockchainName string) ([addressLength]byte, error) {
decoded, err := base58.DecodeBase58(address, addressLength)
if err != nil {
return [address_length]byte{}, errors.New("failed to decode")
return [addressLength]byte{}, errors.New("failed to decode")
}
buf := [address_length]byte{}
buf := [addressLength]byte{}
for i, b := range decoded {
buf[i] = b
}

View file

@ -1,10 +1,10 @@
package address
import (
"./base58"
"github.com/lbryio/lbryschema.go/address/base58"
)
func EncodeAddress(address [address_length]byte, blockchainName string) (string, error) {
func EncodeAddress(address [addressLength]byte, blockchainName string) (string, error) {
buf, err := ValidateAddress(address, blockchainName)
if err != nil {
return "", err

View file

@ -1,59 +1,59 @@
package address
import (
"./base58"
"errors"
"github.com/lbryio/lbryschema.go/address/base58"
)
const lbrycrd_main_pubkey_prefix = byte(85)
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 lbrycrdMainPubkeyPrefix = byte(85)
const lbrycrdMainScriptPrefix = byte(122)
const lbrycrdTestnetPubkeyPrefix = byte(111)
const lbrycrdTestnetScriptPrefix = byte(196)
const lbrycrdRegtestPubkeyPrefix = byte(111)
const lbrycrdRegtestScriptPrefix = byte(196)
const prefix_length = 1
const pubkey_length = 20
const checksum_length = 4
const address_length = prefix_length + pubkey_length + checksum_length
const lbrycrd_main = "lbrycrd_main"
const lbrycrd_testnet = "lbrycrd_testnet"
const lbrycrd_regtest = "lbrycrd_regtest"
const prefixLength = 1
const pubkeyLength = 20
const checksumLength = 4
const addressLength = prefixLength + pubkeyLength + checksumLength
const lbrycrdMain = "lbrycrd_main"
const lbrycrdTestnet = "lbrycrd_testnet"
const lbrycrdRegtest = "lbrycrd_regtest"
var address_prefixes = map[string][2]byte{}
var addressPrefixes = map[string][2]byte{}
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}
addressPrefixes[lbrycrdMain] = [2]byte{lbrycrdMainPubkeyPrefix, lbrycrdMainScriptPrefix}
addressPrefixes[lbrycrdTestnet] = [2]byte{lbrycrdTestnetPubkeyPrefix, lbrycrdTestnetScriptPrefix}
addressPrefixes[lbrycrdRegtest] = [2]byte{lbrycrdRegtestPubkeyPrefix, lbrycrdRegtestScriptPrefix}
}
func PrefixIsValid(address [address_length]byte, blockchainName string) bool {
func PrefixIsValid(address [addressLength]byte, blockchainName string) bool {
SetPrefixes()
prefix := address[0]
for _, addr_prefix := range address_prefixes[blockchainName] {
if addr_prefix == prefix {
for _, addrPrefix := range addressPrefixes[blockchainName] {
if addrPrefix == prefix {
return true
}
}
return false
}
func PubKeyIsValid(address [address_length]byte) bool {
pubkey := address[prefix_length : pubkey_length+prefix_length]
func PubKeyIsValid(address [addressLength]byte) bool {
pubkey := address[prefixLength : pubkeyLength+prefixLength]
// TODO: validate this for real
if len(pubkey) != pubkey_length {
if len(pubkey) != pubkeyLength {
return false
}
return true
}
func AddressChecksumIsValid(address [address_length]byte) bool {
func ChecksumIsValid(address [addressLength]byte) bool {
return base58.VerifyBase58Checksum(address[:])
}
func ValidateAddress(address [address_length]byte, blockchainName string) ([address_length]byte, error) {
if blockchainName != lbrycrd_main && blockchainName != lbrycrd_testnet && blockchainName != lbrycrd_regtest {
func ValidateAddress(address [addressLength]byte, blockchainName string) ([addressLength]byte, error) {
if blockchainName != lbrycrdMain && blockchainName != lbrycrdTestnet && blockchainName != lbrycrdRegtest {
return address, errors.New("invalid blockchain name")
}
if !PrefixIsValid(address, blockchainName) {
@ -62,7 +62,7 @@ func ValidateAddress(address [address_length]byte, blockchainName string) ([addr
if !PubKeyIsValid(address) {
return address, errors.New("invalid pubkey")
}
if !AddressChecksumIsValid(address) {
if !ChecksumIsValid(address) {
return address, errors.New("invalid address checksum")
}
return address, nil

View file

@ -1,10 +1,10 @@
package main
import (
"../address"
"../claim"
"C"
"encoding/hex"
"github.com/lbryio/lbryschema.go/address"
"github.com/lbryio/lbryschema.go/claim"
)
//export VerifySignature

View file

@ -1,8 +1,8 @@
package claim
import (
"../address"
"../pb"
"github.com/lbryio/lbryschema.go/address"
"github.com/lbryio/lbryschema.go/pb"
"encoding/hex"
"errors"
"github.com/golang/protobuf/jsonpb"

View file

@ -17,27 +17,28 @@ func TestDecodeClaims(t *testing.T) {
if err != nil {
t.Error(err)
}
serialized_hex, err := claim.SerializedHexString()
serializedHex, err := claim.SerializedHexString()
if err != nil {
t.Error(err)
}
if serialized_hex != claim_hex {
if serializedHex != claim_hex {
t.Error("failed to re-serialize")
}
}
}
func TestStripSignature(t *testing.T) {
claim_hex := raw_claims[1]
claim, err := DecodeClaimHex(claim_hex, "lbrycrd_main")
claimHex := raw_claims[1]
claim, err := DecodeClaimHex(claimHex, "lbrycrd_main")
if err != nil {
t.Error(err)
}
no_sig, err := claim.SerializedNoSignature()
noSig, err := claim.SerializedNoSignature()
if err != nil {
t.Error(err)
}
if hex.EncodeToString(no_sig) != raw_claims[2] {
if hex.EncodeToString(noSig) != raw_claims[2] {
t.Error("failed to remove signature")
}
}

View file

@ -1,7 +1,7 @@
package claim
import (
"../pb"
"github.com/lbryio/lbryschema.go/pb"
"encoding/hex"
"errors"
"github.com/golang/protobuf/proto"

View file

@ -1,7 +1,6 @@
package claim
import (
"../address"
"crypto/ecdsa"
"crypto/sha256"
"crypto/x509/pkix"
@ -9,6 +8,7 @@ import (
"encoding/hex"
"errors"
"github.com/btcsuite/btcd/btcec"
"github.com/lbryio/lbryschema.go/address"
"math/big"
)
@ -20,6 +20,9 @@ type publicKeyInfo struct {
const SECP256k1 = "SECP256k1"
//const NIST256p = "NIST256p"
//const NIST384p = "NIST384p"
func GetClaimSignatureDigest(claimAddress [25]byte, certificateId [20]byte, serializedNoSig []byte) [32]byte {
var combined []byte
for _, c := range claimAddress {
@ -39,8 +42,8 @@ func (c *ClaimHelper) GetCertificatePublicKey() (*btcec.PublicKey, error) {
derBytes := c.GetCertificate().GetPublicKey()
pub := publicKeyInfo{}
asn1.Unmarshal(derBytes, &pub)
pubkey_bytes := []byte(pub.PublicKey.Bytes)
p, err := btcec.ParsePubKey(pubkey_bytes, btcec.S256())
pubkeyBytes := []byte(pub.PublicKey.Bytes)
p, err := btcec.ParsePubKey(pubkeyBytes, btcec.S256())
if err != nil {
return &btcec.PublicKey{}, err
}
@ -48,7 +51,7 @@ func (c *ClaimHelper) GetCertificatePublicKey() (*btcec.PublicKey, error) {
}
func (c *ClaimHelper) VerifyDigest(certificate *ClaimHelper, signature [64]byte, digest [32]byte) bool {
public_key, err := certificate.GetCertificatePublicKey()
publicKey, err := certificate.GetCertificatePublicKey()
if err != nil {
return false
}
@ -58,7 +61,7 @@ func (c *ClaimHelper) VerifyDigest(certificate *ClaimHelper, signature [64]byte,
S := &big.Int{}
R.SetBytes(signature[0:32])
S.SetBytes(signature[32:64])
return ecdsa.Verify(public_key.ToECDSA(), digest[:], R, S)
return ecdsa.Verify(publicKey.ToECDSA(), digest[:], R, S)
}
return false
}

View file

@ -1,16 +1,16 @@
package main
import (
"../claim"
"fmt"
"github.com/lbryio/lbryschema.go/claim"
"os"
)
func main() {
args := os.Args[1:]
if len(args) == 1 {
claim_bytes := []byte(args[0])
decoded, err := claim.DecodeClaimBytes(claim_bytes, "lbrycrd_main")
claimBytes := []byte(args[0])
decoded, err := claim.DecodeClaimBytes(claimBytes, "lbrycrd_main")
if err != nil {
fmt.Println("Decoding error:", err)
return
@ -23,8 +23,8 @@ func main() {
fmt.Println(text)
return
} else if (len(args) == 2) && (args[1] == "--decode_hex") {
claim_hex := args[0]
decoded, err := claim.DecodeClaimHex(claim_hex, "lbrycrd_main")
claimHex := args[0]
decoded, err := claim.DecodeClaimHex(claimHex, "lbrycrd_main")
if err != nil {
fmt.Println("Decoding error:", err)
return