bip39.cr

Build Status Code Coverage Documentation Release Language License

a native library implementing bip-0039 mnemonics purely for the crystal language.

this bip (0039) describes the implementation of a mnemonic code or mnemonic sentence -- a group of easy to remember words -- for the generation of deterministic wallets.

it consists of two parts: generating the mnemonic and converting it into a binary seed. this seed can be later used to generate deterministic wallets using bip-0032 or similar methods.

ref: bitcoin/bips/bip-0039

installation

add the bip39 library to your shard.yml

dependencies:
  bip39:
    github: q9f/bip39.cr
    version: "~> 0.1"

usage

# import bip39
require "bip39"

you can generate and recover mnemonics fully adhering to the bip-0039 specification.

m0 = Bip0039::Mnemonic.new
# => <Bip0039::Mnemonic:0x7f51769bcd20 @ent=128, @seed=183297182565288719506055787609377395053>

m0.to_words
# => ["measure", "come", "cube", "ostrich", "wide", "inspire", "hello", "essay", "ready", "cute", "reform", "sustain"]

m0.to_hex
# => "89e5c0d5ce7faaea9ab269b2c6d6d16d"

the default entropy is of 128 bits. this shard can generate seeds of of 128/160/192/224/256-bit entropy. just initialize mnemonics with the bit-size, e.g., Bip0039::Mnemonic.new 256.

it's easily possible to recover bip-0039 mnemonics from a phrase or a seed by simply passing it to the constructor.

m1 = Bip0039::Mnemonic.new ["measure", "come", "cube", "ostrich", "wide", "inspire", "hello", "essay", "ready", "cute", "reform", "sustain"]
# => <Bip0039::Mnemonic:0x7f37ca6e4c80 @ent=128, @seed=183297182565288719506055787609377395053>
m1.to_hex
# => "89e5c0d5ce7faaea9ab269b2c6d6d16d"

m2 = Bip0039::Mnemonic.new "89e5c0d5ce7faaea9ab269b2c6d6d16d"
# => <Bip0039::Mnemonic:0x7f37ca6e4be0 @ent=128, @seed=183297182565288719506055787609377395053>
m2.to_words
# => ["measure", "come", "cube", "ostrich", "wide", "inspire", "hello", "essay", "ready", "cute", "reform", "sustain"]

documentation

the full library documentation can be found here: q9f.github.io/bip39.cr

generate a local copy with:

crystal docs

testing

the library is entirely specified through tests in ./spec; run:

crystal spec --verbose

contribute

create a pull request, and make sure tests and linter passes.

license: apache license v2.0

contributors: @q9f