Пример #1
0
int main()
{
    btc_ecc_start();

    u_run_test(test_random);
    u_run_test(test_bitcoin_hash);
    u_run_test(test_base58check);
    u_run_test(test_aes);

    u_run_test(test_bip32);
    u_run_test(test_ecc);
    u_run_test(test_vector);
    u_run_test(test_tx_serialization);
    u_run_test(test_tx_sighash);
    u_run_test(test_tx_negative_version);
    u_run_test(test_block_header);
    u_run_test(test_script_parse);
    u_run_test(test_script_op_codeseperator);

    u_run_test(test_eckey);

#ifdef WITH_WALLET
    u_run_test(test_wallet);
#endif

    btc_ecc_stop();
    return 0;
}
Пример #2
0
int main(int argc, char* argv[])
{
    int long_index = 0;
    int opt = 0;
    char* pkey = 0;
    char* pubkey = 0;
    char* cmd = 0;
    char* keypath = 0;
    const btc_chainparams* chain = &btc_chainparams_main;

    /* get arguments */
    while ((opt = getopt_long_only(argc, argv, "p:k:m:c:trv", long_options, &long_index)) != -1) {
        switch (opt) {
        case 'p':
            pkey = optarg;
            if (strlen(pkey) < 50)
                return showError("Private key must be WIF encoded");
            break;
        case 'c':
            cmd = optarg;
            break;
        case 'm':
            keypath = optarg;
            break;
        case 'k':
            pubkey = optarg;
            break;
        case 't':
            chain = &btc_chainparams_test;
            break;
        case 'r':
            chain = &btc_chainparams_regtest;
            break;
        case 'v':
            print_version();
            exit(EXIT_SUCCESS);
            break;
        default:
            print_usage();
            exit(EXIT_FAILURE);
        }
    }

    if (!cmd) {
        /* exit if no command was provided */
        print_usage();
        exit(EXIT_FAILURE);
    }

    /* start ECC context */
    btc_ecc_start();

    const char *pkey_error = "Missing extended key (use -p)";

    if (strcmp(cmd, "pubfrompriv") == 0) {
        /* output compressed hex pubkey from hex privkey */

        size_t sizeout = 128;
        char pubkey_hex[sizeout];
        if (!pkey)
            return showError(pkey_error);
        if (!pubkey_from_privatekey(chain, pkey, pubkey_hex, &sizeout))
            return showError("Operation failed");

        /* clean memory of private key */
        memset(pkey, 0, strlen(pkey));

        /* give out hex pubkey */
        printf("pubkey: %s\n", pubkey_hex);

        /* give out p2pkh address */
        char address[sizeout];
        address_from_pubkey(chain, pubkey_hex, address);
        printf("p2pkh address: %s\n", address);

        /* clean memory */
        memset(pubkey_hex, 0, strlen(pubkey_hex));
        memset(address, 0, strlen(address));
    } else if (strcmp(cmd, "addrfrompub") == 0 || strcmp(cmd, "p2pkhaddrfrompub") == 0) {
        /* get p2pkh address from pubkey */

        size_t sizeout = 128;
        char address[sizeout];
        if (!pubkey)
            return showError("Missing public key (use -k)");
        if (!address_from_pubkey(chain, pubkey, address))
            return showError("Operation failed, invalid pubkey");
        printf("p2pkh address: %s\n", address);
        memset(pubkey, 0, strlen(pubkey));
        memset(address, 0, strlen(address));
    } else if (strcmp(cmd, "genkey") == 0) {
        size_t sizeout = 128;
        char newprivkey_wif[sizeout];
        char newprivkey_hex[sizeout];

        /* generate a new private key */
        gen_privatekey(chain, newprivkey_wif, sizeout, newprivkey_hex);
        printf("privatekey WIF: %s\n", newprivkey_wif);
        printf("privatekey HEX: %s\n", newprivkey_hex);
        memset(newprivkey_wif, 0, strlen(newprivkey_wif));
        memset(newprivkey_hex, 0, strlen(newprivkey_hex));
    } else if (strcmp(cmd, "hdgenmaster") == 0) {
        size_t sizeout = 128;
        char masterkey[sizeout];

        /* generate a new hd master key */
        hd_gen_master(chain, masterkey, sizeout);
        printf("masterkey: %s\n", masterkey);
        memset(masterkey, 0, strlen(masterkey));
    } else if (strcmp(cmd, "hdprintkey") == 0) {
        if (!pkey)
            return showError(pkey_error);
        if (!hd_print_node(chain, pkey))
            return showError("Failed. Probably invalid extended key.\n");
    } else if (strcmp(cmd, "hdderive") == 0) {
        if (!pkey)
            return showError(pkey_error);
        if (!keypath)
            return showError("Missing keypath (use -m)");
        size_t sizeout = 128;
        char newextkey[sizeout];
        if (!hd_derive(chain, pkey, keypath, newextkey, sizeout))
            return showError("Deriving child key failed\n");
        else
            hd_print_node(chain, newextkey);
    }

    btc_ecc_stop();

    return 0;
}
Пример #3
0
static bool showError(const char* er)
{
    printf("Error: %s\n", er);
    btc_ecc_stop();
    return 1;
}