|
1 | 1 | #include "internal.h" |
2 | 2 | #ifdef BUILD_ELEMENTS |
3 | 3 | #include "script_int.h" |
| 4 | +#include "tx_io.h" |
4 | 5 | #include <include/wally_address.h> |
5 | 6 | #include <include/wally_bip32.h> |
6 | 7 | #include <include/wally_elements.h> |
|
13 | 14 | #include <secp256k1_whitelist.h> |
14 | 15 |
|
15 | 16 |
|
16 | | -static const unsigned char LABEL_STR[] = { |
| 17 | +static const unsigned char SLIP77_LABEL[] = { |
17 | 18 | 'S', 'L', 'I', 'P', '-', '0', '0', '7', '7' |
18 | 19 | }; |
19 | 20 |
|
| 21 | +/* SHA256(CT-Blinding-Key/1.0) */ |
| 22 | +static const unsigned char CT_BLINDING_KEY_1_0_SHA256[SHA256_LEN] = { |
| 23 | + 0x02, 0xe0, 0xc2, 0x24, 0x76, 0xf8, 0xc5, 0xfd, 0xb7, 0x30, 0x5d, 0x9f, 0xd0, 0xe0, 0xa3, 0x56, |
| 24 | + 0xb5, 0x88, 0x77, 0x69, 0x24, 0x8e, 0x04, 0xc8, 0x6f, 0xda, 0xad, 0x35, 0x11, 0x37, 0x85, 0xb4 |
| 25 | +}; |
| 26 | + |
20 | 27 | static int parse_generator(const secp256k1_context *ctx, |
21 | 28 | const unsigned char *generator, size_t generator_len, |
22 | 29 | secp256k1_generator *dest) |
@@ -808,7 +815,8 @@ int wally_asset_blinding_key_from_seed( |
808 | 815 |
|
809 | 816 | ret = wally_symmetric_key_from_seed(bytes, bytes_len, root, sizeof(root)); |
810 | 817 | if (ret == WALLY_OK) { |
811 | | - ret = wally_symmetric_key_from_parent(root, sizeof(root), 0, LABEL_STR, sizeof(LABEL_STR), |
| 818 | + ret = wally_symmetric_key_from_parent(root, sizeof(root), 0, |
| 819 | + SLIP77_LABEL, sizeof(SLIP77_LABEL), |
812 | 820 | bytes_out, len); |
813 | 821 | wally_clear(root, sizeof(root)); |
814 | 822 | } |
@@ -874,6 +882,93 @@ int wally_asset_blinding_key_to_ec_public_key( |
874 | 882 | #endif /* BUILD_ELEMENTS */ |
875 | 883 | } |
876 | 884 |
|
| 885 | +#ifdef BUILD_ELEMENTS |
| 886 | +static void elip150_tagged_hash(const unsigned char *pubkey, size_t pubkey_len, |
| 887 | + const unsigned char *script, size_t script_len, |
| 888 | + struct sha256 *sha_out) |
| 889 | +{ |
| 890 | + struct sha256_ctx ctx; |
| 891 | + tagged_hash_init(&ctx, CT_BLINDING_KEY_1_0_SHA256, SHA256_LEN); |
| 892 | + sha256_update(&ctx, pubkey, pubkey_len); |
| 893 | + hash_varbuff(&ctx, script, script_len); /* Consensus encoding */ |
| 894 | + sha256_done(&ctx, sha_out); |
| 895 | +} |
| 896 | +#endif /* BUILD_ELEMENTS */ |
| 897 | + |
| 898 | +int wally_elip150_private_key_to_ec_private_key( |
| 899 | + const unsigned char *bytes, size_t bytes_len, |
| 900 | + const unsigned char *script, size_t script_len, |
| 901 | + unsigned char *bytes_out, size_t len) |
| 902 | +{ |
| 903 | +#ifndef BUILD_ELEMENTS |
| 904 | + return WALLY_ERROR; |
| 905 | +#else |
| 906 | + unsigned char pubkey[EC_PUBLIC_KEY_LEN]; |
| 907 | + int ret; |
| 908 | + |
| 909 | + if (!bytes || bytes_len != EC_PRIVATE_KEY_LEN || !script || !script_len || |
| 910 | + !bytes_out || len != EC_PRIVATE_KEY_LEN) |
| 911 | + return WALLY_EINVAL; |
| 912 | + |
| 913 | + ret = wally_ec_public_key_from_private_key(bytes, bytes_len, |
| 914 | + pubkey, sizeof(pubkey)); |
| 915 | + if (ret == WALLY_OK) { |
| 916 | + struct sha256 sha; |
| 917 | + unsigned char tweaked[EC_PRIVATE_KEY_LEN]; |
| 918 | + elip150_tagged_hash(pubkey, sizeof(pubkey), script, script_len, &sha); |
| 919 | + ret = wally_ec_scalar_add(bytes, bytes_len, sha.u.u8, sizeof(sha), |
| 920 | + tweaked, sizeof(tweaked)); |
| 921 | + if (ret == WALLY_OK) |
| 922 | + memcpy(bytes_out, tweaked, sizeof(tweaked)); |
| 923 | + } |
| 924 | + return ret; |
| 925 | +#endif /* BUILD_ELEMENTS */ |
| 926 | +} |
| 927 | + |
| 928 | +int wally_elip150_private_key_to_ec_public_key( |
| 929 | + const unsigned char *bytes, size_t bytes_len, |
| 930 | + const unsigned char *script, size_t script_len, |
| 931 | + unsigned char *bytes_out, size_t len) |
| 932 | +{ |
| 933 | +#ifndef BUILD_ELEMENTS |
| 934 | + return WALLY_ERROR; |
| 935 | +#else |
| 936 | + unsigned char pubkey[EC_PUBLIC_KEY_LEN]; |
| 937 | + int ret = wally_ec_public_key_from_private_key(bytes, bytes_len, |
| 938 | + pubkey, sizeof(pubkey)); |
| 939 | + if (ret == WALLY_OK) |
| 940 | + ret = wally_elip150_public_key_to_ec_public_key(pubkey, sizeof(pubkey), |
| 941 | + script, script_len, |
| 942 | + bytes_out, len); |
| 943 | + return ret; |
| 944 | +#endif /* BUILD_ELEMENTS */ |
| 945 | +} |
| 946 | + |
| 947 | +int wally_elip150_public_key_to_ec_public_key( |
| 948 | + const unsigned char *bytes, size_t bytes_len, |
| 949 | + const unsigned char *script, size_t script_len, |
| 950 | + unsigned char *bytes_out, size_t len) |
| 951 | +{ |
| 952 | +#ifndef BUILD_ELEMENTS |
| 953 | + return WALLY_ERROR; |
| 954 | +#else |
| 955 | + struct sha256 sha; |
| 956 | + unsigned char tweaked[EC_PUBLIC_KEY_LEN]; |
| 957 | + int ret; |
| 958 | + |
| 959 | + if (!bytes || bytes_len != EC_PUBLIC_KEY_LEN || !script || !script_len || |
| 960 | + !bytes_out || len != EC_PUBLIC_KEY_LEN) |
| 961 | + return WALLY_EINVAL; |
| 962 | + |
| 963 | + elip150_tagged_hash(bytes, bytes_len, script, script_len, &sha); |
| 964 | + ret = wally_ec_public_key_tweak(bytes, bytes_len, sha.u.u8, sizeof(sha), |
| 965 | + tweaked, sizeof(tweaked)); |
| 966 | + if (ret == WALLY_OK) |
| 967 | + memcpy(bytes_out, tweaked, sizeof(tweaked)); |
| 968 | + return ret; |
| 969 | +#endif /* BUILD_ELEMENTS */ |
| 970 | +} |
| 971 | + |
877 | 972 | #define BK_ABF 0x1 |
878 | 973 | #define BK_VBF 0x2 |
879 | 974 |
|
|
0 commit comments