niceBit

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

walletImportFormat.h (1164B)


      1 #include <openssl/ripemd.h>
      2 #include <openssl/sha.h>
      3 #include <stdio.h>
      4 
      5 /* https://en.bitcoin.it/wiki/Wallet_import_format */
      6 
      7 char *create_wif(const unsigned char *privatekey) {
      8     unsigned char newKey[65];
      9     unsigned char SHAkey[65];
     10     unsigned char SHAkey2[65];
     11     unsigned char checksum[11];
     12     unsigned char combinedKey[75];
     13 
     14     size_t combinedKeySize = 37;
     15     size_t wifSize = 51;
     16     char wif[51];
     17 
     18     /* Add 0x80 byte in front */
     19     newKey[0] = 128;
     20     for (int i = 0; i < 32; i++) {
     21         newKey[i + 1] = privatekey[i];
     22     }
     23 
     24     /* Perform SHA-256 hash on the extended key */
     25     SHA256(newKey, 33, SHAkey);
     26 
     27     /* Perform SHA-256 hash again on the result */
     28     SHA256(SHAkey, 32, SHAkey2);
     29 
     30     /* Checksum is first 4 bytes of 2nd SHA*/
     31     for (int i = 0; i < 4; i++) {
     32         checksum[i] = SHAkey2[i];
     33     }
     34 
     35     /* Append checksum to end of 2nd SHA */
     36     for (int i = 0; i < 33; i++) {
     37         combinedKey[i] = newKey[i];
     38     }
     39     for (int i = 0; i < 4; i++) {
     40         combinedKey[33 + i] = checksum[i];
     41     }
     42 
     43     /* Encode with base-58 */
     44     base58(combinedKey, combinedKeySize, wif, wifSize);
     45     puts(wif);
     46 
     47     return 0;
     48 }