Skip to content

Add key generation support #41

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions bitcoin/core/key.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,26 @@ def set_secretbytes(self, secret):
_ssl.BN_CTX_free(ctx)
return self.k

def get_secretbytes(self):
secret = _ssl.EC_KEY_get0_private_key(self.k)
mb = ctypes.create_string_buffer(32)
size = _ssl.BN_bn2bin(secret, mb)
if size == 32:
return mb.raw
else:
# Move the data into a zero-padded buffer of 32 bytes
padding = 32 - size
new_buffer = ctypes.create_string_buffer(32)
for idx in range(0, padding):
new_buffer[idx] = "\x00"
for idx in range(padding, 32):
new_buffer[idx] = mb[idx - padding]
return new_buffer.raw

def generate(self):
_ssl.EC_KEY_generate_key(self.k)
return self.k

def set_privkey(self, key):
self.mb = ctypes.create_string_buffer(key)
return _ssl.d2i_ECPrivateKey(ctypes.byref(self.k), ctypes.byref(ctypes.pointer(self.mb)), len(key))
Expand Down