Skip to content

Commit

Permalink
Merge branch 'trunk' of github.com:trinity-project/trinity into trunk
Browse files Browse the repository at this point in the history
  • Loading branch information
zhuofanxu committed Apr 19, 2018
2 parents 923015b + 8436ad5 commit c1db9b3
Show file tree
Hide file tree
Showing 20 changed files with 1,063 additions and 112 deletions.
323 changes: 323 additions & 0 deletions TX/interface.py

Large diffs are not rendered by default.

3 changes: 1 addition & 2 deletions TX/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,6 @@ def pubkeyToAddress(pubkey):
return address




def ToAddresstHash(address):

data = b58decode(address)
Expand Down Expand Up @@ -177,5 +175,6 @@ def blockheight_to_script(input):
return output

if __name__=="__main__":
print(b58decode("b58decode"))
print (blockheight_to_script(1319028))
print (createTxid("d101a00400ca9a3b140069ec6703aa90a51280ab74eb92cb09cca0549514dfee2d95daf8b67b960aaf997900ab94abc3fd1b53c1087472616e7366657267f1dfcf0051ec48ec95c8d0569e0b95075d099d84f10400ca9a3b140069ec6703aa90a51280ab74eb92cb09cca05495142099925aaeee225009fc51b599c71fee77bd30ca53c1087472616e7366657267f1dfcf0051ec48ec95c8d0569e0b95075d099d84f100000000000000000320dfee2d95daf8b67b960aaf997900ab94abc3fd1b202099925aaeee225009fc51b599c71fee77bd30caf0045ac46e4b0000"))
18 changes: 16 additions & 2 deletions gateway/gateway.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,19 @@ class Gateway():
"""
gateway class
"""
TransMessageType=["Rsmc",
"FounderSign",
"Founder",
"RsmcSign",
"FounderFail",
"Settle",
"SettleSign",
"SettleFail",
"RsmcFail",
"Htlc",
"HtlcSign",
"HtlcFail"]

def __init__(self):
"""Counstruct"""
self.websocket = None
Expand Down Expand Up @@ -131,7 +144,8 @@ def handle_tcp_request(self, protocol, bdata):
protocol.transport.send(utils.generate_error_msg(local_url, data["Sender"], "Invalid wallet address"))
else:
self._send_jsonrpc_msg("CreateChannle", json.dumps(data))
elif msg_type in ["Rsmc","FounderSign","Founder","RsmcSign","FounderFail"]:

elif msg_type in Gateway.TransMessageType:
self.handle_transaction_message(data)
return utils.request_handle_result.get("correct")
elif msg_type == "ResumeChannel":
Expand Down Expand Up @@ -315,7 +329,7 @@ def handle_jsonrpc_request(self, method, params):
elif method == "TransactionMessage":
if msg_type == "RegisterChannel":
self._send_tcp_msg(data["Receiver"], data)
elif msg_type in ["Rsmc","FounderSign","Founder","RsmcSign","FounderFail"]:
elif msg_type in Gateway.TransMessageType:
self.handle_transaction_message(data)
elif method == "SyncBlock":
# push the data to spvs
Expand Down
17 changes: 15 additions & 2 deletions gateway/gateway_old.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,19 @@ class Gateway():
"""
gateway 类 定义了所有直接相关的行为与属性
"""
TransMessageType = ["Rsmc",
"FounderSign",
"Founder",
"RsmcSign",
"FounderFail",
"Settle",
"SettleSign",
"SettleFail",
"RsmcFail",
"Htlc",
"HtlcSign",
"HtlcFail"]

