Skip to content

Commit 375c657

Browse files
author
alex v
authored
Mnemonic support (#400)
* added donate button * added a donate feature to the send page * Update moc_cfund_voting.cpp * update byteswap.h from bitcoin core * mnemonic support * remove unnecesary files * add missing init help * fix inc * fix link order * adds other languages and minor tweaks to help text (#55) * adds test (#56) * Revert "added donate button" This reverts commit 70f41f2. * Revert "Update moc_cfund_voting.cpp" This reverts commit 9d56a55. * Revert "Revert "Update moc_cfund_voting.cpp"" This reverts commit 1bdd883.
1 parent a25b139 commit 375c657

29 files changed

+22469
-48
lines changed

qa/pull-tester/rpc-tests.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@
173173
'hardfork-451.py',
174174
'hardfork-452.py',
175175
'staticr-tx-send.py',
176-
176+
'mnemonic.py',
177177
'sendtoaddress.py',
178178
'stakeimmaturebalance.py',
179179
'rpc-help.py',

qa/rpc-tests/mnemonic.py

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
#!/usr/bin/env python3
2+
# Copyright (c) 2019 Navcoin Core developers
3+
# Distributed under the MIT software license, see the accompanying
4+
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
5+
6+
from test_framework.test_framework import NavCoinTestFramework
7+
from test_framework.util import (
8+
start_node,
9+
assert_equal,
10+
slow_gen,
11+
)
12+
import os
13+
14+
class MnemonicTest(NavCoinTestFramework):
15+
16+
def __init__(self):
17+
super().__init__()
18+
self.setup_clean_chain = True
19+
self.num_nodes = 1
20+
self.languages = ["english", "spanish", "italian", "japanese",\
21+
"french", "russian", "czech", "ukrainian",\
22+
"simplified chinese", "traditional chinese"]
23+
self.mnemonics = {}
24+
25+
def setup_network(self):
26+
self.nodes = []
27+
self.nodes.append(start_node(0, self.options.tmpdir, []))
28+
self.is_network_split = False
29+
30+
def run_test (self):
31+
# Record masterkeyid in base58
32+
masterkeyid = self.nodes[0].dumpmasterprivkey()
33+
34+
# Record default mnemonic in English
35+
mnemonic_eng = self.nodes[0].dumpmnemonic()
36+
37+
# Record mnemonics in other languages
38+
for language in self.languages:
39+
self.mnemonics[language] = self.nodes[0].dumpmnemonic(language)
40+
41+
print("Restoring from mnemonic ...")
42+
self.check_mnemonic_works(masterkeyid, mnemonic_eng)
43+
44+
for language in self.languages:
45+
self.check_mnemonic_works(masterkeyid, self.mnemonics[language], language)
46+
47+
def check_mnemonic_works(self, masterprivkey, mnemonic, language="english"):
48+
self.stop_node(0)
49+
os.remove(self.options.tmpdir + "/node0/devnet/wallet.dat")
50+
51+
self.nodes[0] = start_node(0, self.options.tmpdir, ["-importmnemonic=" + mnemonic, "-mnemoniclanguage=" + language])
52+
assert_equal(masterprivkey, self.nodes[0].dumpmasterprivkey())
53+
54+
if __name__ == '__main__':
55+
MnemonicTest().main ()

src/Makefile.am

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,8 @@ NAVCOIN_CORE_H = \
156156
wallet/rpcwallet.h \
157157
wallet/wallet.h \
158158
wallet/walletdb.h \
159+
mnemonic/dictionary.h \
160+
mnemonic/mnemonic.h \
159161
zmq/zmqabstractnotifier.h \
160162
zmq/zmqconfig.h\
161163
zmq/zmqnotificationinterface.h \
@@ -229,6 +231,8 @@ libnavcoin_wallet_a_CPPFLAGS = $(AM_CPPFLAGS) $(NAVCOIN_INCLUDES)
229231
libnavcoin_wallet_a_CXXFLAGS = $(AM_CXXFLAGS) $(PIE_FLAGS)
230232
libnavcoin_wallet_a_SOURCES = \
231233
consensus/cfund.cpp \
234+
mnemonic/dictionary.cpp \
235+
mnemonic/mnemonic.cpp \
232236
wallet/crypter.cpp \
233237
wallet/db.cpp \
234238
wallet/navtech.cpp \
@@ -252,6 +256,18 @@ crypto_libnavcoin_crypto_a_SOURCES = \
252256
crypto/hmac_sha512.h \
253257
crypto/ripemd160.cpp \
254258
crypto/ripemd160.h \
259+
crypto/external/hmac_sha256.c \
260+
crypto/external/hmac_sha256.h \
261+
crypto/external/hmac_sha512.c \
262+
crypto/external/hmac_sha512.h \
263+
crypto/external/pkcs5_pbkdf2.c \
264+
crypto/external/pkcs5_pbkdf2.h \
265+
crypto/external/sha256.c \
266+
crypto/external/sha256.h \
267+
crypto/external/sha512.c \
268+
crypto/external/sha512.h \
269+
crypto/external/zeroize.c \
270+
crypto/external/zeroize.h \
255271
crypto/sha1.cpp \
256272
crypto/sha1.h \
257273
crypto/sha256.cpp \

src/Makefile.test.include

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -149,14 +149,14 @@ NAVCOIN_TESTS =\
149149

150150
test_test_navcoin_SOURCES = $(NAVCOIN_TESTS) $(JSON_TEST_FILES) $(RAW_TEST_FILES)
151151
test_test_navcoin_CPPFLAGS = $(AM_CPPFLAGS) $(NAVCOIN_INCLUDES) -I$(builddir)/test/ $(TESTDEFS)
152-
test_test_navcoin_LDADD = $(LIBNAVCOIN_SERVER) $(LIBNAVCOIN_CLI) $(LIBNAVCOIN_COMMON) $(LIBNAVCOIN_UTIL) $(LIBNAVCOIN_CONSENSUS) $(LIBNAVCOIN_CRYPTO) $(LIBUNIVALUE) $(LIBLEVELDB) $(LIBMEMENV) \
152+
test_test_navcoin_LDADD = $(LIBNAVCOIN_SERVER) $(LIBNAVCOIN_CLI) $(LIBNAVCOIN_COMMON) $(LIBNAVCOIN_UTIL) $(LIBNAVCOIN_CONSENSUS) $(LIBUNIVALUE) $(LIBLEVELDB) $(LIBMEMENV) \
153153
$(BOOST_LIBS) $(BOOST_UNIT_TEST_FRAMEWORK_LIB) $(LIBSECP256K1)
154154
test_test_navcoin_CXXFLAGS = $(AM_CXXFLAGS) $(PIE_FLAGS)
155155
if ENABLE_WALLET
156156
test_test_navcoin_LDADD += $(LIBNAVCOIN_WALLET)
157157
endif
158158

159-
test_test_navcoin_LDADD += $(LIBNAVCOIN_CONSENSUS) $(BDB_LIBS) $(SSL_LIBS) $(UNBOUND_LIBS) $(CURL_LIBS) $(CRYPTO_LIBS) $(MINIUPNPC_LIBS) $(LIBNAVCOIN_ZMQ) $(EVENT_LIBS) $(EVENT_PTHREADS_LIBS)
159+
test_test_navcoin_LDADD += $(LIBNAVCOIN_CRYPTO) $(LIBNAVCOIN_CONSENSUS) $(BDB_LIBS) $(SSL_LIBS) $(UNBOUND_LIBS) $(CURL_LIBS) $(CRYPTO_LIBS) $(MINIUPNPC_LIBS) $(LIBNAVCOIN_ZMQ) $(EVENT_LIBS) $(EVENT_PTHREADS_LIBS)
160160
test_test_navcoin_LDFLAGS = $(RELDFLAGS) $(AM_LDFLAGS) $(LIBTOOL_APP_LDFLAGS) -static
161161

162162
if ENABLE_ZMQ

src/compat/byteswap.h

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,47 @@
1-
// Copyright (c) 2014 The Bitcoin developers
1+
// Copyright (c) 2014-2018 The Bitcoin Core developers
22
// Distributed under the MIT software license, see the accompanying
33
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
44

5-
#ifndef NAVCOIN_COMPAT_BYTESWAP_H
6-
#define NAVCOIN_COMPAT_BYTESWAP_H
7-
8-
#if defined(HAVE_CONFIG_H)
9-
#include "config/navcoin-config.h"
10-
#endif
5+
#ifndef BITCOIN_COMPAT_BYTESWAP_H
6+
#define BITCOIN_COMPAT_BYTESWAP_H
117

128
#include <stdint.h>
139

1410
#if defined(HAVE_BYTESWAP_H)
1511
#include <byteswap.h>
1612
#endif
1713

14+
#if defined(MAC_OSX)
15+
16+
#if !defined(bswap_16)
17+
18+
// Mac OS X / Darwin features; we include a check for bswap_16 because if it is already defined, protobuf has
19+
// defined these macros for us already; if it isn't, we do it ourselves. In either case, we get the exact same
20+
// result regardless which path was taken
21+
#include <libkern/OSByteOrder.h>
22+
#define bswap_16(x) OSSwapInt16(x)
23+
#define bswap_32(x) OSSwapInt32(x)
24+
#define bswap_64(x) OSSwapInt64(x)
25+
26+
#endif // !defined(bswap_16)
27+
28+
#else
29+
// Non-Mac OS X / non-Darwin
30+
1831
#if HAVE_DECL_BSWAP_16 == 0
1932
inline uint16_t bswap_16(uint16_t x)
2033
{
21-
return (x >> 8) | ((x & 0x00ff) << 8);
34+
return (x >> 8) | (x << 8);
2235
}
23-
#endif // HAVE_DECL_BSWAP16
36+
#endif // HAVE_DECL_BSWAP16 == 0
2437

2538
#if HAVE_DECL_BSWAP_32 == 0
2639
inline uint32_t bswap_32(uint32_t x)
2740
{
2841
return (((x & 0xff000000U) >> 24) | ((x & 0x00ff0000U) >> 8) |
2942
((x & 0x0000ff00U) << 8) | ((x & 0x000000ffU) << 24));
3043
}
31-
#endif // HAVE_DECL_BSWAP32
44+
#endif // HAVE_DECL_BSWAP32 == 0
3245

3346
#if HAVE_DECL_BSWAP_64 == 0
3447
inline uint64_t bswap_64(uint64_t x)
@@ -42,6 +55,8 @@ inline uint64_t bswap_64(uint64_t x)
4255
| ((x & 0x000000000000ff00ull) << 40)
4356
| ((x & 0x00000000000000ffull) << 56));
4457
}
45-
#endif // HAVE_DECL_BSWAP64
58+
#endif // HAVE_DECL_BSWAP64 == 0
59+
60+
#endif // defined(MAC_OSX)
4661

