Skip to content

Commit

Permalink
handling ecdsa error
Browse files Browse the repository at this point in the history
  • Loading branch information
Daiki Muroga authored and Daiki Muroga committed Jun 14, 2022
1 parent a7f6989 commit 075ea32
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 15 deletions.
10 changes: 8 additions & 2 deletions internal/application/usecase/blockchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,14 @@ func (ba *blockchainApp) GetChain(ctx context.Context) (*result.Blockchain, erro

func (ba *blockchainApp) CreateTransactions(ctx context.Context, cmd command.TransactionCreate) error {
bc := ba.GetBlockchain()
publicKey := converter.PublicKeyFromString(*cmd.SenderPublicKey)
signature := model.NewSignature(*cmd.Signature)
publicKey, err := converter.PublicKeyFromString(*cmd.SenderPublicKey)
if err != nil {
return err
}
signature, err := model.NewSignature(*cmd.Signature)
if err != nil {
return err
}
isCreated := bc.CreateTransaction(*cmd.SenderBlockchainAddress, *cmd.RecipientBlockchainAddress, *cmd.Value, publicKey, signature)
if !isCreated {
return errors.New("ERROR: can't create transactions")
Expand Down
9 changes: 6 additions & 3 deletions internal/domain/model/signature.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,12 @@ type Signature struct {
S *big.Int
}

func NewSignature(signature string) *Signature {
x, y := converter.String2BigIntTuple(signature)
return &Signature{&x, &y}
func NewSignature(signature string) (*Signature, error) {
x, y, err := converter.String2BigIntTuple(signature)
if err != nil {
return nil, err
}
return &Signature{&x, &y}, nil
}

func (s *Signature) String() string {
Expand Down
32 changes: 22 additions & 10 deletions pkg/converter/ecdsa.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,37 @@ import (
"math/big"
)

func String2BigIntTuple(s string) (big.Int, big.Int) {
bx, _ := hex.DecodeString(s[:64])
by, _ := hex.DecodeString(s[64:])
func String2BigIntTuple(s string) (big.Int, big.Int, error) {
bx, err := hex.DecodeString(s[:64])
if err != nil {
return big.Int{}, big.Int{}, err
}
by, err := hex.DecodeString(s[64:])
if err != nil {
return big.Int{}, big.Int{}, err
}
var bix big.Int
var biy big.Int

_ = bix.SetBytes(bx)
_ = biy.SetBytes(by)
return bix, biy
return bix, biy, nil
}

func PublicKeyFromString(s string) *ecdsa.PublicKey {
x, y := String2BigIntTuple(s)
return &ecdsa.PublicKey{elliptic.P256(), &x, &y}
func PublicKeyFromString(s string) (*ecdsa.PublicKey, error) {
x, y, err := String2BigIntTuple(s)
if err != nil {
return nil, err
}
return &ecdsa.PublicKey{elliptic.P256(), &x, &y}, nil
}

func PrivateKeyFromString(s string, publicKey *ecdsa.PublicKey) *ecdsa.PrivateKey {
b, _ := hex.DecodeString(s[:])
func PrivateKeyFromString(s string, publicKey *ecdsa.PublicKey) (*ecdsa.PrivateKey, error) {
b, err := hex.DecodeString(s[:])
if err != nil {
return nil, err
}
var bi big.Int
_ = bi.SetBytes(b)
return &ecdsa.PrivateKey{*publicKey, &bi}
return &ecdsa.PrivateKey{*publicKey, &bi}, nil
}

0 comments on commit 075ea32

Please sign in to comment.