forked from swapper-org/NodeChain
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
125 lines (89 loc) · 3.9 KB
/
Copy pathutils.py
File metadata and controls
125 lines (89 loc) · 3.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#!/usr/bin/python3
import binascii
import hashlib
import math
from decimal import Decimal
import random
import sys
from logger import logger
from rpcutils import error as rpcerrorhandler
from wsutils import topics
from rpcutils.rpcconnector import RPCConnector
from rpcutils.rpcsocketconnector import RPCSocketConnector
from .constants import *
from . import apirpc
def convertToSatoshi(strAmount):
return str(int(Decimal(strAmount) * 100000000))
def convertKbToBytes(strAmount):
return str(int(Decimal(strAmount) / 1000))
def getMethodSchemas(name):
return getRequestMethodSchema(name), getResponseMethodSchema(name)
def getRequestMethodSchema(name):
return RPC_JSON_SCHEMA_FOLDER + name + SCHEMA_CHAR_SEPARATOR + REQUEST + SCHEMA_EXTENSION
def getResponseMethodSchema(name):
return RPC_JSON_SCHEMA_FOLDER + name + SCHEMA_CHAR_SEPARATOR + RESPONSE + SCHEMA_EXTENSION
def getWSMethodSchemas(name):
return getWSRequestMethodSchema(name), getWSResponseMethodSchema(name)
def getWSRequestMethodSchema(name):
return WS_JSON_SCHEMA_FOLDER + name + SCHEMA_CHAR_SEPARATOR + REQUEST + SCHEMA_EXTENSION
def getWSResponseMethodSchema(name):
return WS_JSON_SCHEMA_FOLDER + name + SCHEMA_CHAR_SEPARATOR + RESPONSE + SCHEMA_EXTENSION
def closeAddrBalanceTopic(topicName):
addrTopicSplitted = topicName.split(topics.TOPIC_SEPARATOR)
if len(addrTopicSplitted) <= 1:
logger.printError(f"Topic name [{topicName}] not valid for Address Balance WS")
raise rpcerrorhandler.RpcInternalServerError(f"Can not unsubscribe {topicName} to node")
id = random.randint(1, sys.maxsize)
response = apirpc.notify(
id,
{
"success": addrTopicSplitted[1],
"callBackEndpoint": ""
}
)
if not response["success"]:
logger.printError(f"Can not unsubscribe {topicName} to node")
raise rpcerrorhandler.BadRequestError(f"Can not unsubscribe {topicName} to node")
def decodeTransactionDetails(txDecoded, bitcoincoreRpcEndpoint):
outputs = []
for output in txDecoded["vout"]:
if "addresses" in output["scriptPubKey"] and len(output["scriptPubKey"]["addresses"]) == 1:
outputs.append(
{"amount": math.trunc(output["value"] * 100000000), "address": output["scriptPubKey"]["addresses"][0]})
else:
outputs.append({"amount": math.trunc(output["value"] * 100000000), "address": None})
sumOutputs = 0
for output in outputs:
sumOutputs += output["amount"]
inputs = []
for txInput in txDecoded["vin"]:
if "coinbase" in txInput: # This is a coinbase transaction and thus it have one only input of 'sumOutputs'
inputs.append({"amount": sumOutputs, "address": None})
break
transaction = RPCConnector.request(
endpoint=bitcoincoreRpcEndpoint,
id=0,
method="getrawtransaction",
params=[
txInput["txid"],
True
]
)
for txOutput in transaction["vout"]:
if txOutput["n"] == txInput["vout"] and "addresses" in txOutput["scriptPubKey"] and len(
txOutput["scriptPubKey"]["addresses"]) == 1:
inputs.append({"amount": math.trunc(txOutput["value"] * 100000000),
"address": txOutput["scriptPubKey"]["addresses"][0]})
elif "addresses" not in txOutput["scriptPubKey"] or len(txOutput["scriptPubKey"]["addresses"]) != 1:
inputs.append({"amount": math.trunc(txOutput["value"] * 100000000), "address": None})
sumInputs = 0
for txInput in inputs:
sumInputs += txInput["amount"]
fee = sumInputs - sumOutputs
transactionsDetails = {"fee": fee, "inputs": inputs, "outputs": outputs}
return transactionsDetails
def sortUnspentOutputs(outputs):
try:
return outputs['txHash']
except KeyError:
return ''