Skip to content

Commit 482255d

Browse files
committed
tools for working with deterministic wallets.
1 parent d9cfb62 commit 482255d

File tree

9 files changed

+247
-0
lines changed

9 files changed

+247
-0
lines changed

build.sh

+43
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,52 @@ cd showtx
1919
make
2020
cd ..
2121
echo "Compiled showtx"
22+
# Sign tx input
23+
cd sign-input
24+
make
25+
cd ..
26+
echo "Compiled sign-input"
27+
# Broadcast a transaction.
28+
cd broadcast-tx
29+
make
30+
cd ..
31+
echo "Compiled broadcast-tx"
32+
# Create new deterministic wallet
33+
cd newseed
34+
make
35+
cd ..
36+
echo "Compiled newseed"
37+
# Get master public key from a seed
38+
cd mpk
39+
make
40+
cd ..
41+
echo "Compiled mpk"
42+
# Generate a private key from a seed, number and optional for_change flag.
43+
cd genpriv
44+
make
45+
cd ..
46+
echo "Compiled genpriv"
47+
# Generate an address from a master pubkey, number and optional for_change flag.
48+
cd genpub
49+
make
50+
cd ..
51+
echo "Compiled genpub"
2252
# These 2 tools depend on Obelisk: http://github.com/genjix/obelisk
2353
#cd balance
2454
#make
2555
#cd history
2656
#make
2757

58+
mkdir -p bin
59+
cp newpriv/newpriv bin/
60+
cp showpriv/showpriv bin/
61+
cp mktx/mktx bin/
62+
cp showtx/showtx bin/
63+
cp sign-input/sign-input bin/
64+
cp broadcast-tx/broadcast-tx bin/
65+
cp newseed/newseed bin/
66+
cp mpk/mpk bin/
67+
cp genpriv/genpriv bin/
68+
cp genpub/genpub bin/
69+
echo "Copied binaries to bin/"
70+

genpriv/Makefile

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
CXXFLAGS=$(shell pkg-config --cflags libbitcoin)
2+
LIBS=$(shell pkg-config --libs libbitcoin)
3+
4+
default: genpriv
5+
6+
main.o: main.cpp
7+
g++ -c main.cpp -o main.o $(CXXFLAGS)
8+
9+
genpriv: main.o
10+
g++ -o genpriv main.o $(LIBS)
11+

genpriv/main.cpp

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
#include <boost/lexical_cast.hpp>
2+
#include <boost/algorithm/string.hpp>
3+
#include <bitcoin/bitcoin.hpp>
4+
using namespace bc;
5+
6+
std::string dump_file(std::istream& in_file)
7+
{
8+
std::istreambuf_iterator<char> it(in_file);
9+
std::istreambuf_iterator<char> end;
10+
return std::string(it, end);
11+
}
12+
13+
int main(int argc, char** argv)
14+
{
15+
if (argc != 2 && argc != 3)
16+
{
17+
std::cerr << "Usage: genpriv N [CHANGE]" << std::endl;
18+
return -1;
19+
}
20+
size_t n;
21+
try
22+
{
23+
n = boost::lexical_cast<size_t>(argv[1]);
24+
}
25+
catch (const boost::bad_lexical_cast&)
26+
{
27+
std::cerr << "genpriv: Bad N provided" << std::endl;
28+
return -1;
29+
}
30+
bool for_change = false;
31+
if (argc == 3)
32+
{
33+
std::string change_str = argv[2];
34+
boost::algorithm::to_lower(change_str);
35+
if (change_str == "true" || change_str == "1")
36+
for_change = true;
37+
}
38+
std::string seed = dump_file(std::cin);
39+
boost::algorithm::trim(seed);
40+
deterministic_wallet wallet;
41+
if (!wallet.set_seed(seed))
42+
{
43+
std::cerr << "genpriv: Error setting seed" << std::endl;
44+
return -1;
45+
}
46+
secret_parameter secret = wallet.generate_secret(n, for_change);
47+
elliptic_curve_key key;
48+
if (!key.set_secret(secret))
49+
{
50+
std::cerr << "genpriv: Internal error setting secret" << std::endl;
51+
return -1;
52+
}
53+
private_data raw_private_key = key.private_key();
54+
std::cout << std::string(raw_private_key.begin(), raw_private_key.end());
55+
return 0;
56+
}
57+

genpub/Makefile

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
CXXFLAGS=$(shell pkg-config --cflags libbitcoin)
2+
LIBS=$(shell pkg-config --libs libbitcoin)
3+
4+
default: genpub
5+
6+
main.o: main.cpp
7+
g++ -c main.cpp -o main.o $(CXXFLAGS)
8+
9+
genpub: main.o
10+
g++ -o genpub main.o $(LIBS)
11+