def __init__(self):
"""Counstruct"""
self.websocket = None
Expand Down Expand Up @@ -128,7 +141,7 @@ def handle_tcp_request(self, transport, bdata):
transport.send(utils.generate_error_msg(local_url, data["Sender"], "Invalid wallet address"))
else:
self._send_jsonrpc_msg("CreateChannle", json.dumps(data))
elif msg_type in ["Rsmc","FounderSign","Founder","RsmcSign","FounderFail"]:
elif msg_type in Gateway.TransMessageType:
self.handle_transaction_message(data)
elif msg_type == "SyncChannelState":
# node receive the syncchannel msg
Expand Down Expand Up @@ -287,7 +300,7 @@ def handle_jsonrpc_request(self, method, params):
elif method == "TransactionMessage":
if msg_type == "RegisterChannel":
self._send_tcp_msg(data["Receiver"], data)
elif msg_type in ["Rsmc","FounderSign","Founder","RsmcSign","FounderFail"]:
elif msg_type in Gateway.TransMessageType:
self.handle_transaction_message(data)
elif method == "SyncBlock":
# push the data to spvs
Expand Down
2 changes: 1 addition & 1 deletion gateway/msg.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@
"MessageType":"SyncWallet",
"MesssgeBody":{
"Publickey":publickey,
"CommitMinDeposit":Configure["Channel"]["CommitMinDeposit"]
"CommitMinDeposit":Configure["Channel"]["CommitMinDeposit"],
"Fee":Configure["Fee"]
}
}
Expand Down
16 changes: 12 additions & 4 deletions gateway/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,18 @@
"RegisterChannel",
"SyncChannelState",
"ResumeChannel",
"Rsmc",
"FounderSign",
"Founder","RsmcSign",
"FounderFail"
"Rsmc",
"FounderSign",
"Founder",
"RsmcSign",
"FounderFail",
"Settle",
"SettleSign",
"SettleFail",
"RsmcFail",
"Htlc",
"HtlcSign",
"HtlcFail"
]

