def sign(to_sign, private_key, options, chain_name='mainnet'): import copy copy = copy.deepcopy(to_sign) if 'signature' in copy: del copy['signature'] # normalize and get data to hash normalized = normalize_jsonld(to_sign) to_hash = _getDataToHash(normalized, options=options) # TODO: obtain lock while modifying global state bitcoin.SelectParams(chain_name) message = BitcoinMessage(to_hash) secret_key = CBitcoinSecret(private_key) signature = SignMessage(secret_key, message) # compact just signature part against all contexts signature_payload = { '@context': SECURITY_CONTEXT_URL, 'type': algorithm, 'creator': options.creator, 'created': options.created, 'signatureValue': signature.decode('utf-8') } tmp = {'https://w3id.org/security#signature': signature_payload} prev_contexts = JsonLdProcessor.get_values(to_sign, '@context') if not SECURITY_CONTEXT_URL in prev_contexts: prev_contexts.append(SECURITY_CONTEXT_URL) c = {'@context': prev_contexts} res = jsonld.compact(tmp, c, options={'documentLoader': cached_document_loader}) copy['@context'] = prev_contexts copy['signature'] = res['signature'] return copy
# This file is part of python-bitcoinlib. # # It is subject to the license terms in the LICENSE file found in the top-level # directory of this distribution. # # No part of python-bitcoinlib, including this file, may be copied, modified, # propagated, or distributed except according to the terms contained in the # LICENSE file. from __future__ import absolute_import, division, print_function, unicode_literals from bitcoin.wallet import CBitcoinSecret, P2PKHBitcoinAddress from bitcoin.signmessage import BitcoinMessage, VerifyMessage, SignMessage key = CBitcoinSecret("L4vB5fomsK8L95wQ7GFzvErYGht49JsCPJyJMHpB4xGM6xgi2jvG") address = P2PKHBitcoinAddress.from_pubkey(key.pub) # "1F26pNMrywyZJdr22jErtKcjF8R3Ttt55G" message = "Hey I just met you, and this is crazy, but I'll verify my address, maybe ..." message = BitcoinMessage(message) signature = SignMessage(key, message) print(key, address) print("Address: %s" % address) print("Message: %s" % message) print("\nSignature: %s" % signature) print("\nVerified: %s" % VerifyMessage(address, message, signature)) print("\nTo verify using bitcoin core;") print("`bitcoin-cli verifymessage %s \"%s\" \"%s\"`" % (address, signature.decode('ascii'), message))
# It is subject to the license terms in the LICENSE file found in the top-level # directory of this distribution. # # No part of python-bitcoinlib, including this file, may be copied, modified, # propagated, or distributed except according to the terms contained in the # LICENSE file. from __future__ import absolute_import, division, print_function, unicode_literals from bitcoin.wallet import CBitcoinSecret, P2PKHBitcoinAddress from bitcoin.signmessage import BitcoinMessage, VerifyMessage, SignMessage key = CBitcoinSecret("L4vB5fomsK8L95wQ7GFzvErYGht49JsCPJyJMHpB4xGM6xgi2jvG") address = P2PKHBitcoinAddress.from_pubkey( key.pub) # "1F26pNMrywyZJdr22jErtKcjF8R3Ttt55G" message = "Hey I just met you, and this is crazy, but I'll verify my address, maybe ..." message = BitcoinMessage(message) signature = SignMessage(key, message) print(key, address) print("Address: %s" % address) print("Message: %s", message) print("\nSignature: %s" % signature) print("\nVerified: %s" % VerifyMessage(address, message, signature)) print("\nTo verify using bitcoin core;") print("`bitcoin-cli verifymessage %s \"%s\" \"%s\"`" % (address, signature.decode('ascii'), message))