コード例 #1
0
ファイル: transaction.py プロジェクト: topiasv/BitPurse
 def multisig_script(self, public_keys):
     # supports only "2 of 2", and "2 of 3" transactions
     n = len(public_keys)
     s = '52'
     for k in public_keys:
         s += var_int(len(k) / 2)
         s += k
     if n == 2:
         s += '52'
     elif n == 3:
         s += '53'
     else:
         raise
     s += 'ae'
     return s
コード例 #2
0
ファイル: transaction.py プロジェクト: donSchoe/BitPurse
 def multisig_script(self, public_keys):
     # supports only "2 of 2", and "2 of 3" transactions
     n = len(public_keys)
     s = '52'
     for k in public_keys:
         s += var_int(len(k) / 2)
         s += k
     if n == 2:
         s += '52'
     elif n == 3:
         s += '53'
     else:
         raise
     s += 'ae'
     return s
コード例 #3
0
ファイル: transaction.py プロジェクト: topiasv/BitPurse
    def raw_tx(self, inputs, outputs, for_sig=None):
        s = int_to_hex(1, 4)  # version
        s += var_int(len(inputs))  # number of inputs
        for i in range(len(inputs)):
            _, _, p_hash, p_index, p_script, pubkeysig = inputs[i]
            s += p_hash.decode('hex')[::-1].encode('hex')  # prev hash
            s += int_to_hex(p_index, 4)  # prev index

            if for_sig is None:
                if len(pubkeysig) == 1:
                    pubkey, sig = pubkeysig[0]
                    sig = sig + chr(1)  # hashtype
                    script = int_to_hex(len(sig))
                    script += sig.encode('hex')
                    script += int_to_hex(len(pubkey))
                    script += pubkey.encode('hex')
                else:
                    pubkey0, sig0 = pubkeysig[0]
                    pubkey1, sig1 = pubkeysig[1]
                    sig0 = sig0 + chr(1)
                    sig1 = sig1 + chr(1)
                    inner_script = self.multisig_script([pubkey0, pubkey1])
                    script = '00'  # op_0
                    script += int_to_hex(len(sig0))
                    script += sig0.encode('hex')
                    script += int_to_hex(len(sig1))
                    script += sig1.encode('hex')
                    script += var_int(len(inner_script) / 2)
                    script += inner_script

            elif for_sig == i:
                if len(pubkeysig) > 1:
                    script = self.multisig_script(pubkeysig)  # p2sh uses the
                    # inner script
                else:
                    script = p_script  # scriptsig
            else:
                script = ''
            s += var_int(len(self.tx_filter(script)) / 2)  # script length
            s += script
            s += "ffffffff"  # sequence

        s += var_int(len(outputs))  # number of outputs
        for output in outputs:
            addr, amount = output
            s += int_to_hex(amount, 8)  # amount
            addrtype, hash_160 = bc_address_to_hash_160(addr)
            if addrtype == 0:
                script = '76a9'  # op_dup, op_hash_160
                script += '14'  # push 0x14 bytes
                script += hash_160.encode('hex')
                script += '88ac'  # op_equalverify, op_checksig
            elif addrtype == 5:
                script = 'a9'  # op_hash_160
                script += '14'  # push 0x14 bytes
                script += hash_160.encode('hex')
                script += '87'  # op_equal
            else:
                raise

            s += var_int(len(self.tx_filter(script)) / 2)  # script length
            s += script  # script
        s += int_to_hex(0, 4)  # lock time
        if for_sig is not None:
            s += int_to_hex(1, 4)  # hash type
        return self.tx_filter(s)
コード例 #4
0
ファイル: transaction.py プロジェクト: donSchoe/BitPurse
    def raw_tx(self, inputs, outputs, for_sig=None):
        s = int_to_hex(1, 4)                                 # version
        s += var_int(len(inputs))                            # number of inputs
        for i in range(len(inputs)):
            _, _, p_hash, p_index, p_script, pubkeysig = inputs[i]
            s += p_hash.decode('hex')[::-1].encode('hex')    # prev hash
            s += int_to_hex(p_index, 4)                    # prev index

            if for_sig is None:
                if len(pubkeysig) == 1:
                    pubkey, sig = pubkeysig[0]
                    sig = sig + chr(1)                               # hashtype
                    script = int_to_hex(len(sig))
                    script += sig.encode('hex')
                    script += int_to_hex(len(pubkey))
                    script += pubkey.encode('hex')
                else:
                    pubkey0, sig0 = pubkeysig[0]
                    pubkey1, sig1 = pubkeysig[1]
                    sig0 = sig0 + chr(1)
                    sig1 = sig1 + chr(1)
                    inner_script = self.multisig_script([pubkey0, pubkey1])
                    script = '00'                                    # op_0
                    script += int_to_hex(len(sig0))
                    script += sig0.encode('hex')
                    script += int_to_hex(len(sig1))
                    script += sig1.encode('hex')
                    script += var_int(len(inner_script) / 2)
                    script += inner_script

            elif for_sig == i:
                if len(pubkeysig) > 1:
                    script = self.multisig_script(pubkeysig)  # p2sh uses the
                                                              # inner script
                else:
                    script = p_script                         # scriptsig
            else:
                script = ''
            s += var_int(len(self.tx_filter(script)) / 2)    # script length
            s += script
            s += "ffffffff"                                   # sequence

        s += var_int(len(outputs))                      # number of outputs
        for output in outputs:
            addr, amount = output
            s += int_to_hex(amount, 8)                              # amount
            addrtype, hash_160 = bc_address_to_hash_160(addr)
            if addrtype == 0:
                script = '76a9'                         # op_dup, op_hash_160
                script += '14'                          # push 0x14 bytes
                script += hash_160.encode('hex')
                script += '88ac'                 # op_equalverify, op_checksig
            elif addrtype == 5:
                script = 'a9'                           # op_hash_160
                script += '14'                          # push 0x14 bytes
                script += hash_160.encode('hex')
                script += '87'                          # op_equal
            else:
                raise

            s += var_int(len(self.tx_filter(script)) / 2)  # script length
            s += script                                    # script
        s += int_to_hex(0, 4)                               # lock time
        if for_sig is not None:
            s += int_to_hex(1, 4)      # hash type
        return self.tx_filter(s)