request_handle_result = {
Expand Down
90 changes: 90 additions & 0 deletions lightwallet/.prompt.py.history
Original file line number Diff line number Diff line change
Expand Up @@ -154,3 +154,93 @@

# 2018-04-13 10:58:23.381703
+help

# 2018-04-16 16:24:52.987441
+open wallet test.json

# 2018-04-16 16:24:57.503781
+wallet

# 2018-04-16 16:31:39.455659
+help

# 2018-04-16 20:37:17.553016
+wallet

# 2018-04-16 20:37:40.573388
+send tnc AJAd9ZaZCwLvdktHc1Rs7w3hmpVSBuTKzs 1

# 2018-04-17 10:30:54.561253
+exit

# 2018-04-17 10:31:21.227565
+open wallet test.json

# 2018-04-17 10:31:25.460181
+wallet

# 2018-04-17 10:31:48.427237
+send tnc AJAd9ZaZCwLvdktHc1Rs7w3hmpVSBuTKzs 1

# 2018-04-17 10:38:03.613256
+send tnc AJAd9ZaZCwLvdktHc1Rs7w3hmpVSBuTKzs 0.3

# 2018-04-17 10:50:44.915656
+wallet

# 2018-04-17 10:57:41.568344
+send neo AJAd9ZaZCwLvdktHc1Rs7w3hmpVSBuTKzs 0.3

# 2018-04-17 10:57:58.721646
+send neo AJAd9ZaZCwLvdktHc1Rs7w3hmpVSBuTKzs 1

# 2018-04-17 11:01:04.695891
+exit

# 2018-04-17 11:01:13.060638
+open wallet test.json

# 2018-04-17 11:01:35.835709
+send neo AJAd9ZaZCwLvdktHc1Rs7w3hmpVSBuTKzs 1

# 2018-04-17 11:20:08.725176
+wallet

# 2018-04-17 11:28:38.754846
+send neo AJAd9ZaZCwLvdktHc1Rs7w3hmpVSBuTKzs 1

# 2018-04-17 11:28:45.554427
+wallet

# 2018-04-17 11:29:07.023100
+send gas AJAd9ZaZCwLvdktHc1Rs7w3hmpVSBuTKzs 0.1

# 2018-04-17 11:53:02.516218
+exit

# 2018-04-17 11:53:16.851592
+open wallet test.json

# 2018-04-17 11:53:23.196738
+tx

# 2018-04-17 11:53:45.612145
+exit

# 2018-04-17 11:53:53.185366
+open wallet test.json

# 2018-04-17 11:53:56.403202
+tx

# 2018-04-17 11:57:36.105085
+exit

# 2018-04-17 11:57:43.545167
+open wallet test.json

# 2018-04-17 11:57:47.186756
+tx

# 2018-04-17 12:01:53.512980
+exit
48 changes: 33 additions & 15 deletions lightwallet/Nep6Wallet.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,12 @@

class Nep6Wallet(object):
AddressVersion = None
_passwordHash=None

Name=None
_path = ''
_keys = {}
Version = "1.0"
Scrypt={"n":16384,"r":8,"p":8}
_accounts=[]

_db_path = _path




Expand All @@ -39,6 +36,9 @@ def __init__(self, path, passwordKey, create):
"""

self._path = path
self._accounts = []
self._keys={}
self._passwordHash=None

if create:
self._iv = bytes(Random.get_random_bytes(16))
Expand Down Expand Up @@ -190,14 +190,24 @@ def GetKeys(self):
return [item["account"] for item in self._accounts]

def Sign(self,tx_data):
"""
:param tx_data:
:return:
"""
privtKey=binascii.hexlify(self._accounts[0]["account"].PrivateKey).decode()
signature = privtkey_sign(tx_data, privtKey)
publicKey = privtKey_to_publicKey(privtKey)
rawData = tx_data + "014140" +signature + "2321" + publicKey + "ac"
return rawData


def Sign1(self,tx_data):
def SignContent(self,tx_data):
"""
:param tx_data:
:return:
"""
privtKey=binascii.hexlify(self._accounts[0]["account"].PrivateKey).decode()
signature = privtkey_sign(tx_data, privtKey)
return signature
Expand All @@ -210,9 +220,10 @@ def send(self,addressFrom,addressTo,amount,assetId):
res = construct_tx(addressFrom,addressTo,amount,assetId)
print(res)
raw_txdata=self.Sign(res["result"]["txData"])
send_raw_tx(raw_txdata)
print("txid: "+res["result"]["txid"])
return True
if send_raw_tx(raw_txdata):
print("txid: "+res["result"]["txid"])
return True,res["result"]["txid"]
return False,res["result"]["txid"]

def ToJson(self, verbose=False):

Expand All @@ -231,9 +242,6 @@ def ToJson(self, verbose=False):
}
}
jsn['accounts'].append(tmp_dict)



return jsn

def ToJsonFile(self, path,password):
Expand All @@ -252,12 +260,22 @@ def ToJsonFile(self, path,password):

with open(path,"wb") as f:
f.write(json.dumps(jsn).encode())
f.close()



return None

def fromJsonFile(self,path):
with open (path,"rb") as f:
with open(path,"rb") as f:
content=json.loads(f.read().decode())
return content
return content

def LoadStoredData(self, key):
wallet = self.fromJsonFile(self._path)
return wallet.get("extra").get(key)

def SaveStoredData(self, key, value):
wallet_info = self.fromJsonFile(self._path)
wallet_info["extra"][key] = value
with open(self._path,"wb") as f:
f.write(json.dumps(wallet_info).encode())
51 changes: 51 additions & 0 deletions lightwallet/model.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@

from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import Column, Integer, String, Numeric,Boolean
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
import pymysql
from decimal import Decimal


pymysql.install_as_MySQLdb()
engine = create_engine('sqlite:///transction_record.db')
Session = sessionmaker(bind=engine)
Base = declarative_base()
session=Session()

class TX_RECORD(Base):
__tablename__ = 'tx_record'
id = Column(Integer, primary_key=True)
tx_id = Column(String(256))
state = Column(Boolean)
asset_type = Column(String(64))
address_from = Column(String(64))
address_to = Column(String(64))
value = Column(Numeric(16,8))


@staticmethod
def save(tx_id,asset_type,address_from,address_to,value,state):
new_instance = TX_RECORD(tx_id=tx_id,state=state,
asset_type=asset_type,address_from=address_from,
address_to=address_to,value=Decimal(value))
session.add(new_instance)
session.commit()

@staticmethod
def query(address):
res=session.query(TX_RECORD).filter(TX_RECORD.address_from == address or TX_RECORD.address_from == address).all()
return res

def to_json(self):
return {
"tx_id":self.tx_id,
"asset_type":self.asset_type,
"address_from":self.address_from,
"address_to":self.address_to,
"value":self.value,
"state":self.state,
}


Base.metadata.create_all(engine)
Loading

0 comments on commit c1db9b3

Please sign in to comment.