niceBit

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README | LICENSE

createPubKey.h (1182B)


      1 #include "base58.h"
      2 #include <ctype.h>
      3 #include <openssl/ripemd.h>
      4 #include <openssl/sha.h>
      5 #include <stdio.h>
      6 #include <string.h>
      7 
      8 typedef unsigned char byte;
      9 
     10 int is_hex(const char *s) {
     11     int i;
     12     for (i = 0; i < 64; i++)
     13         if (!isxdigit(s[i]))
     14             return 0;
     15     return 1;
     16 }
     17 
     18 void str_to_byte(const char *src, byte *dst, int n) {
     19     while (n--)
     20         sscanf(src + n * 2, "%2hhx", dst + n);
     21 }
     22 
     23 void pubkey_to_P2PKH(const unsigned char *pubkey64, char *out) {
     24     byte s[65];
     25     byte rmd[5 + RIPEMD160_DIGEST_LENGTH];
     26 
     27     int j;
     28     for (j = 0; j < 65; j++) {
     29         s[j] = pubkey64[j];
     30     }
     31 
     32     rmd[0] = 0;
     33     RIPEMD160(SHA256(s, 65, 0), SHA256_DIGEST_LENGTH, rmd + 1);
     34 
     35     memcpy(rmd + 21, SHA256(SHA256(rmd, 21, 0), SHA256_DIGEST_LENGTH, 0), 4);
     36 
     37     base58(rmd, 25, out, 34);
     38 
     39     /* Count the number of extra 1s at the beginning of the address */
     40     int k;
     41     for (k = 1; out[k] == '1'; k++)
     42         ;
     43 
     44     /* Count the number of extra leading 0x00 bytes */
     45     int n;
     46     for (n = 1; rmd[n] == 0x00; n++)
     47         ;
     48 
     49     /* Remove k-n leading 1's from the address */
     50     memmove(out, out + (k - n), 34 - (k - n));
     51     out[34 - (k - n)] = '\0';
     52 }