示例#1
0
def transferToExec(title: str, amount: int, execName: str,
                   note: str) -> tx.Transaction:
    """
    transferToExec 将coins执行器下面的代币转移到其他执行器下面
    :param title: 平行链链名前缀 比如user.p.huobi.
    :param amount: 转账金额
    :param execName: 合约名称
    :param note: 备注
    :return: tx.Transaction
    """
    transfer = tx_pb2.AssetsTransferToExec()
    transfer.amount = amount
    transfer.note = bytes(note, encoding='utf-8')
    transfer.execName = execName
    transfer.to = address.getExecAddress(execName)
    action = tx_pb2.CoinsAction(transferToExec=transfer)
    action.ty = 10
    execer = title + "coins"
    to = transfer.to
    if title != "":
        to = address.getExecAddress(execer)
    return tx.CreateTransaction(execer=bytes(execer, encoding='utf-8'),
                                payload=action.SerializeToString(),
                                expire=0,
                                to=to)
示例#2
0
def withdraw(title: str, amount: int, execName: str,
             note: str) -> tx.Transaction:
    """
     构造提款交易,将其他执行器下面coins代币提回到coins执行器下面
    :param title: 平行链链名前缀 比如user.p.huobi.
    :param amount: 提款金额
    :param execName: 合约名称
    :param note: 备注
    :return: tx.Transaction
    """
    withdraw = tx_pb2.AssetsWithdraw()
    withdraw.amount = amount
    withdraw.note = bytes(note, encoding='utf-8')
    withdraw.execName = execName
    withdraw.to = address.getExecAddress(execName)
    action = tx_pb2.CoinsAction(withdraw=withdraw)
    action.ty = 3
    execer = title + "coins"
    to = withdraw.to
    if title != "":
        to = address.getExecAddress(execer)
    return tx.CreateTransaction(execer=bytes(execer, encoding='utf-8'),
                                payload=action.SerializeToString(),
                                expire=0,
                                to=to)
示例#3
0
def encryptStorage(title: str, key: str, contentHash: bytes,
                   encryptContent: bytes, nonce: bytes,
                   value: str) -> tx.Transaction:
    """
    encryptStorage 构建加密存证交易,返回Transaction类对象实例
    :param title: 链名前缀,比如user.p.huobi.     (主链填空字符串即可)
    :param key: 唯一标识,后面可以根据key来查询存证内容,如果不填则默认以txhash作为key
    :param contentHash: 存证明文hash
    :param encryptContent: 密文
    :param nonce: 加密iv,通过AES进行加密时制定随机生成的iv,解密时需要使用该值
    :param value: 备注
    :return:
    """
    v = storage_pb2.EncryptNotaryStorage()
    v.key = key
    v.value = value
    v.contentHash = contentHash
    v.encryptContent = encryptContent
    v.nonce = nonce
    action = storage_pb2.StorageAction(encryptStorage=v)
    action.ty = 4
    execer = title + "storage"
    to = address.getExecAddress(execer)
    return tx.CreateTransaction(execer=bytes(execer, encoding='utf-8'),
                                payload=action.SerializeToString(),
                                expire=0,
                                to=to)
示例#4
0
def contentStorage(title: str, key: str, content: bytes, value: str,
                   op: int) -> tx.Transaction:
    """
    contentStorage 构建明文存证交易,返回Transaction类对象实例
    :param title: 链名前缀,比如user.p.huobi.     (主链填空字符串即可)
    :param key: 唯一标识,后面可以根据key来查询存证内容,如果不填则默认以txhash作为key
    :param content: 存证内容,(注意 content和value只能二选一,不可同时填写,要么选择bytes类型存储,要么选择str类型存储)
    :param value: 存证内容(注意 content和value只能二选一,不可同时填写,要么选择bytes类型存储,要么选择str类型存储)
    :param op:  0表示创建 1表示追加add
    :return:
    """
    if op != 0 and op != 1:
        raise ValueError("Error: op is not correct.")
    v = storage_pb2.ContentOnlyNotaryStorage()
    v.key = key
    v.value = value
    v.op = op
    action = storage_pb2.StorageAction(contentStorage=v)
    action.ty = 1
    execer = title + "storage"
    to = address.getExecAddress(execer)
    return tx.CreateTransaction(execer=bytes(execer, encoding='utf-8'),
                                payload=action.SerializeToString(),
                                expire=0,
                                to=to)
