2019-07-24 18:59:15 +00:00
|
|
|
package generator
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/ecdsa"
|
2019-11-21 10:04:52 +00:00
|
|
|
"crypto/sha256"
|
2020-12-10 13:38:58 +00:00
|
|
|
"time"
|
2019-07-24 18:59:15 +00:00
|
|
|
|
2019-12-19 16:03:00 +00:00
|
|
|
"github.com/status-im/status-go/eth-node/crypto"
|
2019-11-23 17:57:05 +00:00
|
|
|
"github.com/status-im/status-go/eth-node/types"
|
2019-07-24 18:59:15 +00:00
|
|
|
"github.com/status-im/status-go/extkeys"
|
2020-12-10 13:38:58 +00:00
|
|
|
"github.com/status-im/status-go/multiaccounts"
|
2019-07-24 18:59:15 +00:00
|
|
|
)
|
|
|
|
|
2020-12-15 15:06:59 +00:00
|
|
|
type Account struct {
|
2019-07-24 18:59:15 +00:00
|
|
|
privateKey *ecdsa.PrivateKey
|
|
|
|
extendedKey *extkeys.ExtendedKey
|
|
|
|
}
|
|
|
|
|
2020-12-15 15:06:59 +00:00
|
|
|
func NewAccount(privateKey *ecdsa.PrivateKey, extKey *extkeys.ExtendedKey) Account {
|
|
|
|
if privateKey == nil {
|
|
|
|
privateKey = extKey.ToECDSA()
|
|
|
|
}
|
|
|
|
|
|
|
|
return Account{
|
|
|
|
privateKey: privateKey,
|
|
|
|
extendedKey: extKey,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *Account) ToAccountInfo() AccountInfo {
|
2022-10-28 09:30:00 +00:00
|
|
|
privateKeyHex := types.EncodeHex(crypto.FromECDSA(a.privateKey))
|
2019-11-23 17:57:05 +00:00
|
|
|
publicKeyHex := types.EncodeHex(crypto.FromECDSAPub(&a.privateKey.PublicKey))
|
2019-07-24 18:59:15 +00:00
|
|
|
addressHex := crypto.PubkeyToAddress(a.privateKey.PublicKey).Hex()
|
|
|
|
|
|
|
|
return AccountInfo{
|
2022-10-28 09:30:00 +00:00
|
|
|
PrivateKey: privateKeyHex,
|
|
|
|
PublicKey: publicKeyHex,
|
|
|
|
Address: addressHex,
|
2019-07-24 18:59:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-15 15:06:59 +00:00
|
|
|
func (a *Account) ToIdentifiedAccountInfo(id string) IdentifiedAccountInfo {
|
|
|
|
info := a.ToAccountInfo()
|
2019-11-21 10:04:52 +00:00
|
|
|
keyUID := sha256.Sum256(crypto.FromECDSAPub(&a.privateKey.PublicKey))
|
2019-11-23 17:57:05 +00:00
|
|
|
keyUIDHex := types.EncodeHex(keyUID[:])
|
2019-07-24 18:59:15 +00:00
|
|
|
return IdentifiedAccountInfo{
|
|
|
|
AccountInfo: info,
|
|
|
|
ID: id,
|
2019-11-21 10:04:52 +00:00
|
|
|
KeyUID: keyUIDHex,
|
2019-07-24 18:59:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-15 15:06:59 +00:00
|
|
|
func (a *Account) ToGeneratedAccountInfo(id string, mnemonic string) GeneratedAccountInfo {
|
|
|
|
idInfo := a.ToIdentifiedAccountInfo(id)
|
2019-07-24 18:59:15 +00:00
|
|
|
return GeneratedAccountInfo{
|
|
|
|
IdentifiedAccountInfo: idInfo,
|
|
|
|
Mnemonic: mnemonic,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// AccountInfo contains a PublicKey and an Address of an account.
|
|
|
|
type AccountInfo struct {
|
2022-10-28 09:30:00 +00:00
|
|
|
PrivateKey string `json:"privateKey"`
|
|
|
|
PublicKey string `json:"publicKey"`
|
|
|
|
Address string `json:"address"`
|
2019-07-24 18:59:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// IdentifiedAccountInfo contains AccountInfo and the ID of an account.
|
|
|
|
type IdentifiedAccountInfo struct {
|
|
|
|
AccountInfo
|
|
|
|
ID string `json:"id"`
|
2019-11-21 10:04:52 +00:00
|
|
|
// KeyUID is calculated as sha256 of the master public key and used for key
|
|
|
|
// identification. This is the only information available about the master
|
|
|
|
// key stored on a keycard before the card is paired.
|
|
|
|
// KeyUID name is chosen over KeyID in order to make it consistent with
|
|
|
|
// the name already used in Status and Keycard codebases.
|
|
|
|
KeyUID string `json:"keyUid"`
|
2019-07-24 18:59:15 +00:00
|
|
|
}
|
|
|
|
|
2020-12-15 15:06:59 +00:00
|
|
|
func (iai *IdentifiedAccountInfo) ToMultiAccount() *multiaccounts.Account {
|
|
|
|
return &multiaccounts.Account{
|
2020-12-15 18:00:31 +00:00
|
|
|
Timestamp: time.Now().Unix(),
|
|
|
|
KeyUID: iai.KeyUID,
|
2020-12-15 15:06:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-24 18:59:15 +00:00
|
|
|
// GeneratedAccountInfo contains IdentifiedAccountInfo and the mnemonic of an account.
|
|
|
|
type GeneratedAccountInfo struct {
|
|
|
|
IdentifiedAccountInfo
|
|
|
|
Mnemonic string `json:"mnemonic"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a GeneratedAccountInfo) toGeneratedAndDerived(derived map[string]AccountInfo) GeneratedAndDerivedAccountInfo {
|
|
|
|
return GeneratedAndDerivedAccountInfo{
|
|
|
|
GeneratedAccountInfo: a,
|
|
|
|
Derived: derived,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// GeneratedAndDerivedAccountInfo contains GeneratedAccountInfo and derived AccountInfo mapped by derivation path.
|
|
|
|
type GeneratedAndDerivedAccountInfo struct {
|
|
|
|
GeneratedAccountInfo
|
|
|
|
Derived map[string]AccountInfo `json:"derived"`
|
|
|
|
}
|