Skip to content

Commit

Permalink
Adds checksum validation and required refactorings.
Browse files Browse the repository at this point in the history
  • Loading branch information
eMaringolo committed Mar 4, 2018
1 parent 031555c commit ddbbb39
Show file tree
Hide file tree
Showing 19 changed files with 82 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ https://github.com/bitcoin/bips/tree/master/bip-0039

I know my entropy and can generate the mnemonic words in my defined language.

You can instantiate me using the convenient class-side methods like:
You can instantiate me using the convenient class-side "instance-creation" methods like:

#fromBits:
#fromHexEntropy:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
instance creation
assertions
assertWordCount: count
((12 to: 24 by: 3) includes: count)
ifFalse: [ self
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
utilities
createChecksumFrom: hexString size: checksumSize
^ Integer
readFrom:
(((SHA256 hashMessage: (ByteArray fromHexString: hexString asLowercase)) asInteger
printStringBase: 2
length: 256
padded: true) copyFrom: 1 to: checksumSize)
base: 2
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
instance creation
utilities
defaultLanguage
^ 'english'
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
instance creation
utilities
detectLanguageOf: aString

self ensureAllLanguages.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,10 @@ fromHexEntropy: hexString
(hexString size \\ 4) isZero
ifFalse: [ self error: 'Entropy string has an invalid lenght.' ].
checksumSize := hexString size * 4 // 32.
checksum := Integer
readFrom:
(((SHA256 new hashMessage: (ByteArray fromHexString: hexString)) asInteger
printStringBase: 2
length: 256
padded: true) copyFrom: 1 to: checksumSize)
base: 2.
checksum := self createChecksumFrom: hexString size: checksumSize.
^ self
fromBits: ((Integer readFrom: hexString base: 16) bitShift: checksumSize) + checksum
size: hexString size * 4 + checksumSize / 11
fromBits:
((Integer readFrom: hexString base: 16) bitShift: checksumSize)
+ checksum
size: (hexString size * 4 + checksumSize) / 11
language: self defaultLanguage
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
instance creation
generate: entropyLength
"Answer a new instance of receiver with the default bit length.
WARNING: The entropy generation of this class is weak, so please avoid using it for critical purposes, randomness has not been tested."

^self fromBits: (1 to: (2 raisedTo: entropyLength)) atRandom
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
instance creation
generate
"Answer a new instance of receiver with the default bit length.
WARNING: The entropy generation of this class is weak, so please avoid using it for critical purposes, randomness has not been tested."

^self generate: 128
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
printing
bitsString
^ self bits printStringBase: 2 length: self size * 11 padded: true
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
accessing
checksum
| bitsString |
bitsString := self bitsString.
^Integer readFrom: (bitsString copyFrom: bitsString size - self checksumSize + 1 to: bitsString size) base: 2
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
accessing
checksumSize
^ self bitsString size // 33
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
accessing
entropy
| bitsString |
bitsString := self bitsString.
^Integer readFrom: (bitsString copyFrom: 1 to: bitsString size - self checksumSize) base: 2
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
testing
hasValidChecksum
^ self checksum
=
(self class
createChecksumFrom: (self entropy printStringBase: 16 length: (self bitsString size - self checksumSize) / 4 padded: true)
size: self checksumSize)
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ isValid
^false ].
(words allSatisfy: [:word | (self wordsIn: self language) includes: word ])
ifFalse: [ ^false ].
^true
^self hasValidChecksum
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"commentStamp" : "EstebanMaringolo 3/4/2018 10:07",
"commentStamp" : "EstebanMaringolo 3/4/2018 11:08",
"super" : "Object",
"category" : "BIP39Mnemonic-Core",
"classinstvars" : [
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
tests
testChecksumSize

| m |
m := BIP39Mnemonic fromWords: 'legal winner thank year wave sausage worth useful legal winner thank yellow'.
self assert: m checksumSize = 4.

m := BIP39Mnemonic fromWords: 'void come effort suffer camp survey warrior heavy shoot primary clutch crush open amazing screen patrol group space point ten exist slush involve unfold'.
self assert: m checksumSize = 8




Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
tests
testInvalidChecksum
| m |
m := BIP39Mnemonic fromWords: 'bless cloud wheel regular tiny venue bird web grief security dignity zoo'.
self deny: m isValid

Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
tests
testSymmetry

| m1 m2 |
m1 := BIP39Mnemonic fromWords: 'letter advice cage absurd amount doctor acoustic avoid letter advice cage absurd amount doctor acoustic avoid letter advice cage absurd amount doctor acoustic bless' language: 'english'.
m2 := BIP39Mnemonic fromBits: m1 bits size: m1 size language: m1 language.
self assert: m1 words equals: m2 words.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
tests
testWordsFromEntropy
testVectors
self vectors
do: [ :v |
| mnemonic |
Expand Down

0 comments on commit ddbbb39

Please sign in to comment.