Example #1
0
func coreKeygen(auth, keyType string) ([]byte, error) {
	var keyStore crypto.KeyStore
	var err error

	logger.Infof("Generating new key. Type (%s). Encrypted (%v)\n", keyType, auth != "")

	if auth == "" {
		keyStore, err = newKeyStore()
		if err != nil {
			return nil, err
		}
	} else {
		keyStore = AccountManager.KeyStore()
	}

	var key *crypto.Key
	keyT, err := crypto.KeyTypeFromString(keyType)
	if err != nil {
		return nil, err
	}
	key, err = keyStore.GenerateNewKey(keyT, auth)
	if err != nil {
		return nil, fmt.Errorf("error generating key %s %s", keyType, err)
	}
	logger.Infof("Generated new key. Address (%x). Type (%s). Encrypted (%v)\n", key.Address, key.Type, auth != "")
	return key.Address, nil
}
Example #2
0
func coreImport(auth, keyType, theKey string) ([]byte, error) {
	var keyStore crypto.KeyStore
	var err error

	logger.Infof("Importing key. Type (%s). Encrypted (%v)\n", keyType, auth != "")

	if auth == "" {
		if keyStore, err = newKeyStore(); err != nil {
			return nil, err
		}
	} else {
		keyStore = AccountManager.KeyStore()
	}

	// TODO: unmarshal json and use auth to encrypt

	// if theKey is actually json, make sure
	// its a valid key, write to file
	if len(theKey) > 0 && theKey[:1] == "{" {
		keyJson := []byte(theKey)
		if addr := crypto.IsValidKeyJson(keyJson); addr != nil {
			return writeKey(KeysDir, addr, keyJson)
		} else {
			return nil, fmt.Errorf("invalid json key passed on command line")
		}
	}

	// else theKey is presumably a hex encoded private key
	keyBytes, err := hex.DecodeString(theKey)
	if err != nil {
		return nil, fmt.Errorf("private key is not a valid json or is invalid hex: %v", err)
	}

	keyT, err := crypto.KeyTypeFromString(keyType)
	if err != nil {
		return nil, err
	}
	key, err := crypto.NewKeyFromPriv(keyT, keyBytes)
	if err != nil {
		return nil, err
	}

	// store the new key
	if err = keyStore.StoreKey(key, auth); err != nil {
		return nil, err
	}

	return key.Address, nil
}