47-
#endif // NAVCOIN_COMPAT_BYTESWAP_H
62+
#endif // BITCOIN_COMPAT_BYTESWAP_H

src/crypto/external/hmac_sha256.c

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
/* libsodium: hmac_hmacsha256.c, v0.4.5 2014/04/16 */
2+
/**
3+
* Copyright 2005,2007,2009 Colin Percival. All rights reserved.
4+
*
5+
* Redistribution and use in source and binary forms, with or without
6+
* modification, are permitted provided that the following conditions
7+
* are met:
8+
* 1. Redistributions of source code must retain the above copyright
9+
* notice, this list of conditions and the following disclaimer.
10+
* 2. Redistributions in binary form must reproduce the above copyright
11+
* notice, this list of conditions and the following disclaimer in the
12+
* documentation and/or other materials provided with the distribution.
13+
*
14+
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15+
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16+
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17+
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18+
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19+
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20+
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21+
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22+
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23+
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24+
* SUCH DAMAGE.
25+
*/
26+
#include "hmac_sha256.h"
27+
28+
#include <stdint.h>
29+
#include <string.h>
30+
#include "sha256.h"
31+
#include "zeroize.h"
32+
33+
void HMACSHA256(const uint8_t* input, size_t length, const uint8_t* key,
34+
size_t key_length, uint8_t digest[HMACSHA256_DIGEST_LENGTH])
35+
{
36+
HMACSHA256CTX context;
37+
HMACSHA256Init(&context, key, key_length);
38+
HMACSHA256Update(&context, input, length);
39+
HMACSHA256Final(&context, digest);
40+
}
41+
42+
void HMACSHA256Final(HMACSHA256CTX* context,
43+
uint8_t digest[HMACSHA256_DIGEST_LENGTH])
44+
{
45+
uint8_t hash[HMACSHA256_DIGEST_LENGTH];
46+
47+
SHA256Final(&context->ictx, hash);
48+
SHA256Update(&context->octx, hash, HMACSHA256_DIGEST_LENGTH);
49+
SHA256Final(&context->octx, digest);
50+
51+
zeroize((void*)hash, sizeof hash);
52+
}
53+
54+
void HMACSHA256Init(HMACSHA256CTX* context, const uint8_t* key,
55+
size_t key_length)
56+
{
57+
size_t i;
58+
uint8_t pad[SHA256_BLOCK_LENGTH];
59+
uint8_t key_hash[SHA256_DIGEST_LENGTH];
60+
61+
if (key_length > SHA256_BLOCK_LENGTH)
62+
{
63+
SHA256Init(&context->ictx);
64+
SHA256Update(&context->ictx, key, key_length);
65+
SHA256Final(&context->ictx, key_hash);
66+
key = key_hash;
67+
key_length = SHA256_DIGEST_LENGTH;
68+
}
69+
70+
SHA256Init(&context->ictx);
71+
memset(pad, 0x36, SHA256_BLOCK_LENGTH);
72+
73+
for (i = 0; i < key_length; i++)
74+
pad[i] ^= key[i];
75+
76+
SHA256Update(&context->ictx, pad, SHA256_BLOCK_LENGTH);
77+
SHA256Init(&context->octx);
78+
memset(pad, 0x5c, SHA256_BLOCK_LENGTH);
79+
80+
for (i = 0; i < key_length; i++)
81+
pad[i] ^= key[i];
82+
83+
SHA256Update(&context->octx, pad, SHA256_BLOCK_LENGTH);
84+
zeroize((void*)key_hash, sizeof key_hash);
85+
}
86+
87+
void HMACSHA256Update(HMACSHA256CTX* context, const uint8_t* input,
88+
size_t length)
89+
{
90+
SHA256Update(&context->ictx, input, length);
91+
}

