Skip to content

Instantly share code, notes, and snippets.

@debreczeni
Last active October 24, 2015 09:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save debreczeni/7d04d03e3cccdd0d0170 to your computer and use it in GitHub Desktop.
Save debreczeni/7d04d03e3cccdd0d0170 to your computer and use it in GitHub Desktop.
Ruby implementation of HD Structure of SLIP-0013 Authentication using deterministic hierarchy
# lib/slip13.rb
# Ruby implementation of SLIP-0013 : Authentication using deterministic hierarchy
# https://doc.satoshilabs.com/slips/slip-0013.html
module SLIP13
class HDNode
def initialize(uri, index = 0)
@uri = uri
@index = index
end
def self.derive(uri, index = 0)
new(uri, index).derive
end
def derive
"m/#{hardened_paths.join("/")}"
end
private
def hardened_paths
unhardened_paths.map do |path|
0x80000000 | path
end
end
def unhardened_paths
[purpose] + digest.unpack('I8')[0..3]
end
def purpose
13
end
def digest
sha256 = Digest::SHA256.new
sha256 << [@index].pack('V')
sha256 << @uri
sha256.digest
end
end
end
# spec/lib/slip13_spec.rb
require 'spec_helper'
require_relative '../../lib/slip13'
describe SLIP13::HDNode do
context ".derive" do
it "Derives HD nodes according to SLIP13" do
test_vectors = [
{
index: 0,
uri: "https://satoshi@bitcoin.org/login",
path: "m/2147483661/2637750992/2845082444/3761103859/4005495825"
},
{
index: 3,
uri: "ftp://satoshi@bitcoin.org:2323/pub",
path: "m/2147483661/3098912377/2734671409/3632509519/3125730426"
},
{
index: 47,
uri: "ssh://satoshi@bitcoin.org",
path: "m/2147483661/4111640159/2980290904/2332131323/3701645358"
},
{
index: 0,
uri: "https://www.mrcoin.eu/callback/bitid",
path: "m/2147483661/2771203381/2356980894/2767891142/3030839793"
},
{
index: 1,
uri: "https://www.mrcoin.eu/callback/bitid",
path: "m/2147483661/3666892818/2293082814/2553532930/2457674449"
}
]
test_vectors.each do |test_vector|
expect(
SLIP13::HDNode.derive(test_vector[:uri], test_vector[:index])
).to eq(test_vector[:path])
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment