base58 0.2.0 → 0.2.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG +3 -0
- data/MIT-LICENSE +1 -1
- data/README.md +15 -2
- data/VERSION +1 -1
- data/base58.gemspec +3 -3
- data/lib/base58.rb +17 -4
- data/test/test_base58.rb +141 -0
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 33ccbcef765a46e1e856bc882fc45b75b727de51da69e5bece5e097bc39d1dbd
|
4
|
+
data.tar.gz: 7eb54e225d04709b747c16b1d741b645d7c42b2911dd5da83a4111104b00b26e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d6cb74b302d8f367860fb85bfd0bdc842bb57064a2c852bd03e4fafe215fc850deb3efc4ae98ca21380c8f2939eb92f17886a4a29bcde37b77524292cdbd80fa
|
7
|
+
data.tar.gz: cb77134f325a5ed494727f1f2c5662e1332f57666ee824d74db8929ea7d04990823531b666c4cca3877633a30bc56b8ee75cf1b2d57a72c448c1ec268e57fd47
|
data/CHANGELOG
CHANGED
data/MIT-LICENSE
CHANGED
data/README.md
CHANGED
@@ -86,6 +86,8 @@ The alphabet used by the [Bitcoin](https://en.wikipedia.org/wiki/Bitcoin) protoc
|
|
86
86
|
|
87
87
|
123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz
|
88
88
|
|
89
|
+
See the "Leading Zeros" section for enabling full Bitcoin-style leading-zeros support.
|
90
|
+
|
89
91
|
|
90
92
|
### Ripple
|
91
93
|
|
@@ -96,6 +98,17 @@ The alphabet used by the [Ripple protocol](https://en.wikipedia.org/wiki/Ripple_
|
|
96
98
|
rpshnaf39wBUDNEGHJKLM4PQRST7VWXYZ2bcdeCg65jkm8oFqi1tuvAxyz
|
97
99
|
|
98
100
|
|
101
|
+
## Leading Zeroes
|
102
|
+
|
103
|
+
Some protocols, such as Bitcoin, require that leading zeros be encoded. Passing `true` to the third argument of binary_to_base58()` will enable this behaviour.
|
104
|
+
|
105
|
+
```ruby
|
106
|
+
bitcoin_address_hex = '00000000000000000000123456789ABCDEF0'
|
107
|
+
bitcoin_address_bin = [bitcoin_address_hex].pack('H*')
|
108
|
+
|
109
|
+
Base58.binary_to_base58(bitcoin_address_bin, :bitcoin, true) # => 111111111143c9JGph3DZ
|
110
|
+
```
|
111
|
+
|
99
112
|
## Contributing
|
100
113
|
|
101
114
|
Source repository is on [Github](https://github.com/dougal/base58), please file issues and pull requests there.
|
@@ -114,13 +127,13 @@ rake rdoc
|
|
114
127
|
|
115
128
|
## Credits
|
116
129
|
|
117
|
-
* [Joel Nordell](https://github.com/joelnordell) for Bitcoin
|
130
|
+
* [Joel Nordell](https://github.com/joelnordell) for various Bitcoin/Ripple support features.
|
118
131
|
* [Jim Myhrberg](https://github.com/jimeh) for argument checking and method aliases.
|
119
132
|
|
120
133
|
|
121
134
|
## Copyright and Licence
|
122
135
|
|
123
|
-
Copyright (c) 2009-2018, Douglas F Shearer.
|
136
|
+
Copyright (c) 2009 - 2018, Douglas F Shearer.
|
124
137
|
|
125
138
|
Base58 is licensed under the MIT Licence.
|
126
139
|
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.2.
|
1
|
+
0.2.1
|
data/base58.gemspec
CHANGED
@@ -2,16 +2,16 @@
|
|
2
2
|
# DO NOT EDIT THIS FILE DIRECTLY
|
3
3
|
# Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
|
4
4
|
# -*- encoding: utf-8 -*-
|
5
|
-
# stub: base58 0.2.
|
5
|
+
# stub: base58 0.2.1 ruby lib
|
6
6
|
|
7
7
|
Gem::Specification.new do |s|
|
8
8
|
s.name = "base58".freeze
|
9
|
-
s.version = "0.2.
|
9
|
+
s.version = "0.2.1"
|
10
10
|
|
11
11
|
s.required_rubygems_version = Gem::Requirement.new(">= 0".freeze) if s.respond_to? :required_rubygems_version=
|
12
12
|
s.require_paths = ["lib".freeze]
|
13
13
|
s.authors = ["Douglas F Shearer".freeze]
|
14
|
-
s.date = "2018-01-
|
14
|
+
s.date = "2018-01-08"
|
15
15
|
s.description = "Base58 is a Ruby library for converting ints or binaries to and from base58.".freeze
|
16
16
|
s.email = "dougal.s@gmail.com".freeze
|
17
17
|
s.extra_rdoc_files = [
|
data/lib/base58.rb
CHANGED
@@ -1,5 +1,5 @@
|
|
1
1
|
# Base58
|
2
|
-
# Copyright (c) 2009 -
|
2
|
+
# Copyright (c) 2009 - 2018 Douglas F Shearer.
|
3
3
|
# http://douglasfshearer.com
|
4
4
|
# Distributed under the MIT license as included with this plugin.
|
5
5
|
|
@@ -40,17 +40,30 @@ class Base58
|
|
40
40
|
end
|
41
41
|
|
42
42
|
# Converts a ASCII-8BIT (binary) encoded string to a base58 string.
|
43
|
-
def self.binary_to_base58(binary_val, alphabet = :flickr)
|
43
|
+
def self.binary_to_base58(binary_val, alphabet = :flickr, include_leading_zeroes = true)
|
44
44
|
raise ArgumentError, 'Value passed is not a String.' unless binary_val.is_a?(String)
|
45
45
|
raise ArgumentError, 'Value passed is not binary.' unless binary_val.encoding == Encoding::BINARY
|
46
46
|
raise ArgumentError, 'Invalid alphabet selection.' unless ALPHABETS.include?(alphabet)
|
47
|
-
int_to_base58(
|
47
|
+
return int_to_base58(0, alphabet) if binary_val.empty?
|
48
|
+
|
49
|
+
if include_leading_zeroes
|
50
|
+
nzeroes = binary_val.bytes.find_index{|b| b != 0} || binary_val.length-1
|
51
|
+
prefix = ALPHABETS[alphabet][0] * nzeroes
|
52
|
+
else
|
53
|
+
prefix = ''
|
54
|
+
end
|
55
|
+
|
56
|
+
prefix + int_to_base58(binary_val.bytes.inject{|a,b|(a<<8)+b}, alphabet)
|
48
57
|
end
|
49
58
|
|
50
59
|
# Converts a base58 string to an ASCII-8BIT (binary) encoded string.
|
60
|
+
# All leading zeroes in the base58 input are preserved and converted to
|
61
|
+
# "\x00" in the output.
|
51
62
|
def self.base58_to_binary(base58_val, alphabet = :flickr)
|
52
63
|
raise ArgumentError, 'Invalid alphabet selection.' unless ALPHABETS.include?(alphabet)
|
53
|
-
|
64
|
+
nzeroes = base58_val.chars.find_index{|c| c != ALPHABETS[alphabet][0]} || base58_val.length-1
|
65
|
+
prefix = nzeroes < 0 ? '' : '00' * nzeroes
|
66
|
+
[prefix + base58_to_int(base58_val, alphabet).to_s(16)].pack('H*')
|
54
67
|
end
|
55
68
|
|
56
69
|
class << self
|
data/test/test_base58.rb
CHANGED
@@ -551,6 +551,147 @@ EXAMPLES = {
|
|
551
551
|
}
|
552
552
|
}
|
553
553
|
|
554
|
+
LEADING_ZEROES_EXAMPLES = {
|
555
|
+
:bitcoin => [
|
556
|
+
{
|
557
|
+
:hex => "00000000000000000000123456789ABCDEF0",
|
558
|
+
:b58 => "111111111143c9JGph3DZ",
|
559
|
+
:b58_no_zeroes => "43c9JGph3DZ"
|
560
|
+
},
|
561
|
+
{
|
562
|
+
:hex => "00000000000000000000",
|
563
|
+
:b58 => "1111111111",
|
564
|
+
:b58_no_zeroes => "1"
|
565
|
+
},
|
566
|
+
{
|
567
|
+
:hex => "00",
|
568
|
+
:b58 => "1",
|
569
|
+
:b58_no_zeroes => "1"
|
570
|
+
},
|
571
|
+
{
|
572
|
+
:hex => "",
|
573
|
+
:b58 => "1",
|
574
|
+
:b58_no_zeroes => "1"
|
575
|
+
},
|
576
|
+
],
|
577
|
+
:flickr => [
|
578
|
+
{
|
579
|
+
:hex => "00000000000000000000123456789ABCDEF0",
|
580
|
+
:b58 => "111111111143B9igPG3dy",
|
581
|
+
:b58_no_zeroes => "43B9igPG3dy"
|
582
|
+
},
|
583
|
+
{
|
584
|
+
:hex => "00000000000000000000",
|
585
|
+
:b58 => "1111111111",
|
586
|
+
:b58_no_zeroes => "1"
|
587
|
+
},
|
588
|
+
{
|
589
|
+
:hex => "00",
|
590
|
+
:b58 => "1",
|
591
|
+
:b58_no_zeroes => "1"
|
592
|
+
},
|
593
|
+
{
|
594
|
+
:hex => "",
|
595
|
+
:b58 => "1",
|
596
|
+
:b58_no_zeroes => "1"
|
597
|
+
},
|
598
|
+
],
|
599
|
+
:ripple => [
|
600
|
+
{
|
601
|
+
:hex => "00000000000000000000123456789ABCDEF0",
|
602
|
+
:b58 => "rrrrrrrrrrhsc9JGF6sDZ",
|
603
|
+
:b58_no_zeroes => "hsc9JGF6sDZ"
|
604
|
+
},
|
605
|
+
{
|
606
|
+
:hex => "00000000000000000000",
|
607
|
+
:b58 => "rrrrrrrrrr",
|
608
|
+
:b58_no_zeroes => "r"
|
609
|
+
},
|
610
|
+
{
|
611
|
+
:hex => "00",
|
612
|
+
:b58 => "r",
|
613
|
+
:b58_no_zeroes => "r"
|
614
|
+
},
|
615
|
+
{
|
616
|
+
:hex => "",
|
617
|
+
:b58 => "r",
|
618
|
+
:b58_no_zeroes => "r"
|
619
|
+
}
|
620
|
+
]
|
621
|
+
}
|
622
|
+
|
623
|
+
HEX_TO_BIN = ->hex { [hex].pack('H*') }
|
624
|
+
HEX_TO_INT = ->hex { hex.to_i(16) }
|
625
|
+
|
626
|
+
STRIP_LEADING = ->char { ->str { str.gsub(/^#{char}*/, '') } }
|
627
|
+
STRIP_BINARY = STRIP_LEADING["\x00"]
|
628
|
+
|
629
|
+
LEADING_ZEROES_EXAMPLES.each do |alphabet, examples|
|
630
|
+
examples.each do |example|
|
631
|
+
define_method("test_binary_to_base58_leading_zeroes_#{alphabet}_#{example[:hex]}") do
|
632
|
+
bin = HEX_TO_BIN[example[:hex]]
|
633
|
+
bin_no_zeroes = STRIP_BINARY[bin]
|
634
|
+
|
635
|
+
assert_equal example[:b58], Base58.binary_to_base58(bin, alphabet) # Default behavior is to include leading zeroes
|
636
|
+
assert_equal example[:b58], Base58.binary_to_base58(bin, alphabet, true)
|
637
|
+
assert_equal example[:b58_no_zeroes], Base58.binary_to_base58(bin, alphabet, false) # Optionally, leading zeroes may be omitted
|
638
|
+
|
639
|
+
# If there are no zeroes in the input, there are never zeroes in the output
|
640
|
+
assert_equal example[:b58_no_zeroes], Base58.binary_to_base58(bin_no_zeroes, alphabet)
|
641
|
+
assert_equal example[:b58_no_zeroes], Base58.binary_to_base58(bin_no_zeroes, alphabet, true)
|
642
|
+
assert_equal example[:b58_no_zeroes], Base58.binary_to_base58(bin_no_zeroes, alphabet, false)
|
643
|
+
end
|
644
|
+
end
|
645
|
+
end
|
646
|
+
|
647
|
+
LEADING_ZEROES_EXAMPLES.each do |alphabet, examples|
|
648
|
+
examples.each do |example|
|
649
|
+
define_method("test_base58_to_binary_leading_zeroes_#{alphabet}_#{example[:hex]}") do
|
650
|
+
bin = HEX_TO_BIN[example[:hex]]
|
651
|
+
bin_no_zeroes = STRIP_BINARY[bin]
|
652
|
+
|
653
|
+
# When converting from base58, an empty string will never be produced.
|
654
|
+
bin_no_zeroes = "\x00" if bin_no_zeroes.empty?
|
655
|
+
bin = "\x00" if bin.empty?
|
656
|
+
|
657
|
+
assert_equal bin, Base58.base58_to_binary(example[:b58], alphabet)
|
658
|
+
assert_equal bin_no_zeroes, Base58.base58_to_binary(example[:b58_no_zeroes], alphabet)
|
659
|
+
end
|
660
|
+
end
|
661
|
+
end
|
662
|
+
|
663
|
+
LEADING_ZEROES_EXAMPLES.each do |alphabet, examples|
|
664
|
+
examples.each do |example|
|
665
|
+
define_method("test_int_to_base58_leading_zeroes_#{alphabet}_#{example[:hex]}") do
|
666
|
+
assert_equal example[:b58_no_zeroes], Base58.int_to_base58(HEX_TO_INT[example[:hex]], alphabet)
|
667
|
+
end
|
668
|
+
end
|
669
|
+
end
|
670
|
+
|
671
|
+
LEADING_ZEROES_EXAMPLES.each do |alphabet, examples|
|
672
|
+
examples.each do |example|
|
673
|
+
define_method("test_base58_to_int_leading_zeroes_#{alphabet}_#{example[:hex]}") do
|
674
|
+
assert_equal HEX_TO_INT[example[:hex]], Base58.base58_to_int(example[:b58], alphabet)
|
675
|
+
assert_equal HEX_TO_INT[example[:hex]], Base58.base58_to_int(example[:b58_no_zeroes], alphabet)
|
676
|
+
end
|
677
|
+
end
|
678
|
+
end
|
679
|
+
|
680
|
+
def test_base58_to_binary_empty_input
|
681
|
+
assert_equal "\x00", Base58.base58_to_binary("")
|
682
|
+
end
|
683
|
+
|
684
|
+
def test_base58_to_int_leading_zeroes_other_examples
|
685
|
+
# All other examples, when prepended with varying amounts of leading zeroes, should work properly.
|
686
|
+
EXAMPLES.each do |alphabet, examples|
|
687
|
+
examples.each do |base_58, expected|
|
688
|
+
(0..50).each do |n|
|
689
|
+
assert_equal expected, Base58.base58_to_int( (Base58::ALPHABETS[alphabet][0] * n) + base_58, alphabet )
|
690
|
+
end
|
691
|
+
end
|
692
|
+
end
|
693
|
+
end
|
694
|
+
|
554
695
|
def test_binary_to_base58_all_alphabets
|
555
696
|
BINARY_STRING_EXAMPLES.each do |alphabet, examples|
|
556
697
|
examples.each do |expected, integer|
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: base58
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.2.
|
4
|
+
version: 0.2.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Douglas F Shearer
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2018-01-
|
11
|
+
date: 2018-01-08 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description: Base58 is a Ruby library for converting ints or binaries to and from
|
14
14
|
base58.
|