Skip to content

Commit b47b6ab

Browse files
committed
update naming, adding coin prototyping, adding WIP BIP32 code
1 parent 1166f2b commit b47b6ab

File tree

9 files changed

+1236
-6
lines changed

9 files changed

+1236
-6
lines changed

application.fam

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
App(
2-
appid="flipbip39",
3-
name="Flip-BIP39",
2+
appid="FlipBIP",
3+
name="BIP39 Tool",
44
apptype=FlipperAppType.EXTERNAL,
55
entry_point="flipbip39_app",
6-
cdefines=["APP_FLIPBIP39"],
6+
cdefines=["APP_FLIPBIP"],
77
requires=[
88
"gui",
99
],

coins/coin_info.c

Lines changed: 194 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,194 @@
1+
// This file is automatically generated from coin_info.c.mako
2+
3+
// DO NOT EDIT
4+
5+
6+
7+
#include "coins.h"
8+
9+
10+
11+
#include "../crypto/curves.h"
12+
13+
#include "../crypto/secp256k1.h"
14+
15+
16+
17+
const CoinInfo coins[COINS_COUNT] = {
18+
19+
{
20+
21+
.coin_name = "Bitcoin",
22+
23+
.coin_shortcut = "BTC",
24+
25+
.maxfee_kb = 2000000,
26+
27+
.signed_message_header = "\x18" "Bitcoin Signed Message:\n",
28+
29+
.has_segwit = true,
30+
31+
.has_taproot = true,
32+
33+
.has_fork_id = false,
34+
35+
.force_bip143 = false,
36+
37+
.decred = false,
38+
39+
.decimals = 8,
40+
41+
.address_type = 0,
42+
43+
.address_type_p2sh = 5,
44+
45+
.xpub_magic = 0x0488b21e,
46+
47+
.xpub_magic_segwit_p2sh = 0x049d7cb2,
48+
49+
.xpub_magic_segwit_native = 0x04b24746,
50+
51+
.xpub_magic_multisig_segwit_p2sh = 0x0295b43f,
52+
53+
.xpub_magic_multisig_segwit_native = 0x02aa7ed3,
54+
55+
.fork_id = 0,
56+
57+
.bech32_prefix = "bc",
58+
59+
.cashaddr_prefix = NULL,
60+
61+
.coin_type = (0 | 0x80000000),
62+
63+
.negative_fee = false,
64+
65+
.curve_name = SECP256K1_NAME,
66+
67+
.curve = &secp256k1_info,
68+
69+
.extra_data = false,
70+
71+
.timestamp = false,
72+
73+
.overwintered = false,
74+
75+
},
76+
77+
{
78+
79+
.coin_name = "Regtest",
80+
81+
.coin_shortcut = "REGTEST",
82+
83+
.maxfee_kb = 10000000,
84+
85+
.signed_message_header = "\x18" "Bitcoin Signed Message:\n",
86+
87+
.has_segwit = true,
88+
89+
.has_taproot = true,
90+
91+
.has_fork_id = false,
92+
93+
.force_bip143 = false,
94+
95+
.decred = false,
96+
97+
.decimals = 8,
98+
99+
.address_type = 111,
100+
101+
.address_type_p2sh = 196,
102+
103+
.xpub_magic = 0x043587cf,
104+
105+
.xpub_magic_segwit_p2sh = 0x044a5262,
106+
107+
.xpub_magic_segwit_native = 0x045f1cf6,
108+
109+
.xpub_magic_multisig_segwit_p2sh = 0x024289ef,
110+
111+
.xpub_magic_multisig_segwit_native = 0x02575483,
112+
113+
.fork_id = 0,
114+
115+
.bech32_prefix = "bcrt",
116+
117+
.cashaddr_prefix = NULL,
118+
119+
.coin_type = (1 | 0x80000000),
120+
121+
.negative_fee = false,
122+
123+
.curve_name = SECP256K1_NAME,
124+
125+
.curve = &secp256k1_info,
126+
127+
.extra_data = false,
128+
129+
.timestamp = false,
130+
131+
.overwintered = false,
132+
133+
},
134+
135+
{
136+
137+
.coin_name = "Testnet",
138+
139+
.coin_shortcut = "TEST",
140+
141+
.maxfee_kb = 10000000,
142+
143+
.signed_message_header = "\x18" "Bitcoin Signed Message:\n",
144+
145+
.has_segwit = true,
146+
147+
.has_taproot = true,
148+
149+
.has_fork_id = false,
150+
151+
.force_bip143 = false,
152+
153+
.decred = false,
154+
155+
.decimals = 8,
156+
157+
.address_type = 111,
158+
159+
.address_type_p2sh = 196,
160+
161+
.xpub_magic = 0x043587cf,
162+
163+
.xpub_magic_segwit_p2sh = 0x044a5262,
164+
165+
.xpub_magic_segwit_native = 0x045f1cf6,
166+
167+
.xpub_magic_multisig_segwit_p2sh = 0x024289ef,
168+
169+
.xpub_magic_multisig_segwit_native = 0x02575483,
170+
171+
.fork_id = 0,
172+
173+
.bech32_prefix = "tb",
174+
175+
.cashaddr_prefix = NULL,
176+
177+
.coin_type = (1 | 0x80000000),
178+
179+
.negative_fee = false,
180+
181+
.curve_name = SECP256K1_NAME,
182+
183+
.curve = &secp256k1_info,
184+
185+
.extra_data = false,
186+
187+
.timestamp = false,
188+
189+
.overwintered = false,
190+
191+
},
192+
193+
};
194+

coins/coin_info.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// This file is automatically generated from coin_info.h.mako
2+
// DO NOT EDIT
3+
4+
#ifndef __COIN_INFO_H__
5+
#define __COIN_INFO_H__
6+
7+
#include "coins.h"
8+
9+
#define COINS_COUNT (3)
10+
11+
extern const CoinInfo coins[COINS_COUNT];
12+
13+
#endif

coins/coins.c

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
/*
2+
* This file is part of the Trezor project, https://trezor.io/
3+
*
4+
* Copyright (C) 2014 Pavol Rusnak <[email protected]>
5+
*
6+
* This library is free software: you can redistribute it and/or modify
7+
* it under the terms of the GNU Lesser General Public License as published by
8+
* the Free Software Foundation, either version 3 of the License, or
9+
* (at your option) any later version.
10+
*
11+
* This library is distributed in the hope that it will be useful,
12+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
* GNU Lesser General Public License for more details.
15+
*
16+
* You should have received a copy of the GNU Lesser General Public License
17+
* along with this library. If not, see <http://www.gnu.org/licenses/>.
18+
*/
19+
20+
#include "coins.h"
21+
#include <string.h>
22+
#include "../crypto/address.h"
23+
#include "../crypto/base58.h"
24+
#include "../crypto/ecdsa.h"
25+
26+
const CoinInfo *coinByName(const char *name) {
27+
if (!name) return 0;
28+
for (int i = 0; i < COINS_COUNT; i++) {
29+
if (strcmp(name, coins[i].coin_name) == 0) {
30+
return &(coins[i]);
31+
}
32+
}
33+
return 0;
34+
}
35+
36+
const CoinInfo *coinByAddressType(uint32_t address_type) {
37+
for (int i = 0; i < COINS_COUNT; i++) {
38+
if (address_type == coins[i].address_type) {
39+
return &(coins[i]);
40+
}
41+
}
42+
return 0;
43+
}
44+
45+
const CoinInfo *coinBySlip44(uint32_t coin_type) {
46+
if (coin_type == SLIP44_TESTNET) {
47+
// The slip44 coin type is the same for all testnets, so we return the
48+
// Bitcoin Testnet.
49+
return coinByName("Testnet");
50+
}
51+
52+
for (int i = 0; i < COINS_COUNT; i++) {
53+
if (coin_type == coins[i].coin_type) {
54+
return &(coins[i]);
55+
}
56+
}
57+
return 0;
58+
}
59+
60+
bool coinExtractAddressType(const CoinInfo *coin, const char *addr,
61+
uint32_t *address_type) {
62+
if (!addr) return false;
63+
uint8_t addr_raw[MAX_ADDR_RAW_SIZE] = {0};
64+
int len = base58_decode_check(addr, coin->curve->hasher_base58, addr_raw,
65+
MAX_ADDR_RAW_SIZE);
66+
if (len >= 21) {
67+
return coinExtractAddressTypeRaw(coin, addr_raw, address_type);
68+
}
69+
return false;
70+
}
71+
72+
bool coinExtractAddressTypeRaw(const CoinInfo *coin, const uint8_t *addr_raw,
73+
uint32_t *address_type) {
74+
if (address_check_prefix(addr_raw, coin->address_type)) {
75+
*address_type = coin->address_type;
76+
return true;
77+
}
78+
if (address_check_prefix(addr_raw, coin->address_type_p2sh)) {
79+
*address_type = coin->address_type_p2sh;
80+
return true;
81+
}
82+
*address_type = 0;
83+
return false;
84+
}

coins/coins.h

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
/*
2+
* This file is part of the Trezor project, https://trezor.io/
3+
*
4+
* Copyright (C) 2014 Pavol Rusnak <[email protected]>
5+
*
6+
* This library is free software: you can redistribute it and/or modify
7+
* it under the terms of the GNU Lesser General Public License as published by
8+
* the Free Software Foundation, either version 3 of the License, or
9+
* (at your option) any later version.
10+
*
11+
* This library is distributed in the hope that it will be useful,
12+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
* GNU Lesser General Public License for more details.
15+
*
16+
* You should have received a copy of the GNU Lesser General Public License
17+
* along with this library. If not, see <http://www.gnu.org/licenses/>.
18+
*/
19+
20+
#ifndef __COINS_H__
21+
#define __COINS_H__
22+
23+
#include <stdbool.h>
24+
#include <stdint.h>
25+
26+
#include "../crypto/bip32.h"
27+
#include "../crypto/hasher.h"
28+
29+
typedef struct _CoinInfo {
30+
const char *coin_name;
31+
const char *coin_shortcut;
32+
uint64_t maxfee_kb;
33+
const char *signed_message_header;
34+
uint32_t decimals;
35+
bool has_segwit;
36+
bool has_taproot;
37+
bool has_fork_id;
38+
bool force_bip143;
39+
bool decred;
40+
// address types > 0xFF represent a two-byte prefix in big-endian order
41+
uint32_t address_type;
42+
uint32_t address_type_p2sh;
43+
uint32_t xpub_magic;
44+
uint32_t xpub_magic_segwit_p2sh;
45+
uint32_t xpub_magic_segwit_native;
46+
uint32_t xpub_magic_multisig_segwit_p2sh;
47+
uint32_t xpub_magic_multisig_segwit_native;
48+
uint32_t fork_id;
49+
const char *bech32_prefix;
50+
const char *cashaddr_prefix;
51+
uint32_t coin_type;
52+
bool negative_fee;
53+
const char *curve_name;
54+
const curve_info *curve;
55+
bool extra_data;
56+
bool timestamp;
57+
bool overwintered;
58+
} CoinInfo;
59+
60+
#include "coin_info.h"
61+
62+
// SLIP-44 hardened coin type for Bitcoin
63+
#define SLIP44_BITCOIN 0x80000000
64+
65+
// SLIP-44 hardened coin type for all Testnet coins
66+
#define SLIP44_TESTNET 0x80000001
67+
68+
const CoinInfo *coinByName(const char *name);
69+
const CoinInfo *coinByAddressType(uint32_t address_type);
70+
const CoinInfo *coinBySlip44(uint32_t coin_type);
71+
bool coinExtractAddressType(const CoinInfo *coin, const char *addr,
72+
uint32_t *address_type);
73+
bool coinExtractAddressTypeRaw(const CoinInfo *coin, const uint8_t *addr_raw,
74+
uint32_t *address_type);
75+
76+
#endif

0 commit comments

Comments
 (0)