Address toAddress(Secret _private) { secp256k1_start(); byte pubkey[65]; int pubkeylen = 65; int ok = secp256k1_ecdsa_seckey_verify(_private.data()); if (!ok) return Address(); ok = secp256k1_ecdsa_pubkey_create(pubkey, &pubkeylen, _private.data(), 0); assert(pubkeylen == 65); if (!ok) return Address(); ok = secp256k1_ecdsa_pubkey_verify(pubkey, 65); if (!ok) return Address(); auto ret = right160(dev::eth::sha3(bytesConstRef(&(pubkey[1]), 64))); #if ETH_ADDRESS_DEBUG cout << "---- ADDRESS -------------------------------" << endl; cout << "SEC: " << _private << endl; cout << "PUB: " << toHex(bytesConstRef(&(pubkey[1]), 64)) << endl; cout << "ADR: " << ret << endl; #endif return ret; }
int pass2hash160(unsigned char *pass, size_t pass_sz) { /* only initialize stuff once */ static int bwc_is_init = 0; if (!bwc_is_init) { /* initialize buffers */ mem = malloc(4096); /* initialize hashs */ sha256_ctx = malloc(sizeof(*sha256_ctx)); ripemd160_ctx = malloc(sizeof(*ripemd160_ctx)); /* set the flag */ bwc_is_init = 1; } unsigned char *pub_chr = mem; int pub_chr_sz; SHA256_Init(sha256_ctx); SHA256_Update(sha256_ctx, pass, pass_sz); SHA256_Final(hash256, sha256_ctx); secp256k1_ecdsa_pubkey_create(pub_chr, &pub_chr_sz, hash256, 0); #if 0 i = 0; for (i = 0; i < pub_chr_sz; i++) { printf("%02x", pub_chr[i]); } printf("\n"); #endif /* yo dawg, i heard you like hashes... */ SHA256_Init(sha256_ctx); SHA256_Update(sha256_ctx, pub_chr, pub_chr_sz); SHA256_Final(hash256, sha256_ctx); /* ...so i put a hash in your hash */ RIPEMD160_Init(ripemd160_ctx); RIPEMD160_Update(ripemd160_ctx, hash256, SHA256_DIGEST_LENGTH); RIPEMD160_Final(hash160_tmp.uc, ripemd160_ctx); memcpy(hash160_uncmp.uc, hash160_tmp.uc, 20); /* ugly key compression hack */ pub_chr[0] = 0x02 | (pub_chr[64] & 0x01); /* yo dawg, i heard you like hashes... */ SHA256_Init(sha256_ctx); SHA256_Update(sha256_ctx, pub_chr, 33); SHA256_Final(hash256, sha256_ctx); /* ...so i put a hash in your hash */ RIPEMD160_Init(ripemd160_ctx); RIPEMD160_Update(ripemd160_ctx, hash256, SHA256_DIGEST_LENGTH); RIPEMD160_Final(hash160_tmp.uc, ripemd160_ctx); memcpy(hash160_compr.uc, hash160_tmp.uc, 20); return 0; }
inline static int priv2hash160(unsigned char *priv) { /* only initialize stuff once */ if (!brainflayer_is_init) { /* initialize buffers */ mem = malloc(4096); /* initialize hashs */ sha256_ctx = malloc(sizeof(*sha256_ctx)); ripemd160_ctx = malloc(sizeof(*ripemd160_ctx)); /* set the flag */ brainflayer_is_init = 1; } unsigned char *pub_chr = mem; int pub_chr_sz; secp256k1_ecdsa_pubkey_create(pub_chr, &pub_chr_sz, priv, 0); #if 0 i = 0; for (i = 0; i < pub_chr_sz; i++) { printf("%02x", pub_chr[i]); } printf("\n"); #endif /* compute hash160 for uncompressed public key */ /* sha256(pub) */ SHA256_Init(sha256_ctx); SHA256_Update(sha256_ctx, pub_chr, pub_chr_sz); SHA256_Final(hash256, sha256_ctx); /* ripemd160(sha256(pub)) */ RIPEMD160_Init(ripemd160_ctx); RIPEMD160_Update(ripemd160_ctx, hash256, SHA256_DIGEST_LENGTH); RIPEMD160_Final(hash160_tmp.uc, ripemd160_ctx); /* save result to global struct */ memcpy(hash160_uncmp.uc, hash160_tmp.uc, 20); /* quick and dirty public key compression */ pub_chr[0] = 0x02 | (pub_chr[64] & 0x01); /* compute hash160 for compressed public key */ /* sha256(pub) */ SHA256_Init(sha256_ctx); SHA256_Update(sha256_ctx, pub_chr, 33); SHA256_Final(hash256, sha256_ctx); /* ripemd160(sha256(pub)) */ RIPEMD160_Init(ripemd160_ctx); RIPEMD160_Update(ripemd160_ctx, hash256, SHA256_DIGEST_LENGTH); RIPEMD160_Final(hash160_tmp.uc, ripemd160_ctx); /* save result to global struct */ memcpy(hash160_compr.uc, hash160_tmp.uc, 20); return 0; }
ec_point secret_to_public_key(const ec_secret& secret, bool compressed) { init.init(); size_t size = ec_uncompressed_size; if (compressed) size = ec_compressed_size; ec_point out(size); int out_size; if (!secp256k1_ecdsa_pubkey_create(out.data(), &out_size, secret.data(), compressed)) return ec_point(); BITCOIN_ASSERT(size == static_cast<size_t>(out_size)); return out; }