本文整理汇总了Python中bitcoin.core.script.SIGHASH_ALL属性的典型用法代码示例。如果您正苦于以下问题:Python script.SIGHASH_ALL属性的具体用法?Python script.SIGHASH_ALL怎么用?Python script.SIGHASH_ALL使用的例子?那么, 这里精选的属性代码示例或许可以为您提供帮助。您也可以进一步了解该属性所在类bitcoin.core.script
的用法示例。
在下文中一共展示了script.SIGHASH_ALL属性的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: sign
# 需要导入模块: from bitcoin.core import script [as 别名]
# 或者: from bitcoin.core.script import SIGHASH_ALL [as 别名]
def sign(self, default_wallet: BitcoinWallet =None):
"""Signing transaction using the wallet object."""
for tx_index, tx_in in enumerate(self.tx.vin):
utxo = self.solvable_utxo[tx_index]
wallet = utxo.wallet or default_wallet
if wallet is None:
raise RuntimeError('Cannot sign transaction without a wallet.')
tx_script = utxo.parsed_script
if utxo.contract:
sig_hash = script.SignatureHash(
script.CScript.fromhex(utxo.contract),
self.tx,
tx_index,
script.SIGHASH_ALL
)
else:
sig_hash = script.SignatureHash(
tx_script,
self.tx,
tx_index,
script.SIGHASH_ALL
)
sig = wallet.private_key.sign(sig_hash) + struct.pack('<B', script.SIGHASH_ALL)
script_sig = [sig, wallet.private_key.pub] + utxo.unsigned_script_sig
tx_in.scriptSig = script.CScript(script_sig)
VerifyScript(
tx_in.scriptSig,
tx_script,
self.tx,
tx_index,
(SCRIPT_VERIFY_P2SH,)
)
self.signed = True
示例2: sign
# 需要导入模块: from bitcoin.core import script [as 别名]
# 或者: from bitcoin.core.script import SIGHASH_ALL [as 别名]
def sign(self, privkey):
"""
Sign each of the inputs with the private key. Inputs should all be sent to
the same scriptPubkey so we should only need one key.
"""
seckey = CBitcoinSecret.from_secret_bytes(x(bitcointools.encode_privkey(privkey, "hex")))
for i in range(len(self.tx.vin)):
txin_scriptPubKey = self.tx.vin[i].scriptSig
sighash = SignatureHash(txin_scriptPubKey, self.tx, i, SIGHASH_ALL)
sig = seckey.sign(sighash) + struct.pack('<B', SIGHASH_ALL)
self.tx.vin[i].scriptSig = CScript([sig, seckey.pub])
VerifyScript(self.tx.vin[i].scriptSig, txin_scriptPubKey, self.tx, i, (SCRIPT_VERIFY_P2SH,))
示例3: create_signature
# 需要导入模块: from bitcoin.core import script [as 别名]
# 或者: from bitcoin.core.script import SIGHASH_ALL [as 别名]
def create_signature(self, privkey, reedem_script):
"""
Exports a raw signature suitable for use in a multisig transaction
"""
seckey = CBitcoinSecret.from_secret_bytes(x(bitcointools.encode_privkey(privkey, "hex")))
signatures = []
for i in range(len(self.tx.vin)):
sighash = SignatureHash(CScript(x(reedem_script)), self.tx, i, SIGHASH_ALL)
signatures.append({
"index": i,
"signature": (seckey.sign(sighash) + struct.pack('<B', SIGHASH_ALL)).encode("hex"),
"outpoint": b2lx(self.tx.vin[i].prevout.hash) + b2lx(struct.pack(b"<I", self.tx.vin[i].prevout.n))
})
return signatures
示例4: ecdsa_tx_sign
# 需要导入模块: from bitcoin.core import script [as 别名]
# 或者: from bitcoin.core.script import SIGHASH_ALL [as 别名]
def ecdsa_tx_sign(unsigned_tx, sk, hashflag=SIGHASH_ALL, deterministic=True):
""" Performs and ECDSA sign over a given transaction using a given secret key.
:param unsigned_tx: unsigned transaction that will be double-sha256 and signed.
:type unsigned_tx: hex str
:param sk: ECDSA private key that will sign the transaction.
:type sk: SigningKey
:param hashflag: hash type that will be used during the signature process and will identify the signature format.
:type hashflag: int
:param deterministic: Whether the signature is performed using a deterministic k or not. Set by default.
:type deterministic: bool
:return:
"""
# Encode the hash type as a 4-byte hex value.
if hashflag in [SIGHASH_ALL, SIGHASH_SINGLE, SIGHASH_NONE]:
hc = int2bytes(hashflag, 4)
else:
raise Exception("Wrong hash flag.")
# ToDo: Deal with SIGHASH_ANYONECANPAY
# sha-256 the unsigned transaction together with the hash type (little endian).
h = sha256(unhexlify(unsigned_tx + change_endianness(hc))).digest()
# Sign the transaction (using a sha256 digest, that will conclude with the double-sha256)
# If deterministic is set, the signature will be performed deterministically choosing a k from the given transaction
if deterministic:
s = sk.sign_deterministic(h, hashfunc=sha256, sigencode=sigencode_der_canonize)
# Otherwise, k will be chosen at random. Notice that this can lead to a private key disclosure if two different
# messages are signed using the same k.
else:
s = sk.sign(h, hashfunc=sha256, sigencode=sigencode_der_canonize)
# Finally, add the hashtype to the end of the signature as a 2-byte big endian hex value.
return hexlify(s) + hc[-2:]
示例5: signature
# 需要导入模块: from bitcoin.core import script [as 别名]
# 或者: from bitcoin.core.script import SIGHASH_ALL [as 别名]
def signature(self, transaction):
"""Signature for a transaction."""
sighash = SignatureHash(CScript(self.anchor_redeem),
transaction, 0, SIGHASH_ALL)
sig = g.seckey.sign(sighash) + bytes([SIGHASH_ALL])
return sig
示例6: sig_hash_explanation
# 需要导入模块: from bitcoin.core import script [as 别名]
# 或者: from bitcoin.core.script import SIGHASH_ALL [as 别名]
def sig_hash_explanation(hash_type):
"""Return a description of a hash type.
Explanations taken from https://bitcoin.org/en/developer-guide#signature-hash-types.
"""
explanations = {
SIGHASH_ALL: 'Signs all the inputs and outputs, protecting everything except the signature scripts against modification.',
SIGHASH_NONE: 'Signs all of the inputs but none of the outputs, allowing anyone to change where the satoshis are going unless other signatures using other signature hash flags protect the outputs.',
SIGHASH_SINGLE: 'The only output signed is the one corresponding to this input (the output with the same output index number as this input), ensuring nobody can change your part of the transaction but allowing other signers to change their part of the transaction. The corresponding output must exist or the value "1" will be signed, breaking the security scheme. This input, as well as other inputs, are included in the signature. The sequence numbers of other inputs are not included in the signature, and can be updated.',
SIGHASH_ALL | SIGHASH_ANYONECANPAY: 'Signs all of the outputs but only this one input, and it also allows anyone to add or remove other inputs, so anyone can contribute additional satoshis but they cannot change how many satoshis are sent nor where they go.',
SIGHASH_NONE | SIGHASH_ANYONECANPAY: 'Signs only this one input and allows anyone to add or remove other inputs or outputs, so anyone who gets a copy of this input can spend it however they\'d like.',
SIGHASH_SINGLE | SIGHASH_ANYONECANPAY: 'Signs this one input and its corresponding output. Allows anyone to add or remove other inputs.',
}
return explanations.get(hash_type)
示例7: clear
# 需要导入模块: from bitcoin.core import script [as 别名]
# 或者: from bitcoin.core.script import SIGHASH_ALL [as 别名]
def clear(self):
self.beginResetModel()
self.utxo_script = None
self.tx = None
self.inIdx = 0
self.sighash_type = SIGHASH_ALL
self.anyone_can_pay = False
self.endResetModel()