示例#5
0
def linkStorage(title: str, key: str, link: bytes,
                value: str) -> tx.Transaction:
    """
    linkStorage 构建链接存证交易,返回Transaction类对象实例
    :param title: 链名前缀,比如user.p.huobi.     (主链填空字符串即可)
    :param key: 唯一标识,后面可以根据key来查询存证内容,如果不填则默认以txhash作为key
    :param link: 存证内容
    :param value: 备注
    :return:
    """
    v = storage_pb2.LinkNotaryStorage()
    v.key = key
    v.value = value
    v.link = link
    action = storage_pb2.StorageAction(linkStorage=v)
    action.ty = 3
    execer = title + "storage"
    to = address.getExecAddress(execer)
    return tx.CreateTransaction(execer=bytes(execer, encoding='utf-8'),
                                payload=action.SerializeToString(),
                                expire=0,
                                to=to)
示例#6
0
def hashStorage(title: str, key: str, hash: bytes,
                value: str) -> tx.Transaction:
    """
    hashStorage 构建哈希存证交易,返回Transaction类对象实例
    :param title: 链名前缀,比如user.p.huobi.     (主链填空字符串即可)
    :param key: 唯一标识,后面可以根据key来查询存证内容,如果不填则默认以txhash作为key
    :param hash: 存证内容
    :param value: 存证内容,用户可以先将Hash值 str
    :return:
    """
    v = storage_pb2.HashOnlyNotaryStorage()
    v.key = key
    v.value = value
    v.hash = hash
    action = storage_pb2.StorageAction(hashStorage=v)
    action.ty = 2
    execer = title + "storage"
    to = address.getExecAddress(execer)
    return tx.CreateTransaction(execer=bytes(execer, encoding='utf-8'),
                                payload=action.SerializeToString(),
                                expire=0,
                                to=to)
示例#7
0
def createManage(title: str, key: str, value: str, op: str,
                 addr: str) -> tx.Transaction:
    """
    createManage 构造管理合约配置交易
    :param title: 平行链链名前缀 比如user.p.huobi.
    :param key:  类型
    :param value: 具体值
    :param op: 操作符,add,delete
    :param addr: 替换之前addr的值,具有唯一性,这个值不会增加或者删除,始终只有一个
    :return: tx.Transaction
    """
    modify = manage_pb2.ModifyConfig()
    modify.key = key
    modify.value = value
    modify.op = op
    modify.addr = addr
    action = manage_pb2.ManageAction(modify=modify)
    action.ty = 0
    execer = title + "manage"
    to = address.getExecAddress(execer)
    return tx.CreateTransaction(execer=bytes(execer, encoding='utf-8'),
                                payload=action.SerializeToString(),
                                expire=0,
                                to=to)
示例#8
0
def transfer(title: str, amount: int, toAddr: str,
             note: str) -> tx.Transaction:
    """
    transfer 构造普通转账交易
    :param title: 平行链链名前缀 比如user.p.huobi.
    :param amount: 转账金额
    :param toAddr: 收款地址
    :param note: 备注
    :return: tx.Transaction
    """
    transfer = tx_pb2.AssetsTransfer()
    transfer.amount = amount
    transfer.note = bytes(note, encoding='utf-8')
    transfer.to = toAddr
    action = tx_pb2.CoinsAction(transfer=transfer)
    action.ty = 1
    execer = title + "coins"
    to = toAddr
    if title != "":
        to = address.getExecAddress(execer)
    return tx.CreateTransaction(execer=bytes(execer, encoding='utf-8'),
                                payload=action.SerializeToString(),
                                expire=0,
                                to=to)
示例#9
0
        to = address.getExecAddress(execer)
    return tx.CreateTransaction(execer=bytes(execer, encoding='utf-8'),
                                payload=action.SerializeToString(),
                                expire=0,
                                to=to)


if __name__ == '__main__':
    print('this a test!')
    acc1 = account.newAccount(signer.SECP256K1)
    # 本地构造转账交易
    tx1 = transfer("", 100000, '19MJmA7GcE1NfMwdGqgLJioBjVbzQnVYvR',
                   "this is a test!")
    tx1.Sign(acc1)
    assert tx1.CheckSign()
    assert tx1.Tx().to == '19MJmA7GcE1NfMwdGqgLJioBjVbzQnVYvR'

    # 本地构造transferToExec交易
    tx2 = transferToExec("", 100000, 'ticket', '把币打到ticket合约下')
    tx2.Sign(acc1)
    assert tx2.CheckSign()
    assert tx2.Tx().to == address.getExecAddress('ticket')

    # 本地构造withdraw交易
    tx3 = withdraw("", 100000, 'ticket', '从ticket合约下提币')
    tx3.Sign(acc1)
    assert tx3.CheckSign()
    assert tx3.Tx().to == address.getExecAddress('ticket')
    print(1 & 1 << 0 == 0)
    print('test sucessfully!')