src/crypto/external/hmac_sha256.h

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/* libsodium: hmac_hmacsha512.c, v0.4.5 2014/04/16 */
2+
/**
3+
* Copyright 2005,2007,2009 Colin Percival. All rights reserved.
4+
*
5+
* Redistribution and use in source and binary forms, with or without
6+
* modification, are permitted provided that the following conditions
7+
* are met:
8+
* 1. Redistributions of source code must retain the above copyright
9+
* notice, this list of conditions and the following disclaimer.
10+
* 2. Redistributions in binary form must reproduce the above copyright
11+
* notice, this list of conditions and the following disclaimer in the
12+
* documentation and/or other materials provided with the distribution.
13+
*
14+
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15+
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16+
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17+
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18+
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19+
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20+
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21+
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22+
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23+
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24+
* SUCH DAMAGE.
25+
*/
26+
#ifndef LIBBITCOIN_HMACSHA256_H
27+
#define LIBBITCOIN_HMACSHA256_H
28+
29+
#include <stdint.h>
30+
#include <stddef.h>
31+
#include "sha256.h"
32+
33+
#define HMACSHA256_DIGEST_LENGTH 32U
34+
35+
#ifdef __cplusplus
36+
extern "C"
37+
{
38+
#endif
39+
40+
typedef struct HMACSHA256CTX
41+
{
42+
SHA256CTX ctx;
43+
SHA256CTX ictx;
44+
SHA256CTX octx;
45+
} HMACSHA256CTX;
46+
47+
void HMACSHA256(const uint8_t* input, size_t length, const uint8_t* key,
48+
size_t key_length, uint8_t digest[HMACSHA256_DIGEST_LENGTH]);
49+
50+
void HMACSHA256Final(HMACSHA256CTX* context,
51+
uint8_t digest[HMACSHA256_DIGEST_LENGTH]);
52+
53+
void HMACSHA256Init(HMACSHA256CTX* context, const uint8_t* key,
54+
size_t key_length);
55+
56+
void HMACSHA256Update(HMACSHA256CTX* context, const uint8_t* input,
57+
size_t length);
58+
59+
#ifdef __cplusplus
60+
}
61+
#endif
62+
63+
#endif

0 commit comments

Comments
 (0)