aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMaran <maran.hidskes@gmail.com>2014-04-09 23:06:30 +0800
committerMaran <maran.hidskes@gmail.com>2014-04-09 23:06:30 +0800
commit5714a82778c688a332f4b42bee28e99fb2b30e0b (patch)
tree13565e962eb3eb48094699e6514388fd2c121eb1
parent272b135b74931fd159d4e50a2328ea32a73f787c (diff)
downloaddexon-5714a82778c688a332f4b42bee28e99fb2b30e0b.tar
dexon-5714a82778c688a332f4b42bee28e99fb2b30e0b.tar.gz
dexon-5714a82778c688a332f4b42bee28e99fb2b30e0b.tar.bz2
dexon-5714a82778c688a332f4b42bee28e99fb2b30e0b.tar.lz
dexon-5714a82778c688a332f4b42bee28e99fb2b30e0b.tar.xz
dexon-5714a82778c688a332f4b42bee28e99fb2b30e0b.tar.zst
dexon-5714a82778c688a332f4b42bee28e99fb2b30e0b.zip
Small tweaks to mnemonic
-rw-r--r--ethutil/mnemonic.go106
1 files changed, 54 insertions, 52 deletions
diff --git a/ethutil/mnemonic.go b/ethutil/mnemonic.go
index cc58de84a..00f089e3b 100644
--- a/ethutil/mnemonic.go
+++ b/ethutil/mnemonic.go
@@ -5,6 +5,60 @@ import (
"strconv"
)
+// TODO: See if we can refactor this into a shared util lib if we need it multiple times
+func IndexOf(slice []string, value string) int64 {
+ for p, v := range slice {
+ if v == value {
+ return int64(p)
+ }
+ }
+ return -1
+}
+
+func MnemonicEncode(message string) []string {
+ var out []string
+ n := int64(len(words))
+
+ for i := 0; i < len(message); i += (len(message) / 8) {
+ x := message[i : i+8]
+ bit, _ := strconv.ParseInt(x, 16, 64)
+ w1 := (bit % n)
+ w2 := ((bit / n) + w1) % n
+ w3 := ((bit / n / n) + w2) % n
+ out = append(out, words[w1], words[w2], words[w3])
+ }
+ return out
+}
+
+func MnemonicDecode(wordsar []string) string {
+ var out string
+ n := int64(len(words))
+
+ for i := 0; i < len(wordsar); i += 3 {
+ word1 := wordsar[i]
+ word2 := wordsar[i+1]
+ word3 := wordsar[i+2]
+ w1 := IndexOf(words, word1)
+ w2 := IndexOf(words, word2)
+ w3 := IndexOf(words, word3)
+
+ y := (w2 - w1) % n
+ z := (w3 - w2) % n
+
+ // Golang handles modulo with negative numbers different then most languages
+ // The modulo can be negative, we don't want that.
+ if z < 0 {
+ z += n
+ }
+ if y < 0 {
+ y += n
+ }
+ x := w1 + n*(y) + n*n*(z)
+ out += fmt.Sprintf("%08x", x)
+ }
+ return out
+}
+
// Electrum word list
var words []string = []string{
"like",
@@ -1634,55 +1688,3 @@ var words []string = []string{
"weapon",
"weary",
}
-
-var n int64 = 1626
-
-func IndexOf(slice []string, value string) int64 {
- for p, v := range slice {
- if v == value {
- return int64(p)
- }
- }
- return -1
-}
-
-func MnemonicEncode(message string) []string {
- var out []string
-
- for i := 0; i < len(message); i += (len(message) / 8) {
- x := message[i : i+8]
- bit, _ := strconv.ParseInt(x, 16, 64)
- w1 := (bit % n)
- w2 := ((bit / n) + w1) % n
- w3 := ((bit / n / n) + w2) % n
- out = append(out, words[w1], words[w2], words[w3])
- }
- return out
-}
-
-func MnemonicDecode(wordsar []string) string {
- var out string
- for i := 0; i < len(wordsar); i += 3 {
- word1 := wordsar[i]
- word2 := wordsar[i+1]
- word3 := wordsar[i+2]
- w1 := IndexOf(words, word1)
- w2 := IndexOf(words, word2)
- w3 := IndexOf(words, word3)
-
- y := (w2 - w1) % n
- z := (w3 - w2) % n
-
- // Golang handles modulo with negative numbers different then most languages
- // The modulo can be negative, we don't want that.
- if z < 0 {
- z += n
- }
- if y < 0 {
- y += n
- }
- x := w1 + n*(y) + n*n*(z)
- out += fmt.Sprintf("%08x", x)
- }
- return out
-}