Exemplo n.º 1
0
    def test_vector(self):
        for test in TEST_VECT:
            lang = test["lang"]

            # Test mnemonic generator
            mnemonic = AlgorandMnemonicGenerator(lang).FromEntropy(
                binascii.unhexlify(test["entropy"]))

            self.assertEqual(test["mnemonic"], mnemonic.ToStr())
            self.assertEqual(test["mnemonic"], str(mnemonic))
            self.assertEqual(test["mnemonic"].split(" "), mnemonic.ToList())
            self.assertEqual(len(test["mnemonic"].split(" ")),
                             mnemonic.WordsCount())

            # Test mnemonic validator (language specified)
            mnemonic_validator = AlgorandMnemonicValidator(lang)
            self.assertTrue(mnemonic_validator.IsValid(mnemonic))
            # Test mnemonic validator (automatic language detection)
            mnemonic_validator = AlgorandMnemonicValidator()
            self.assertTrue(mnemonic_validator.IsValid(mnemonic))

            # Test decoder (language specified)
            entropy = AlgorandMnemonicDecoder(lang).Decode(mnemonic)
            self.assertEqual(test["entropy"], binascii.hexlify(entropy))
            # Test decoder (automatic language detection)
            entropy = AlgorandMnemonicDecoder().Decode(mnemonic)
            self.assertEqual(test["entropy"], binascii.hexlify(entropy))

            # Test seed generator (seed is the entropy itself for Algorand)
            seed = AlgorandSeedGenerator(mnemonic, lang).Generate()
            self.assertEqual(test["entropy"], binascii.hexlify(seed))

            # Test address
            bip44_ctx = Bip44.FromPrivateKey(seed, Bip44Coins.ALGORAND)
            self.assertEqual(test["address"],
                             bip44_ctx.PublicKey().ToAddress())
Exemplo n.º 2
0
"""Example of wallet creation using Algorand (same addresses of official wallet)."""

from bip_utils import (AlgorandWordsNum, AlgorandMnemonicGenerator,
                       AlgorandSeedGenerator, Bip44Coins, Bip44)

# Generate random mnemonic
mnemonic = AlgorandMnemonicGenerator().FromWordsNumber(
    AlgorandWordsNum.WORDS_NUM_25)
print(f"Mnemonic string: {mnemonic}")
# Generate seed from mnemonic
seed_bytes = AlgorandSeedGenerator(mnemonic).Generate()

# The seed is used as private key in the official wallet
bip44_ctx = Bip44.FromPrivateKey(seed_bytes, Bip44Coins.ALGORAND)

# Print keys
print(f"Algorand private key: {bip44_ctx.PrivateKey().Raw().ToHex()}")
print(f"Algorand public key: {bip44_ctx.PublicKey().RawCompressed().ToHex()}")
# Print address
print(f"Algorand address: {bip44_ctx.PublicKey().ToAddress()}")
Exemplo n.º 3
0
Example of key derivation for ed25519-based coins in the same way of Exodus wallet.

Basically, the Exodus wallet uses the secp256k1 curve to derive the complete BIP44 path for all coins, even if they are not based on the secp256k1 curve.
Then, for the coins based on the ed25519 curve (e.g. Algorand, Solana, Stellar, ...), it uses the last derived private key as a ed25519 master key to compute the public key and address.
It's not the only wallet doing this (Atomic Wallet does the same), because in this way the developers don't have to implement other derivation schemes beside secp256k1.
"""

from bip_utils import Bip39SeedGenerator, Bip32Secp256k1, Bip44Coins, Bip44ConfGetter, Bip44

# Coin that we want
coin_type = Bip44Coins.SOLANA

# Mnemonic
mnemonic = "abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon about"

# Generate seed from mnemonic
seed_bytes = Bip39SeedGenerator(mnemonic).Generate()

# Get coin index from configuration
coin_idx = Bip44ConfGetter.GetConfig(coin_type).CoinIndex()

# Derive the standard BIP44 path using secp256k1
bip32_ctx = Bip32Secp256k1.FromSeed(seed_bytes).DerivePath(
    f"m/44'/{coin_idx}'/0'/0/0")
priv_key_bytes = bip32_ctx.PrivateKey().Raw().ToBytes()

# Use the last private key as a ed25519 master key, we can use Bip44 to simplify the address computation
bip44_ctx = Bip44.FromPrivateKey(priv_key_bytes, coin_type)
# Same address of Exodus
print(f"Address: {bip44_ctx.PublicKey().ToAddress()}")
Exemplo n.º 4
0
"""
Example of wallet creation in the same way of Solana-CLI.

Solana-CLI works differently from other wallets.
First of all, it's not a HD-wallet but it only generates a master key without deriving any child key.
Secondly, the master private key is just the first 32-byte of the seed generated from the mnemonic, it's not computing
the HMAC-SHA512 like BIP32.
"""

from bip_utils import (Bip39WordsNum, Bip39MnemonicGenerator,
                       Bip39SeedGenerator, Bip44Coins, Bip44)

# Generate random mnemonic
mnemonic = Bip39MnemonicGenerator().FromWordsNumber(Bip39WordsNum.WORDS_NUM_12)
print(f"Mnemonic string: {mnemonic}")
# Generate seed from mnemonic
seed_bytes = Bip39SeedGenerator(mnemonic).Generate()

# The first 32-byte of the seed is the private key
bip44_ctx = Bip44.FromPrivateKey(seed_bytes[:32], Bip44Coins.SOLANA)
# Same address of Solana-CLI
print(f"Address: {bip44_ctx.PublicKey().ToAddress()}")
# Print master key, no need to derive any child key
print(f"Public key: {bip44_ctx.PublicKey().RawCompressed().ToHex()}")
print(f"Private key: {bip44_ctx.PrivateKey().Raw().ToHex()}")