github.com/n1ghtfa1l/go-vnt@v0.6.4-alpha.6/accounts/keystore/keystore_secure_enclave.go (about) 1 package keystore 2 3 import ( 4 "crypto/rand" 5 "fmt" 6 7 "encoding/json" 8 9 "github.com/vntchain/go-vnt/common" 10 "github.com/vntchain/go-vnt/crypto" 11 "github.com/vntchain/go-vnt/crypto/ecies" 12 ) 13 14 func Encrypt(msg []byte, pub []byte) ([]byte, error) { 15 ecdsapub, err := crypto.UnmarshalPubkey(pub) 16 if err != nil { 17 return nil, err 18 } 19 eciespub := ecies.ImportECDSAPublic(ecdsapub) 20 return ecies.Encrypt(rand.Reader, eciespub, msg, nil, nil) 21 } 22 23 func Decrypt(msg []byte, pri []byte) ([]byte, error) { 24 ecdaspri, err := crypto.ToECDSA(pri) 25 if err != nil { 26 return nil, err 27 } 28 eciespri := ecies.ImportECDSA(ecdaspri) 29 return eciespri.Decrypt(msg, nil, nil) 30 } 31 32 type keyStoreSecureEnclave struct { 33 vnt_keystore map[common.Address]encryptedKeyJSONV3 //keystore的集合 34 } 35 36 func (ks keyStoreSecureEnclave) GetKey(addr common.Address, filename, auth string) (*Key, error) { 37 // Load the key from the keystore and decrypt its contents 38 ksjson, err := json.Marshal(ks.vnt_keystore[addr]) 39 if err != nil { 40 return nil, err 41 } 42 key, err := DecryptKey(ksjson, auth) 43 if err != nil { 44 return nil, err 45 } 46 // Make sure we're really operating on the requested key (no swap attacks) 47 if key.Address != addr { 48 return nil, fmt.Errorf("key content mismatch: have account %x, want %x", key.Address, addr) 49 } 50 return key, nil 51 } 52 53 func (ks keyStoreSecureEnclave) StoreKey(filename string, key *Key, auth string) error { 54 // pub, err := crypto.UnmarshalPubkey(ks.publicKey) 55 // if err != nil { 56 // return err 57 // } 58 // eciespub := ecies.ImportECDSAPublic(pub) 59 // file, err := ecies.Encrypt(rand.Reader, eciespub, []byte(ks.keyJson), nil, nil) 60 // if err != nil { 61 // return err 62 // } 63 // return writeKeyFile(filename, file) 64 return nil 65 } 66 67 func (ks keyStoreSecureEnclave) JoinPath(filename string) string { 68 // if filepath.IsAbs(filename) { 69 // return filename 70 // } 71 // return filepath.Join(ks.keysDirPath, filename) 72 return "" 73 }