Example #1
0
    def sign_transaction_dict(self, transaction_dict):
        # generate RLP-serializable transaction, with defaults filled
        unsigned_transaction = serializable_unsigned_transaction_from_dict(
            transaction_dict)

        transaction_hash = unsigned_transaction.hash()

        # detect chain
        if isinstance(unsigned_transaction, BcosUnsignedTransaction):
            chain_id = None
        else:
            chain_id = unsigned_transaction.v

        # sign with private key
        (v, r, s) = self.sign_transaction_hash(transaction_hash, chain_id)

        # serialize transaction with rlp
        encoded_transaction = encode_transaction(unsigned_transaction,
                                                 vrs=(v, r, s))

        return (v, r, s, encoded_transaction)
Example #2
0
contractAddress = ""  # empty for deloy
txmap = dict()
txmap["randomid"] = random.randint(0, 1000000000)  # 测试用 todo:改为随机数
txmap["gasPrice"] = 30000000
txmap["gasLimit"] = 30000000
txmap["blockLimit"] = 501  # 测试用,todo:从链上查一下
txmap["to"] = contractAddress
txmap["value"] = 0
txmap["data"] = functiondata
txmap["fiscoChainId"] = 1
txmap["groupId"] = 1
txmap["extraData"] = ""
# txmap["chainId"]=None #chainId没用了,fiscoChainId有用
print(txmap)
# 将mapping构建一个transaction对象,非必要,用来对照的
transaction = serializable_unsigned_transaction_from_dict(txmap)
# 感受下transaction encode的原始数据
print(encode_hex(rlp.encode(transaction)))

# 实际上只需要用sign_transaction就可以获得rawTransaction的编码数据了,input :txmap,私钥
signedTxResult = Account.sign_transaction(txmap, ac2.privateKey)
print(signedTxResult)
# signedTxResult.rawTransaction是二进制的,要放到rpc接口里要encode下
print(encode_hex(signedTxResult.rawTransaction))

url = "http://119.23.46.126:8545"
rpc = utils.rpc.HTTPProvider(url)
if True:
    param = [1, encode_hex(signedTxResult.rawTransaction)]
    # 发送
    response = rpc.make_request("sendRawTransaction", param)