Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
# create some arbitrary data used by the nonce generation
ndata = secp256k1_ffi.new("const int *ndata")
ndata[0] = 0 # it adds "\0x00", then "\0x00\0x00", etc..
while True: # repeat process until deterministic and cannonical
ndata[0] += 1 # increment the arbitrary nonce
# obtain compiled/binary private key from the wif
privkey = secp256k1_PrivateKey(p, raw=True)
print(it('red',str(privkey)))
print(privkey)
# create a new recoverable 65 byte ECDSA signature
sig = secp256k1_ffi.new(
"secp256k1_ecdsa_recoverable_signature *"
)
# parse a compact ECDSA signature (64 bytes + recovery id)
# returns: 1 = deterministic; 0 = not deterministic
deterministic = secp256k1_lib.secp256k1_ecdsa_sign_recoverable(
privkey.ctx, # initialized context object
sig, # array where signature is held
digest, # 32-byte message hash being signed
privkey.private_key, # 32-byte secret key
secp256k1_ffi.NULL, # default nonce function
ndata, # incrementing nonce data
)
if not deterministic:
print("not deterministic, try again...")
continue
# we derive the recovery paramter
# which simplifies the verification of the signature
# it links the signature to a single unique public key
# without this parameter, the back-end would need to test
# for multiple public keys instead of just one
signature, i = privkey.ecdsa_recoverable_serialize(sig)
tx_hex = self.client.get_transaction_hex(self.transaction)
self.derive_digest(chain, tx_hex)
sigs = []
for wif in self.client.keys:
p = compat_bytes(PrivateKey(wif))
i = 0
if USE_SECP256K1:
ndata = secp256k1.ffi.new("const int *ndata")
ndata[0] = 0
while True:
ndata[0] += 1
privkey = secp256k1.PrivateKey(p, raw=True)
sig = secp256k1.ffi.new(
'secp256k1_ecdsa_recoverable_signature *')
signed = secp256k1.lib.secp256k1_ecdsa_sign_recoverable(
privkey.ctx, sig, self.digest, privkey.private_key,
secp256k1.ffi.NULL, ndata)
assert signed == 1
signature, i = privkey.ecdsa_recoverable_serialize(sig)
if self._is_canonical(signature):
i += 4
i += 27
break
else:
cnt = 0
sk = ecdsa.SigningKey.from_string(p, curve=ecdsa.SECP256k1)
while 1:
cnt += 1
if not cnt % 20:
print("Still searching for a canonical signature. "
"Tried %d times already!" % cnt)
if not isinstance(message, bytes):
message = bytes(message, "utf-8")
digest = hashfn(message).digest()
priv_key = PrivateKey(wif)
p = bytes(priv_key)
if SECP256K1_MODULE == "secp256k1":
ndata = secp256k1.ffi.new("const int *ndata")
ndata[0] = 0
while True:
ndata[0] += 1
privkey = secp256k1.PrivateKey(p, raw=True)
sig = secp256k1.ffi.new("secp256k1_ecdsa_recoverable_signature *")
signed = secp256k1.lib.secp256k1_ecdsa_sign_recoverable(
privkey.ctx, sig, digest, privkey.private_key, secp256k1.ffi.NULL, ndata
)
if not signed == 1: # pragma: no cover
raise AssertionError()
signature, i = privkey.ecdsa_recoverable_serialize(sig)
if _is_canonical(signature):
i += 4 # compressed
i += 27 # compact
break
elif SECP256K1_MODULE == "cryptography":
cnt = 0
private_key = ec.derive_private_key(
int(repr(priv_key), 16), ec.SECP256K1(), default_backend()
)
public_key = private_key.public_key()
while True:
if not isinstance(message, bytes):
message = bytes(message, "utf-8")
digest = hashfn(message).digest()
priv_key = PrivateKey(wif)
p = bytes(priv_key)
if SECP256K1_MODULE == "secp256k1":
ndata = secp256k1.ffi.new("const int *ndata")
ndata[0] = 0
while True:
ndata[0] += 1
privkey = secp256k1.PrivateKey(p, raw=True)
sig = secp256k1.ffi.new("secp256k1_ecdsa_recoverable_signature *")
signed = secp256k1.lib.secp256k1_ecdsa_sign_recoverable(
privkey.ctx, sig, digest, privkey.private_key, secp256k1.ffi.NULL, ndata
)
if not signed == 1: # pragma: no cover
raise AssertionError()
signature, i = privkey.ecdsa_recoverable_serialize(sig)
if _is_canonical(signature):
i += 4 # compressed
i += 27 # compact
break
elif SECP256K1_MODULE == "cryptography":
cnt = 0
private_key = ec.derive_private_key(
int(repr(priv_key), 16), ec.SECP256K1(), default_backend()
)
public_key = private_key.public_key()
while True:
]
# Sign the message with every private key given!
sigs = []
for wif in self.privkeys:
p = future_bytes(PrivateKey(wif))
i = 0
if USE_SECP256K1:
ndata = secp256k1.ffi.new("const int *ndata")
ndata[0] = 0
while True:
ndata[0] += 1
privkey = secp256k1.PrivateKey(p, raw=True)
sig = secp256k1.ffi.new(
'secp256k1_ecdsa_recoverable_signature *')
signed = secp256k1.lib.secp256k1_ecdsa_sign_recoverable(
privkey.ctx, sig, self.digest, privkey.private_key,
secp256k1.ffi.NULL, ndata)
assert signed == 1
signature, i = privkey.ecdsa_recoverable_serialize(sig)
if self._is_canonical(signature):
i += 4 # compressed
i += 27 # compact
break
else:
cnt = 0
sk = ecdsa.SigningKey.from_string(p, curve=ecdsa.SECP256k1)
while 1:
cnt += 1
if not cnt % 20:
log.info("Still searching for a canonical signature. "
"Tried %d times already!" % cnt)