Skip to content

Commit

Permalink
accounts/keystore: Replace uuid library
Browse files Browse the repository at this point in the history
  • Loading branch information
blukat29 committed Apr 25, 2024
1 parent 9f6cf6a commit f1bc82b
Show file tree
Hide file tree
Showing 8 changed files with 71 additions and 30 deletions.
19 changes: 15 additions & 4 deletions accounts/keystore/key.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,10 @@ import (
"strings"
"time"

"github.com/google/uuid"
"github.com/klaytn/klaytn/accounts"
"github.com/klaytn/klaytn/common"
"github.com/klaytn/klaytn/crypto"
"github.com/pborman/uuid"
)

// Key represents a keystore storing private keys of an account.
Expand Down Expand Up @@ -133,7 +133,11 @@ func (k *KeyV3) UnmarshalJSON(j []byte) (err error) {
}

u := new(uuid.UUID)
*u = uuid.Parse(keyJSON.Id)
*u, err = uuid.Parse(keyJSON.Id)
if err != nil {
return err
}

k.Id = *u
addr, err := hex.DecodeString(keyJSON.Address)
if err != nil {
Expand Down Expand Up @@ -175,7 +179,10 @@ func (k *KeyV3) ResetPrivateKey() {
}

func newKeyFromECDSAWithAddress(privateKeyECDSA *ecdsa.PrivateKey, address common.Address) Key {
id := uuid.NewRandom()
id, err := uuid.NewRandom()
if err != nil {
panic(fmt.Sprintf("Could not create random uuid: %v", err))
}
key := &KeyV4{
Id: id,
Address: address,
Expand All @@ -185,7 +192,11 @@ func newKeyFromECDSAWithAddress(privateKeyECDSA *ecdsa.PrivateKey, address commo
}

func newKeyFromECDSA(privateKeyECDSA *ecdsa.PrivateKey) Key {
id := uuid.NewRandom()
id, err := uuid.NewRandom()
if err != nil {
panic(fmt.Sprintf("Could not create random uuid: %v", err))
}

key := &KeyV4{
Id: id,
Address: crypto.PubkeyToAddress(privateKeyECDSA.PublicKey),
Expand Down
13 changes: 9 additions & 4 deletions accounts/keystore/key2335.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,10 @@ import (
"encoding/hex"
"encoding/json"
"errors"
"fmt"

"github.com/google/uuid"
"github.com/klaytn/klaytn/crypto/bls"
"github.com/pborman/uuid"
keystorev4 "github.com/wealdtech/go-eth2-wallet-encryptor-keystorev4"
)

Expand All @@ -50,8 +51,12 @@ type encryptedKeyEIP2335JSON struct {

// NewKeyEIP2335 creates a new EIP-2335 keystore Key type using a BLS private key.
func NewKeyEIP2335(blsKey bls.SecretKey) *KeyEIP2335 {
id, err := uuid.NewRandom()
if err != nil {
panic(fmt.Sprintf("Could not create random uuid: %v", err))
}
return &KeyEIP2335{
ID: uuid.NewRandom(),
ID: id,
PublicKey: blsKey.PublicKey(),
SecretKey: blsKey,
}
Expand All @@ -64,8 +69,8 @@ func DecryptKeyEIP2335(keyJSON []byte, password string) (*KeyEIP2335, error) {
return nil, err
}

id := uuid.Parse(k.ID)
if id == nil {
id, err := uuid.Parse(k.ID)
if err != nil {
return nil, errors.New("Invalid UUID")
}

Expand Down
31 changes: 25 additions & 6 deletions accounts/keystore/keystore_passphrase.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,10 @@ import (
"os"
"path/filepath"

"github.com/google/uuid"
"github.com/klaytn/klaytn/common"
"github.com/klaytn/klaytn/common/math"
"github.com/klaytn/klaytn/crypto"
"github.com/pborman/uuid"
"golang.org/x/crypto/pbkdf2"
"golang.org/x/crypto/scrypt"
)
Expand Down Expand Up @@ -336,8 +336,13 @@ func DecryptKey(keyjson []byte, auth string) (Key, error) {
}
}

id, err := uuid.FromBytes(keyId)
if err != nil {
return nil, err
}

return &KeyV4{
Id: uuid.UUID(keyId),
Id: id,
Address: address,
PrivateKeys: privateKeys,
}, nil
Expand All @@ -348,9 +353,13 @@ func decryptKeyV4(keyProtected *encryptedKeyJSONV4, auth string) (keyBytes [][][
return nil, nil, fmt.Errorf("version not supported: %v (should be 4)", keyProtected.Version)
}

keyId = uuid.Parse(keyProtected.Id)
keyBytes = make([][][]byte, len(keyProtected.Keyring))
id, err := uuid.Parse(keyProtected.Id)
if err != nil {
return nil, nil, err
}
keyId = id[:]

keyBytes = make([][][]byte, len(keyProtected.Keyring))
for i, keys := range keyProtected.Keyring {
keyBytes[i] = make([][]byte, len(keys))
for j, key := range keys {
Expand All @@ -368,7 +377,12 @@ func decryptKeyV3(keyProtected *encryptedKeyJSONV3, auth string) (keyBytes []byt
if keyProtected.Version != 3 {
return nil, nil, fmt.Errorf("Version not supported: %v", keyProtected.Version)
}
keyId = uuid.Parse(keyProtected.Id)

id, err := uuid.Parse(keyProtected.Id)
if err != nil {
return nil, nil, err
}
keyId = id[:]

plainText, err := decryptKey(keyProtected.Crypto, auth)
if err != nil {
Expand Down Expand Up @@ -416,7 +430,12 @@ func decryptKey(cryptoJson cryptoJSON, auth string) (keyBytes []byte, err error)
}

func decryptKeyV1(keyProtected *encryptedKeyJSONV1, auth string) (keyBytes []byte, keyId []byte, err error) {
keyId = uuid.Parse(keyProtected.Id)
id, err := uuid.Parse(keyProtected.Id)
if err != nil {
return nil, nil, err
}
keyId = id[:]

mac, err := hex.DecodeString(keyProtected.Crypto.MAC)
if err != nil {
return nil, nil, err
Expand Down
15 changes: 11 additions & 4 deletions accounts/keystore/keystore_passphrase_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ import (
"os"
"testing"

"github.com/google/uuid"
"github.com/klaytn/klaytn/crypto"
"github.com/pborman/uuid"
"github.com/stretchr/testify/require"

"github.com/klaytn/klaytn/common"
Expand Down Expand Up @@ -75,8 +75,11 @@ func TestEncryptDecryptV4Single(t *testing.T) {

auth := string("password")

id, err := uuid.NewRandom()
require.NoError(t, err)

key := &KeyV4{
Id: uuid.NewRandom(),
Id: id,
Address: crypto.PubkeyToAddress(pk.PublicKey),
PrivateKeys: [][]*ecdsa.PrivateKey{{pk}},
}
Expand All @@ -99,10 +102,12 @@ func TestEncryptDecryptV4Multiple(t *testing.T) {
pks[i] = k
}

id, err := uuid.NewRandom()
require.NoError(t, err)
auth := "password"

key := &KeyV4{
Id: uuid.NewRandom(),
Id: id,
Address: crypto.PubkeyToAddress(pks[0].PublicKey),
PrivateKeys: [][]*ecdsa.PrivateKey{pks},
}
Expand All @@ -129,10 +134,12 @@ func TestEncryptDecryptV4RoleBased(t *testing.T) {
}
}

id, err := uuid.NewRandom()
require.NoError(t, err)
auth := "password"

key := &KeyV4{
Id: uuid.NewRandom(),
Id: id,
Address: crypto.PubkeyToAddress(pks[0][0].PublicKey),
PrivateKeys: pks,
}
Expand Down
7 changes: 3 additions & 4 deletions accounts/keystore/keystore_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,13 @@ import (
"testing"
"time"

"github.com/klaytn/klaytn/accounts"
"github.com/klaytn/klaytn/blockchain/types"
"github.com/klaytn/klaytn/common"
"github.com/klaytn/klaytn/crypto"
"github.com/klaytn/klaytn/event"
"github.com/klaytn/klaytn/params"
"github.com/stretchr/testify/assert"

"github.com/klaytn/klaytn/accounts"
"github.com/klaytn/klaytn/common"
"github.com/klaytn/klaytn/event"
)

var testSigData = make([]byte, 32)
Expand Down
7 changes: 5 additions & 2 deletions accounts/keystore/keyv4.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ import (
"encoding/hex"
"encoding/json"

"github.com/google/uuid"
"github.com/klaytn/klaytn/common"
"github.com/klaytn/klaytn/crypto"
"github.com/pborman/uuid"
)

type KeyV4 struct {
Expand Down Expand Up @@ -81,7 +81,10 @@ func (k *KeyV4) UnmarshalJSON(j []byte) (err error) {
return err
}

k.Id = uuid.Parse(keyJSON.Id)
k.Id, err = uuid.Parse(keyJSON.Id)
if err != nil {
return err
}

addr, err := hex.DecodeString(keyJSON.Address)
if err != nil {
Expand Down
3 changes: 1 addition & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ require (
github.com/newrelic/go-agent/v3 v3.11.0
github.com/otiai10/copy v1.0.1
github.com/pbnjay/memory v0.0.0-20190104145345-974d429e7ae4
github.com/pborman/uuid v0.0.0-20170612153648-e790cca94e6c
github.com/peterh/liner v1.1.1-0.20190123174540-a2c9a5303de7
github.com/pkg/errors v0.9.1
github.com/prometheus/client_golang v1.11.1
Expand Down Expand Up @@ -75,6 +74,7 @@ require (
require (
github.com/btcsuite/btcd/btcec/v2 v2.3.2
github.com/dop251/goja v0.0.0-20231014103939-873a1496dc8e
github.com/google/uuid v1.6.0
github.com/satori/go.uuid v1.2.0
github.com/tyler-smith/go-bip32 v1.0.0
github.com/wealdtech/go-eth2-wallet-encryptor-keystorev4 v1.4.1
Expand Down Expand Up @@ -109,7 +109,6 @@ require (
github.com/golang/glog v1.1.0 // indirect
github.com/google/go-cmp v0.5.9 // indirect
github.com/google/pprof v0.0.0-20230207041349-798e818bf904 // indirect
github.com/google/uuid v1.3.0 // indirect
github.com/hashicorp/go-uuid v1.0.2 // indirect
github.com/jcmturner/gofork v1.0.0 // indirect
github.com/jinzhu/inflection v1.0.0 // indirect
Expand Down
6 changes: 2 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -269,8 +269,8 @@ github.com/google/pprof v0.0.0-20230207041349-798e818bf904 h1:4/hN5RUoecvl+RmJRE
github.com/google/pprof v0.0.0-20230207041349-798e818bf904/go.mod h1:uglQLonpP8qtYCYyzA+8c/9qtqgA3qsXGYqCPKARAFg=
github.com/google/renameio v0.1.0/go.mod h1:KWCgfxg9yswjAJkECMjeO8J8rahYeXnNhOm40UhjYkI=
github.com/google/subcommands v1.2.0/go.mod h1:ZjhPrFU+Olkh9WazFPsl27BQ4UPiG37m3yTrtFlrHVk=
github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I=
github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/googleapis/gax-go/v2 v2.0.4/go.mod h1:0Wqv26UfaUD9n4G6kQubkQ+KchISgw+vpHVxEJEs9eg=
github.com/googleapis/gax-go/v2 v2.0.5/go.mod h1:DWXyrwAJ9X0FpwwEdw+IPEYBICEFu5mhpdKc/us6bOk=
github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1/go.mod h1:wJfORRmW1u3UXTncJ5qlYoELFm8eSnnEO6hX4iZ3EWY=
Expand Down Expand Up @@ -448,8 +448,6 @@ github.com/otiai10/mint v1.2.4/go.mod h1:d+b7n/0R3tdyUYYylALXpWQ/kTN+QobSq/4SRGB
github.com/paulbellamy/ratecounter v0.2.0/go.mod h1:Hfx1hDpSGoqxkVVpBi/IlYD7kChlfo5C6hzIHwPqfFE=
github.com/pbnjay/memory v0.0.0-20190104145345-974d429e7ae4 h1:MfIUBZ1bz7TgvQLVa/yPJZOGeKEgs6eTKUjz3zB4B+U=
github.com/pbnjay/memory v0.0.0-20190104145345-974d429e7ae4/go.mod h1:RMU2gJXhratVxBDTFeOdNhd540tG57lt9FIUV0YLvIQ=
github.com/pborman/uuid v0.0.0-20170612153648-e790cca94e6c h1:MUyE44mTvnI5A0xrxIxaMqoWFzPfQvtE2IWUollMDMs=
github.com/pborman/uuid v0.0.0-20170612153648-e790cca94e6c/go.mod h1:VyrYX9gd7irzKovcSS6BIIEwPRkP2Wm2m9ufcdFSJ34=
github.com/pelletier/go-toml v1.2.0/go.mod h1:5z9KED0ma1S8pY6P1sdut58dfprrGBbd/94hg7ilaic=
github.com/peterh/liner v1.0.1-0.20180619022028-8c1271fcf47f/go.mod h1:xIteQHvHuaLYG9IFj6mSxM0fCKrs34IrEQUhOYuGPHc=
github.com/peterh/liner v1.1.1-0.20190123174540-a2c9a5303de7 h1:oYW+YCJ1pachXTQmzR3rNLYGGz4g/UgFcjb28p/viDM=
Expand Down

0 comments on commit f1bc82b

Please sign in to comment.