genpub/main.cpp

+66
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
#include <boost/lexical_cast.hpp>
2+
#include <boost/algorithm/string.hpp>
3+
#include <bitcoin/bitcoin.hpp>
4+
using namespace bc;
5+
6+
std::string dump_file(std::istream& in_file)
7+
{
8+
std::istreambuf_iterator<char> it(in_file);
9+
std::istreambuf_iterator<char> end;
10+
return std::string(it, end);
11+
}
12+
13+
data_chunk read_mpk_data()
14+
{
15+
std::string raw_mpk = dump_file(std::cin);
16+
return data_chunk(raw_mpk.begin(), raw_mpk.end());
17+
}
18+
19+
int main(int argc, char** argv)
20+
{
21+
if (argc != 2 && argc != 3)
22+
{
23+
std::cerr << "Usage: genpub N [CHANGE]" << std::endl;
24+
return -1;
25+
}
26+
size_t n;
27+
try
28+
{
29+
n = boost::lexical_cast<size_t>(argv[1]);
30+
}
31+
catch (const boost::bad_lexical_cast&)
32+
{
33+
std::cerr << "genpub: Bad N provided" << std::endl;
34+
return -1;
35+
}
36+
bool for_change = false;
37+
if (argc == 3)
38+
{
39+
std::string change_str = argv[2];
40+
boost::algorithm::to_lower(change_str);
41+
if (change_str == "true" || change_str == "1")
42+
for_change = true;
43+
}
44+
const data_chunk mpk = read_mpk_data();
45+
if (mpk.empty())
46+
{
47+
std::cerr << "genpub: Empty master public key" << std::endl;
48+
return -1;
49+
}
50+
deterministic_wallet wallet;
51+
if (!wallet.set_master_public_key(mpk))
52+
{
53+
std::cerr << "genpub: Error setting master public key" << std::endl;
54+
return -1;
55+
}
56+
data_chunk pubkey = wallet.generate_public_key(n, for_change);
57+
payment_address addr;
58+
if (!set_public_key(addr, pubkey))
59+
{
60+
std::cerr << "genpub: Internal error setting pubkey" << std::endl;
61+
return -1;
62+
}
63+
std::cout << addr.encoded() << std::endl;
64+
return 0;
65+
}
66+

mpk/Makefile

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
CXXFLAGS=$(shell pkg-config --cflags libbitcoin)
2+
LIBS=$(shell pkg-config --libs libbitcoin)
3+
4+
default: mpk
5+
6+
main.o: main.cpp
7+
g++ -c main.cpp -o main.o $(CXXFLAGS)
8+
9+
mpk: main.o
10+
g++ -o mpk main.o $(LIBS)
11+

mpk/main.cpp

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#include <boost/algorithm/string.hpp>
2+
#include <bitcoin/bitcoin.hpp>
3+
using namespace bc;
4+
5+
std::string dump_file(std::istream& in_file)
6+
{
7+
std::istreambuf_iterator<char> it(in_file);
8+
std::istreambuf_iterator<char> end;
9+
return std::string(it, end);
10+
}
11+
12+
int main()
13+
{
14+
std::string seed = dump_file(std::cin);
15+
boost::algorithm::trim(seed);
16+
deterministic_wallet wallet;
17+
if (!wallet.set_seed(seed))
18+
{
19+
std::cerr << "mpk: Error setting seed" << std::endl;
20+
return -1;
21+
}
22+
const data_chunk mpk = wallet.master_public_key();
23+
std::cout << std::string(mpk.begin(), mpk.end()) << std::endl;
24+
return 0;
25+
}
26+

newseed/Makefile

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
CXXFLAGS=$(shell pkg-config --cflags libbitcoin)
2+
LIBS=$(shell pkg-config --libs libbitcoin)
3+
4+
default: newseed
5+
6+
main.o: main.cpp
7+
g++ -c main.cpp -o main.o $(CXXFLAGS)
8+
9+
newseed: main.o
10+
g++ -o newseed main.o $(LIBS)
11+

newseed/main.cpp

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#include <bitcoin/bitcoin.hpp>
2+
using namespace bc;
3+
4+
int main()
5+
{
6+
deterministic_wallet wallet;
7+
wallet.new_seed();
8+
std::cout << wallet.seed() << std::endl;
9+
return 0;
10+
}
11+

0 commit comments

Comments
 (0)