|
| 1 | +package accounttype |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "crypto/ecdsa" |
| 6 | + "crypto/elliptic" |
| 7 | + "encoding/binary" |
| 8 | + "encoding/hex" |
| 9 | + "errors" |
| 10 | + "fmt" |
| 11 | + "math/big" |
| 12 | + |
| 13 | + "github.com/zoobc/zoobc-core/common/blocker" |
| 14 | + "github.com/zoobc/zoobc-core/common/constant" |
| 15 | + "github.com/zoobc/zoobc-core/common/model" |
| 16 | +) |
| 17 | + |
| 18 | +// EstoniaEidAccountType the default account type |
| 19 | +type EstoniaEidAccountType struct { |
| 20 | + publicKey, fullAddress []byte |
| 21 | +} |
| 22 | + |
| 23 | +func (acc *EstoniaEidAccountType) SetAccountPublicKey(accountPublicKey []byte) { |
| 24 | + if accountPublicKey == nil { |
| 25 | + acc.publicKey = make([]byte, 0) |
| 26 | + } |
| 27 | + acc.publicKey = accountPublicKey |
| 28 | +} |
| 29 | + |
| 30 | +func (acc *EstoniaEidAccountType) GetAccountAddress() ([]byte, error) { |
| 31 | + if acc.fullAddress != nil { |
| 32 | + return acc.fullAddress, nil |
| 33 | + } |
| 34 | + if acc.GetAccountPublicKey() == nil { |
| 35 | + return nil, errors.New("AccountAddressPublicKeyEmpty") |
| 36 | + } |
| 37 | + buff := bytes.NewBuffer([]byte{}) |
| 38 | + tmpBuf := make([]byte, 4) |
| 39 | + binary.LittleEndian.PutUint32(tmpBuf, uint32(acc.GetTypeInt())) |
| 40 | + buff.Write(tmpBuf) |
| 41 | + buff.Write(acc.GetAccountPublicKey()) |
| 42 | + acc.fullAddress = buff.Bytes() |
| 43 | + return acc.fullAddress, nil |
| 44 | +} |
| 45 | + |
| 46 | +func (acc *EstoniaEidAccountType) GetTypeInt() int32 { |
| 47 | + return int32(model.AccountType_EstoniaEidAccountType) |
| 48 | +} |
| 49 | + |
| 50 | +func (acc *EstoniaEidAccountType) GetAccountPublicKey() []byte { |
| 51 | + return acc.publicKey |
| 52 | +} |
| 53 | + |
| 54 | +func (acc *EstoniaEidAccountType) GetAccountPrefix() string { |
| 55 | + return "" |
| 56 | +} |
| 57 | + |
| 58 | +func (acc *EstoniaEidAccountType) GetName() string { |
| 59 | + return "EstoniaEid" |
| 60 | +} |
| 61 | + |
| 62 | +func (acc *EstoniaEidAccountType) GetAccountPublicKeyLength() uint32 { |
| 63 | + return 97 |
| 64 | +} |
| 65 | + |
| 66 | +func (acc *EstoniaEidAccountType) IsEqual(acc2 AccountTypeInterface) bool { |
| 67 | + return bytes.Equal(acc.GetAccountPublicKey(), acc2.GetAccountPublicKey()) && acc.GetTypeInt() == acc2.GetTypeInt() |
| 68 | +} |
| 69 | + |
| 70 | +func (acc *EstoniaEidAccountType) GetSignatureType() model.SignatureType { |
| 71 | + return model.SignatureType_EstoniaEidSignature |
| 72 | +} |
| 73 | + |
| 74 | +func (acc *EstoniaEidAccountType) GetSignatureLength() uint32 { |
| 75 | + return constant.EstoniaEidSignatureLength |
| 76 | +} |
| 77 | + |
| 78 | +func (acc *EstoniaEidAccountType) GetEncodedAddress() (string, error) { |
| 79 | + if acc.GetAccountPublicKey() == nil || bytes.Equal(acc.GetAccountPublicKey(), []byte{}) { |
| 80 | + return "", errors.New("EmptyAccountPublicKey") |
| 81 | + } |
| 82 | + return hex.EncodeToString(acc.GetAccountPublicKey()), nil |
| 83 | +} |
| 84 | + |
| 85 | +func (acc *EstoniaEidAccountType) DecodePublicKeyFromAddress(address string) ([]byte, error) { |
| 86 | + return hex.DecodeString(address) |
| 87 | +} |
| 88 | + |
| 89 | +func (acc *EstoniaEidAccountType) GenerateAccountFromSeed(seed string, optionalParams ...interface{}) error { |
| 90 | + return errors.New("NoImplementation") |
| 91 | +} |
| 92 | + |
| 93 | +func (acc *EstoniaEidAccountType) GetAccountPublicKeyString() (string, error) { |
| 94 | + return acc.GetEncodedAddress() |
| 95 | +} |
| 96 | + |
| 97 | +// GetAccountPrivateKey: we don't store neither generate private key of this account type as the private key resides in the eID |
| 98 | +func (acc *EstoniaEidAccountType) GetAccountPrivateKey() ([]byte, error) { |
| 99 | + return nil, nil |
| 100 | +} |
| 101 | + |
| 102 | +// Sign: this account type and signature comes only from Estonia eID and we don't have private key of the accounts |
| 103 | +// so we don't sign sign for this type of account. |
| 104 | +func (acc *EstoniaEidAccountType) Sign(payload []byte, seed string, optionalParams ...interface{}) ([]byte, error) { |
| 105 | + return []byte{}, nil |
| 106 | +} |
| 107 | + |
| 108 | +func (acc *EstoniaEidAccountType) VerifySignature(payload, signature, accountAddress []byte) error { |
| 109 | + publicKey := acc.loadPublicKeyFromDer(acc.GetAccountPublicKey()) |
| 110 | + r, s, _ := acc.decodeSignatureNIST384RS(signature) |
| 111 | + if !ecdsa.Verify(&publicKey, payload, r, s) { |
| 112 | + return blocker.NewBlocker( |
| 113 | + blocker.ValidationErr, |
| 114 | + "InvalidSignature", |
| 115 | + ) |
| 116 | + } |
| 117 | + return nil |
| 118 | +} |
| 119 | + |
| 120 | +// decodeSignatureNIST384RS: decode signature to R and S component |
| 121 | +// source: https://github.com/warner/python-ecdsa/blob/master/src/ecdsa/util.py (sigdecode_string) |
| 122 | +func (acc *EstoniaEidAccountType) decodeSignatureNIST384RS(signature []byte) (r, s *big.Int, err error) { |
| 123 | + // curveOrder "39402006196394479212279040100143613805079739270465446667946905279627659399113263569398956308152294913554433653942643" |
| 124 | + curveOrderLen := 48 |
| 125 | + if len(signature) != curveOrderLen*2 { |
| 126 | + return nil, nil, fmt.Errorf("error signature length: %d", len(signature)) |
| 127 | + } |
| 128 | + rBytes := signature[:48] |
| 129 | + sBytes := signature[48:] |
| 130 | + r = new(big.Int).SetBytes(rBytes) |
| 131 | + s = new(big.Int).SetBytes(sBytes) |
| 132 | + return r, s, nil |
| 133 | +} |
| 134 | + |
| 135 | +// loadPublicKeyFromDer loads public key from DER byte data |
| 136 | +func (acc *EstoniaEidAccountType) loadPublicKeyFromDer(publicKeyBytes []byte) (publicKey ecdsa.PublicKey) { |
| 137 | + curve := elliptic.P384() |
| 138 | + publicKey.Curve = curve |
| 139 | + publicKey.X, publicKey.Y = elliptic.Unmarshal(curve, publicKeyBytes) |
| 140 | + return publicKey |
| 141 | +} |
0 commit comments