Skip to content

Commit

Permalink
changed data and set script tx to base64 encoding, added string as ty…
Browse files Browse the repository at this point in the history
…pe for data transactions
  • Loading branch information
jansenmarc committed May 26, 2018
1 parent c8b0c8a commit 2cdd0ad
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 10 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@ __pywaves.Address(address, publicKey, privateKey, seed)__ _Creates a new Address

`setScript(script, txFee=pywaves.DEFAULT_SCRIPT_FEE, timestamp=0)` sets a script for this address

`dataTransaction(data, timestamp=0)` sets data for the account. data should be a json array with entries including type (bool, binary, int, string), key and value

### Asset Class
__pywaves.Asset(assetId)__ _Creates a new Asset object_

Expand Down
28 changes: 18 additions & 10 deletions address.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@
import struct
import json
import base58
import base64
import logging
import math
import requests

wordList = ['abandon', 'ability', 'able', 'about', 'above', 'absent', 'absorb', 'abstract', 'absurd', 'abuse', 'access',
'accident', 'account', 'accuse', 'achieve', 'acid', 'acoustic', 'acquire', 'across', 'act', 'action',
Expand Down Expand Up @@ -502,7 +503,7 @@ def sendAsset(self, recipient, asset, amount, attachment='', feeAsset='', txFee=

return pywaves.wrapper('/assets/broadcast/transfer', data)

def `
def massTransferAssets(self, transfers, asset, attachment='', timestamp=0):
txFee = 100000 + len(transfers) * 50000
totalAmount = 0

Expand Down Expand Up @@ -570,9 +571,9 @@ def dataTransaction(self, data, timestamp=0):
dataBinary += keyBytes
if d['type'] == 'binary':
dataBinary += b'\2'
valueAsBytes = base58.b58decode(d['value'])
valueAsBytes = d['value']
dataBinary += struct.pack(">H", len(valueAsBytes))
dataBinary += valueAsBytes
dataBinary += crypto.str2bytes(valueAsBytes)
elif d['type'] == 'boolean':
if d['value']:
dataBinary += b'\1\1'
Expand All @@ -581,7 +582,10 @@ def dataTransaction(self, data, timestamp=0):
elif d['type'] == 'integer':
dataBinary += b'\0'
dataBinary += struct.pack(">Q", d['value'])

elif d['type'] == 'string':
dataBinary += b'\3'
dataBinary += struct.pack(">H", len(d['value']))
dataBinary += crypto.str2bytes(d['value'])
# check: https://stackoverflow.com/questions/2356501/how-do-you-round-up-a-number-in-python
txFee = (int(( (len(crypto.str2bytes(json.dumps(data))) + 2 + 64 )) / 1000.0) + 1 ) * 100000
dataObject['fee'] = txFee
Expand All @@ -594,8 +598,12 @@ def dataTransaction(self, data, timestamp=0):
struct.pack(">Q", txFee)

dataObject['proofs'] = [ crypto.sign(self.privateKey, sData) ]
dataObjectJSON = json.dumps(dataObject)

for entry in dataObject['data']:
if entry['type'] == 'binary':
base64Encoded = base64.b64encode(crypto.str2bytes(entry['value']))
entry['value'] = 'base64:' + crypto.bytes2str(base64Encoded)
dataObjectJSON = json.dumps(dataObject)
return pywaves.wrapper('/transactions/broadcast', dataObjectJSON)

def _postOrder(self, amountAsset, priceAsset, orderType, amount, price, maxLifetime=30*86400, matcherFee=pywaves.DEFAULT_MATCHER_FEE, timestamp=0):
Expand Down Expand Up @@ -873,11 +881,12 @@ def sponsorAsset(self, assetId, minimalFeeInAssets, txFee=pywaves.DEFAULT_SPONSO

return pywaves.wrapper('/transactions/broadcast', data)

def setScript(self, script, txFee=pywaves.DEFAULT_SCRIPT_FEE, timestamp=0):
def setScript(self, scriptSource, txFee=pywaves.DEFAULT_SCRIPT_FEE, timestamp=0):
script = pywaves.wrapper('/utils/script/compile', scriptSource)['script'][7:]
if not self.privateKey:
logging.error('Private key required')
else:
rawScript = base58.b58decode(script)
rawScript = base64.b64decode(script)
scriptLength = len(rawScript)
if timestamp == 0:
timestamp = int(time.time() * 1000)
Expand All @@ -898,12 +907,11 @@ def setScript(self, script, txFee=pywaves.DEFAULT_SCRIPT_FEE, timestamp=0):
"senderPublicKey": self.publicKey,
"fee": txFee,
"timestamp": timestamp,
"script": script,
"script": 'base64:' + script,
"proofs": [
signature
]
})

return pywaves.wrapper('/transactions/broadcast', data)


0 comments on commit 2cdd0ad

Please sign in to comment.