mirror of
https://github.com/trezor/trezor-firmware.git
synced 2024-11-22 07:28:10 +00:00
add script parsing functions
This commit is contained in:
parent
aae96e8285
commit
31e05edca7
1
Makefile
1
Makefile
@ -37,6 +37,7 @@ CFLAGS += -DUSE_PRECOMPUTED_IV=0 -DUSE_PRECOMPUTED_CP=0
|
||||
endif
|
||||
|
||||
SRCS = bignum.c ecdsa.c curves.c secp256k1.c nist256p1.c rand.c hmac.c bip32.c bip39.c pbkdf2.c base58.c
|
||||
SRCS += script.c
|
||||
SRCS += ripemd160.c
|
||||
SRCS += sha2.c
|
||||
SRCS += sha3.c
|
||||
|
64
script.c
Normal file
64
script.c
Normal file
@ -0,0 +1,64 @@
|
||||
/**
|
||||
* Copyright (c) 2016 Pavol Rusnak
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining
|
||||
* a copy of this software and associated documentation files (the "Software"),
|
||||
* to deal in the Software without restriction, including without limitation
|
||||
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
||||
* and/or sell copies of the Software, and to permit persons to whom the
|
||||
* Software is furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included
|
||||
* in all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
|
||||
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
||||
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES
|
||||
* OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
|
||||
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
||||
* OTHER DEALINGS IN THE SOFTWARE.
|
||||
*/
|
||||
|
||||
#include <string.h>
|
||||
#include "base58.h"
|
||||
#include "script.h"
|
||||
|
||||
int script_output_to_address(const uint8_t *script, int scriptlen, char *addr, int addrsize)
|
||||
{
|
||||
uint8_t raw[35];
|
||||
|
||||
// P2PKH
|
||||
if (scriptlen == 25 && script[0] == 0x76 && script[1] == 0xA9 && script[2] == 0x14 && script[23] == 0x88 && script[24] == 0xAC) {
|
||||
raw[0] = 0x00;
|
||||
memcpy(raw + 1, script + 3, 20);
|
||||
return base58_encode_check(raw, 1 + 20, addr, addrsize);
|
||||
}
|
||||
|
||||
// P2SH
|
||||
if (scriptlen == 23 && script[0] == 0xA9 && script[1] == 0x14 && script[22] == 0x87) {
|
||||
raw[0] = 0x05;
|
||||
memcpy(raw + 1, script + 2, 20);
|
||||
return base58_encode_check(raw, 1 + 20, addr, addrsize);
|
||||
}
|
||||
|
||||
// P2WPKH
|
||||
if (scriptlen == 22 && script[0] == 0x00 && script[1] == 0x14) {
|
||||
raw[0] = 0x06;
|
||||
raw[1] = 0x00;
|
||||
raw[2] = 0x00;
|
||||
memcpy(raw + 3, script + 2, 20);
|
||||
return base58_encode_check(raw, 3 + 20, addr, addrsize);
|
||||
}
|
||||
|
||||
// P2WSH
|
||||
if (scriptlen == 34 && script[0] == 0x00 && script[1] == 0x20) {
|
||||
raw[0] = 0x0A;
|
||||
raw[1] = 0x00;
|
||||
raw[2] = 0x00;
|
||||
memcpy(raw + 3, script + 2, 32);
|
||||
return base58_encode_check(raw, 3 + 32, addr, addrsize);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
30
script.h
Normal file
30
script.h
Normal file
@ -0,0 +1,30 @@
|
||||
/**
|
||||
* Copyright (c) 2016 Pavol Rusnak
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining
|
||||
* a copy of this software and associated documentation files (the "Software"),
|
||||
* to deal in the Software without restriction, including without limitation
|
||||
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
||||
* and/or sell copies of the Software, and to permit persons to whom the
|
||||
* Software is furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included
|
||||
* in all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
|
||||
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
||||
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES
|
||||
* OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
|
||||
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
||||
* OTHER DEALINGS IN THE SOFTWARE.
|
||||
*/
|
||||
|
||||
#ifndef __SCRIPT_H__
|
||||
#define __SCRIPT_H__
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
int script_output_to_address(const uint8_t *script, int scriptlen, char *addr, int addrsize);
|
||||
|
||||
#endif
|
27
tests.c
27
tests.c
@ -42,6 +42,7 @@
|
||||
#include "secp256k1.h"
|
||||
#include "nist256p1.h"
|
||||
#include "ed25519.h"
|
||||
#include "script.h"
|
||||
|
||||
uint8_t *fromhex(const char *str)
|
||||
{
|
||||
@ -1872,6 +1873,28 @@ START_TEST(test_ed25519) {
|
||||
}
|
||||
END_TEST
|
||||
|
||||
START_TEST(test_output_script) {
|
||||
static const char *vectors[] = {
|
||||
"76A914010966776006953D5567439E5E39F86A0D273BEE88AC", "16UwLL9Risc3QfPqBUvKofHmBQ7wMtjvM",
|
||||
"A914010966776006953D5567439E5E39F86A0D273BEE87", "31nVrspaydBz8aMpxH9WkS2DuhgqS1fCuG",
|
||||
"0014010966776006953D5567439E5E39F86A0D273BEE", "p2xtZoXeX5X8BP8JfFhQK2nD3emtjch7UeFm",
|
||||
"00200102030405060708090A0B0C0D0E0F101112131415161718191A1B1C1D1E1F20", "7XhPD7te3C6CVKnJWUhrTJbFTwudhHqfrjpS59AS6sMzL4RYFiCNg",
|
||||
0, 0,
|
||||
};
|
||||
const char **scr, **adr;
|
||||
scr = vectors;
|
||||
adr = vectors + 1;
|
||||
char address[60];
|
||||
while (*scr && *adr) {
|
||||
int r = script_output_to_address(fromhex(*scr), strlen(*scr)/2, address, 60);
|
||||
ck_assert_int_eq(r, strlen(*adr) + 1);
|
||||
ck_assert_str_eq(address, *adr);
|
||||
scr += 2;
|
||||
adr += 2;
|
||||
}
|
||||
}
|
||||
END_TEST
|
||||
|
||||
|
||||
// define test suite and cases
|
||||
Suite *test_suite(void)
|
||||
@ -1975,6 +1998,10 @@ Suite *test_suite(void)
|
||||
tcase_add_test(tc, test_ed25519);
|
||||
suite_add_tcase(s, tc);
|
||||
|
||||
tc = tcase_create("script");
|
||||
tcase_add_test(tc, test_output_script);
|
||||
suite_add_tcase(s, tc);
|
||||
|
||||
return s;
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user