From cd631325029bf8df53e2ec813a3d340a841f73e2 Mon Sep 17 00:00:00 2001 From: tom Date: Wed, 4 Jun 2014 23:08:35 -0400 Subject: compile issues, resolve later. commiting because switching machines --- src/crypto/crypto.h | 5 ++- src/crypto/electrum-words.cpp | 77 ++++++++++++++++++++++++++++++++++++++++++- src/crypto/electrum-words.h | 3 ++ 3 files changed, 83 insertions(+), 2 deletions(-) diff --git a/src/crypto/crypto.h b/src/crypto/crypto.h index 61641fbcf..1d92647f6 100644 --- a/src/crypto/crypto.h +++ b/src/crypto/crypto.h @@ -26,7 +26,10 @@ namespace crypto { }; POD_CLASS ec_scalar { - char data[32]; + // FIXME: temporary to get wallet recovery to work. Need to + // discuss proper solution with team + public: + char data[32]; }; POD_CLASS public_key: ec_point { diff --git a/src/crypto/electrum-words.cpp b/src/crypto/electrum-words.cpp index cb01c9203..9818791b9 100644 --- a/src/crypto/electrum-words.cpp +++ b/src/crypto/electrum-words.cpp @@ -6,6 +6,9 @@ #include #include +#include +#include +#include #include "crypto/crypto.h" // for declaration of crypto::secret_key #include "crypto/electrum-words.h" @@ -15,12 +18,84 @@ namespace crypto namespace ElectrumWords { + /* convert words to bytes, 3 words -> 4 bytes + * returns: + * false if not a multiple of 3 words, or if a words is not in the + * words list + * + * true otherwise + */ bool words_to_bytes(const std::string& words, crypto::secret_key& dst) { - return false; + int n = NUMWORDS; // hardcoded because this is what electrum uses + + std::vector wlist; + + boost::split(wlist, words, boost::is_any_of(" ")); + + // error on non-compliant word list + if (wlist.size() != 12 && wlist.size() != 24) return false; + + for (int i=0; i < wlist.size() / 3; i++) + { + uint32_t val; + uint32_t w1, w2, w3; + + // verify all three words exist in the word list + if (wordsMap.count(wlist[i*3]) == 0 || + wordsMap.count(wlist[i*3 + 1]) == 0 || + wordsMap.count(wlist[i*3 + 2]) == 0) + { + return false; + } + + w1 = wordsMap.at(wlist[i*3]); + w2 = wordsMap(wlist[i*3 + 1]); + w3 = wordsMap(wlist[i*3 + 2]); + + val = w1 + n * ((w2 - w1) % n) + n * n * ((w3 - w2) % n); + + memcpy(dst.data + i * 4, val, 4); // copy 4 bytes to position + } + + if (wlist.size() == 12) + { + memcpy(dst.data, dst.data + 16, 16); // if electrum 12-word seed, duplicate + } + + return true; } + + /* convert bytes to words, 4 bytes-> 3 words + * returns: + * false if wrong number of bytes (shouldn't be possible) + * true otherwise + */ bool bytes_to_words(const crypto::secret_key& src, std::string& words) { + int n = NUMWORDS; // hardcoded because this is what electrum uses + + if (sizeof(src.data) % 4 != 0) return false; + + // 8 bytes -> 3 words. 8 digits base 16 -> 3 digits base 1626 + for (int i=0; i < sizeof(src.data)/4; i++, words += ' ') + { + uint32_t w1, w2, w3; + + uint32_t val; + + memcpy(val, src, 4); + + w1 = val % n; + w2 = ((val / n) + w1) % n; + w3 = (((val / n) / n) + w2) % n; + + words += wordsArray[w1]; + words += ' '; + words += wordsArray[w2]; + words += ' '; + words += wordsArray[w3]; + } return false; } diff --git a/src/crypto/electrum-words.h b/src/crypto/electrum-words.h index 6b8291047..f5f8d1d95 100644 --- a/src/crypto/electrum-words.h +++ b/src/crypto/electrum-words.h @@ -12,6 +12,9 @@ namespace crypto { namespace ElectrumWords { + + const int NUMWORDS = 1626; + bool words_to_bytes(const std::string& words, crypto::secret_key& dst); bool bytes_to_words(const crypto::secret_key& src, std::string& words); -- cgit v1.2.3