static void keytest() { { struct bp_key k; bp_key_init(&k); bp_key_free(&k); } // Signature { const uint8_t test_secret[32] = { 0x1 }; const uint8_t test_data[] = { 1, 2, 3, 4, 5, 6, 7, 8 }; bu256_t hash; SHA256(test_data, sizeof(test_data), (uint8_t *)&hash); void *pub = NULL; size_t publen = 0; void *sig = NULL; size_t siglen = 0; struct bp_key k; bp_key_init(&k); assert(bp_key_secret_set(&k, test_secret, sizeof(test_secret))); assert(bp_pubkey_get(&k, &pub, &publen)); assert(NULL != pub); assert(0 != publen); assert(bp_sign(&k, (uint8_t *)&hash, sizeof(hash), &sig, &siglen)); assert(NULL != sig); assert(0 != siglen); bp_key_free(&k); struct bp_key pubk; bp_key_init(&k); assert(bp_pubkey_set(&pubk, pub, publen)); assert(bp_verify(&pubk, (uint8_t *)&hash, sizeof(hash), sig, siglen)); bp_key_free(&k); free(pub); free(sig); } }
static bool sign1(const bu160_t *key_id, struct bp_keystore *ks, const bu256_t *hash, int nHashType, cstring *scriptSig) { struct bp_key key; bool rc = false; bp_key_init(&key); /* find private key in keystore */ if (!bkeys_key_get(ks, key_id, &key)) goto out; void *sig = NULL; size_t siglen = 0; /* sign hash with private key */ if (!bp_sign(&key, hash, sizeof(*hash), &sig, &siglen)) goto out; /* append nHashType to signature */ unsigned char ch = (unsigned char) nHashType; sig = realloc(sig, siglen + 1); memcpy(sig + siglen, &ch, 1); siglen++; /* append signature to scriptSig */ bsp_push_data(scriptSig, sig, siglen); free(sig); rc = true; out: bp_key_free(&key); return rc; }