1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2024-11-30 19:38:15 +00:00

refactor(legacy): introduce cryptoMultisigPubkeys

This commit is contained in:
Ondřej Vejpustek 2024-11-22 17:54:51 +01:00
parent 8b7a28f20e
commit 93e3fd925b
3 changed files with 38 additions and 6 deletions

View File

@ -368,6 +368,25 @@ uint32_t cryptoMultisigPubkeyCount(const MultisigRedeemScriptType *multisig) {
: multisig->pubkeys_count;
}
uint32_t cryptoMultisigPubkeys(const CoinInfo *coin,
const MultisigRedeemScriptType *multisig,
uint8_t *pubkeys) {
const uint32_t n = cryptoMultisigPubkeyCount(multisig);
if (n < 1 || n > 15) {
return 0;
}
for (uint32_t i = 0; i < n; i++) {
const HDNode *pubnode = cryptoMultisigPubkey(coin, multisig, i);
if (!pubnode) {
return 0;
}
memcpy(pubkeys + i * 33, pubnode->public_key, 33);
}
return n;
}
int cryptoMultisigPubkeyIndex(const CoinInfo *coin,
const MultisigRedeemScriptType *multisig,
const uint8_t *pubkey) {

View File

@ -88,6 +88,10 @@ int cryptoMultisigPubkeyIndex(const CoinInfo *coin,
const MultisigRedeemScriptType *multisig,
const uint8_t *pubkey);
uint32_t cryptoMultisigPubkeys(const CoinInfo *coin,
const MultisigRedeemScriptType *multisig,
uint8_t *pubkeys);
int cryptoMultisigFingerprint(const MultisigRedeemScriptType *multisig,
uint8_t *hash);
@ -115,5 +119,6 @@ void slip21_from_seed(const uint8_t *seed, int seed_len, Slip21Node *out);
void slip21_derive_path(Slip21Node *inout, const uint8_t *label,
size_t label_len);
const uint8_t *slip21_key(const Slip21Node *node);
bool multisig_uses_single_path(const MultisigRedeemScriptType *multisig);
#endif

View File

@ -373,6 +373,12 @@ uint32_t compile_script_multisig(const CoinInfo *coin,
const uint32_t n = cryptoMultisigPubkeyCount(multisig);
if (m < 1 || m > 15) return 0;
if (n < 1 || n > 15) return 0;
uint8_t pubkeys[33 * n];
if (!cryptoMultisigPubkeys(coin, multisig, pubkeys)) {
return 0;
}
uint32_t r = 0;
if (out) {
out[r] = 0x50 + m;
@ -380,9 +386,7 @@ uint32_t compile_script_multisig(const CoinInfo *coin,
for (uint32_t i = 0; i < n; i++) {
out[r] = 33;
r++; // OP_PUSH 33
const HDNode *pubnode = cryptoMultisigPubkey(coin, multisig, i);
if (!pubnode) return 0;
memcpy(out + r, pubnode->public_key, 33);
memcpy(out + r, pubkeys + 33 * i, 33);
r += 33;
}
out[r] = 0x50 + n;
@ -409,6 +413,12 @@ uint32_t compile_script_multisig_hash(const CoinInfo *coin,
if (m < 1 || m > 15) return 0;
if (n < 1 || n > 15) return 0;
// allocate on stack instead of heap
uint8_t pubkeys[33 * n];
if (!cryptoMultisigPubkeys(coin, multisig, pubkeys)) {
return 0;
}
Hasher hasher = {0};
hasher_Init(&hasher, coin->curve->hasher_script);
@ -418,9 +428,7 @@ uint32_t compile_script_multisig_hash(const CoinInfo *coin,
for (uint32_t i = 0; i < n; i++) {
d[0] = 33;
hasher_Update(&hasher, d, 1); // OP_PUSH 33
const HDNode *pubnode = cryptoMultisigPubkey(coin, multisig, i);
if (!pubnode) return 0;
hasher_Update(&hasher, pubnode->public_key, 33);
hasher_Update(&hasher, pubkeys + 33 * i, 33);
}
d[0] = 0x50 + n;
d[1] = 0xAE;