示例#1
0
func coreVerify(typ, pub, hash, sig string) (result bool, err error) {
	keyT, err := crypto.KeyTypeFromString(typ)
	if err != nil {
		return result, err
	}
	hashB, err := hex.DecodeString(hash)
	if err != nil {
		return result, fmt.Errorf("hash is invalid hex: %s", err.Error())
	}
	pubB, err := hex.DecodeString(pub)
	if err != nil {
		return result, fmt.Errorf("addr is invalid hex: %s", err.Error())
	}
	sigB, err := hex.DecodeString(sig)
	if err != nil {
		return result, fmt.Errorf("sig is invalid hex: %s", err.Error())
	}

	result, err = crypto.Verify(keyT.CurveType, hashB, sigB, pubB)
	if err != nil {
		return result, fmt.Errorf("error verifying signature %x for pubkey %x: %v", sigB, pubB, err)
	}

	return
}
示例#2
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
}
示例#3
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
}