static bool bp_checksig(const struct buffer *vchSigIn, const struct buffer *vchPubKey, const cstring *scriptCode, const struct bp_tx *txTo, unsigned int nIn) { if (!vchSigIn || !vchPubKey || !scriptCode || !txTo || !vchSigIn->len || !vchPubKey->len || !scriptCode->len) return false; // Hash type is one byte tacked on to the end of the signature unsigned char *vch_back = vchSigIn->p + (vchSigIn->len - 1); int nHashType = *vch_back; struct buffer vchSig = { vchSigIn->p, vchSigIn->len - 1 }; /* calculate signature hash of transaction */ bu256_t sighash; bp_tx_sighash(&sighash, scriptCode, txTo, nIn, nHashType); /* verify signature hash */ struct bp_key pubkey; bp_key_init(&pubkey); bool rc = false; if (!bp_pubkey_set(&pubkey, vchPubKey->p, vchPubKey->len)) goto out; if (!bp_verify(&pubkey, &sighash, sizeof(sighash), vchSig.p, vchSig.len)) goto out; rc = true; out: bp_key_free(&pubkey); return rc; }
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 bp_checksig(const struct buffer *vchSigHT, const struct buffer *vchPubKey, const GString *scriptCode, const struct bp_tx *txTo, unsigned int nIn, int nHashType) { if (!vchSigHT || !vchPubKey || !scriptCode || !txTo || !vchSigHT->len || !vchPubKey->len || !scriptCode->len) return false; /* examine hashtype at end of string, then remove it */ unsigned char *vch_back = vchSigHT->p + (vchSigHT->len - 1); if (nHashType == 0) nHashType = *vch_back; else if (nHashType != *vch_back) return false; struct buffer vchSig = { vchSigHT->p, vchSigHT->len - 1 }; /* calculate signature hash of transaction */ bu256_t sighash; bp_tx_sighash(&sighash, scriptCode, txTo, nIn, nHashType); /* verify signature hash */ struct bp_key key; bp_key_init(&key); bool rc = false; if (!bp_pubkey_set(&key, vchPubKey->p, vchPubKey->len)) goto out; if (!bp_verify(&key, &sighash, sizeof(sighash), vchSig.p, vchSig.len)) goto out; rc = true; out: bp_key_free(&key); return rc; }