1
1
# Basic example of json-rpc calls to Monero's simplewallet.
2
2
#
3
- # Transfer 1 xmr to some address
3
+ # Transfer given xmr amount to some address
4
4
#
5
5
# The simplewallet RPC docs are here:
6
6
# https://getmonero.org/knowledge-base/developer-guides/wallet-rpc
10
10
11
11
import requests
12
12
import json
13
+ import os
14
+ import binascii
15
+
13
16
14
17
def main ():
15
18
"""DONT RUN IT without changing the destination address!!!"""
@@ -22,31 +25,125 @@ def main():
22
25
23
26
destination_address = "489MAxaT7xXP3Etjk2suJT1uDYZU6cqFycsau2ynCTBacncWVEwe9eYFrAD6BqTn4Y2KMs7maX75iX1UFwnJNG5G88wxKoj"
24
27
25
- # send 1 xmr to the given destination_address
26
- recipents = [
27
- {"address" : destination_address , "amount" : 1 }
28
- ]
28
+
29
+ # amount of xmr to send
30
+ amount = 0.54321
31
+
32
+ # cryptonote amount format is different then
33
+ # that normally used by people.
34
+ # thus the float amount must be changed to
35
+ # something that cryptonote understands
36
+ int_amount = int (get_amount (amount ))
37
+
38
+ # just to make sure that amount->coversion->back
39
+ # gives the same amount as in the initial number
40
+ assert amount == float (get_money (str (int_amount ))), "Amount conversion failed"
41
+
42
+ # send specified xmr amount to the given destination_address
43
+ recipents = [{"address" : destination_address ,
44
+ "amount" : int_amount }]
29
45
30
46
# using given mixin
31
47
mixin = 4
32
48
49
+ # get some random payment_id
50
+ payment_id = get_payment_id ()
51
+
33
52
# simplewallet' procedure/method to call
34
53
rpc_input = {
35
54
"method" : "transfer" ,
36
- "params" : {"destinations" : recipents , "mixin" : mixin }
55
+ "params" : {"destinations" : recipents ,
56
+ "mixin" : mixin ,
57
+ "payment_id" : payment_id }
37
58
}
38
59
39
60
# add standard rpc values
40
61
rpc_input .update ({"jsonrpc" : "2.0" , "id" : "0" })
41
62
63
+ print (json .dumps (rpc_input ))
64
+
42
65
# execute the rpc request
43
66
response = requests .post (
44
67
url ,
45
68
data = json .dumps (rpc_input ),
46
69
headers = headers )
47
70
71
+ # print the payment_id
72
+ print ("#payment_id: " , payment_id )
73
+
48
74
# pretty print json output
49
75
print (json .dumps (response .json (), indent = 4 ))
50
76
77
+
78
+ def get_amount (amount ):
79
+ """encode amount (float number) to the cryptonote format. Hope its correct."""
80
+
81
+ CRYPTONOTE_DISPLAY_DECIMAL_POINT = 12
82
+
83
+ str_amount = str (amount )
84
+
85
+ fraction_size = 0
86
+
87
+ if '.' in str_amount :
88
+
89
+ point_index = str_amount .index ('.' )
90
+
91
+ fraction_size = len (str_amount ) - point_index - 1
92
+
93
+ while fraction_size < CRYPTONOTE_DISPLAY_DECIMAL_POINT and '0' == str_amount [- 1 ]:
94
+ print (44 )
95
+ str_amount = str_amount [:- 1 ]
96
+ fraction_size = fraction_size - 1
97
+
98
+ if CRYPTONOTE_DISPLAY_DECIMAL_POINT < fraction_size :
99
+ return False
100
+
101
+ str_amount = str_amount [:point_index ] + str_amount [point_index + 1 :]
102
+
103
+ if not str_amount :
104
+ return False
105
+
106
+ if fraction_size < CRYPTONOTE_DISPLAY_DECIMAL_POINT :
107
+ str_amount = str_amount + '0' * (CRYPTONOTE_DISPLAY_DECIMAL_POINT - fraction_size )
108
+
109
+ return str_amount
110
+
111
+
112
+ def get_money (amount ):
113
+ """decode cryptonote amount format to user friendly format. Hope its correct."""
114
+
115
+ CRYPTONOTE_DISPLAY_DECIMAL_POINT = 12
116
+
117
+ s = amount
118
+
119
+ if len (s ) < CRYPTONOTE_DISPLAY_DECIMAL_POINT + 1 :
120
+ # add some trailing zeros, if needed, to have constant width
121
+ s = '0' * (CRYPTONOTE_DISPLAY_DECIMAL_POINT + 1 - len (s )) + s
122
+
123
+ idx = len (s ) - CRYPTONOTE_DISPLAY_DECIMAL_POINT
124
+
125
+ s = s [0 :idx ] + "." + s [idx :]
126
+
127
+ return s
128
+
129
+ def get_payment_id ():
130
+ """generate random payment_id
131
+
132
+ generate some random payment_id for the
133
+ transactions
134
+
135
+ payment_id is 32 bytes (64 hexadecimal characters)
136
+ thus we first generate 32 random byte array
137
+ which is then change to string representation, since
138
+ json will not not what to do with the byte array.
139
+ """
140
+
141
+ random_32_bytes = os .urandom (32 )
142
+ payment_id = "" .join (map (chr , binascii .hexlify (random_32_bytes )))
143
+
144
+ return payment_id
145
+
146
+
147
+
51
148
if __name__ == "__main__" :
52
149
main ()
0 commit comments