|
| 1 | + |
1 | 2 | [](https://travis-ci.org/StellarCN/py-stellar-base)
|
2 | 3 |
|
3 | 4 | # install
|
4 |
| -`pip install stellar-base.whl` |
| 5 | + pip install stellar-base.whl |
5 | 6 |
|
6 | 7 |
|
7 | 8 | # usage
|
8 | 9 |
|
| 10 | +## Create a Stellar keypair? |
| 11 | +```python |
| 12 | + from stellar_base.keypair import Keypair |
| 13 | + kp = Keypair.random() |
| 14 | +``` |
| 15 | +**or** |
| 16 | +```python |
| 17 | + from __future__ import unicode_literals |
| 18 | + master = u'中文'.encode('utf-8') |
| 19 | + kp = Keypair.deterministic(master) |
| 20 | +``` |
| 21 | +then we can get key/secret from random: |
| 22 | + |
| 23 | + publickey = kp.address().decode() |
| 24 | + secret = kp.seed().decode() |
| 25 | + |
| 26 | + |
| 27 | +let's start with my favourite keypair in TESTNET. |
| 28 | + |
| 29 | + publickey = 'GDVDKQFP665JAO7A2LSHNLQIUNYNAAIGJ6FYJVMG4DT3YJQQJSRBLQDG' |
| 30 | + secret = 'SCVLSUGYEAUC4MVWJORB63JBMY2CEX6ATTJ5MXTENGD3IELUQF4F6HUB' |
| 31 | + |
| 32 | + |
| 33 | + |
| 34 | + |
| 35 | +##Account |
| 36 | + |
| 37 | +### base info |
| 38 | +```python |
| 39 | + from stellar_base.account import Account |
| 40 | + publickey = 'GDVDKQFP665JAO7A2LSHNLQIUNYNAAIGJ6FYJVMG4DT3YJQQJSRBLQDG' |
| 41 | + account = Account(account=publickey) |
| 42 | + # account = Account(account=publickey,network='public') for livenet. |
| 43 | + account.get() |
| 44 | +``` |
| 45 | +now you can check account.`balance` ,`sequence` ,`flags` ,`signers`, `manage_data` etc. |
| 46 | + |
| 47 | +### check payments |
| 48 | +`account.payments()`will give you latest 10 payments. |
| 49 | + |
| 50 | +there are three params using for query : `limit`, `order` and `cursor`(paging_token). and the default value for them is 10, asc and 0 |
| 51 | + |
| 52 | +so need check payments after a specific cursor?try `account.payments(cursor='4225135422738433',limit=20,order=asc)` |
| 53 | + |
| 54 | +Horizon have SSE support for push data ,if you really want, use like this: `account.payment(sse=True,cursor='4225135422738433')` |
| 55 | + |
| 56 | +###like check payments, you can check `transactions`,`effects`,`offers`,and `operations`. |
| 57 | +remember , offers have not SSE support. |
| 58 | + |
| 59 | + |
| 60 | +## Transaction Builder |
| 61 | + |
| 62 | +### create a Transaction Builder at first |
| 63 | + |
| 64 | + from stellar_base.builder import Builder |
| 65 | + builder = Builder(secret=secret) |
| 66 | + # builder = Builder(secret=secret, network='public') for LIVENET. |
| 67 | + |
| 68 | +### operations |
| 69 | +how about make a tips to bob? |
| 70 | + |
| 71 | + bob_address = 'GABCDEFGHIJKLMNOPQRSTUVW' |
| 72 | + builder.append_payment_op(bob_address,'100','XLM') |
| 73 | +or |
| 74 | + |
| 75 | + CNY_ISSUER='GCNYISSUERABCDEFGHIJKLMNOPQ' |
| 76 | + builder.append_payment_op(bob_address,'100','CNY',CNY_ISSUER) |
| 77 | + |
| 78 | +### then maybe need carry a message |
| 79 | + |
| 80 | + builder.add_text_memo('Have a nice day!') # string length <= 28 bytes |
| 81 | + |
| 82 | +### sign & sumbit |
| 83 | + |
| 84 | + builder.sign() |
| 85 | + builder.sumbit() |
| 86 | + |
| 87 | +Done. |
| 88 | + |
| 89 | +### sign a muilt-sig transaction |
| 90 | + |
| 91 | + you get a xdr string (or transaction envlope xdr)from a friend or partner ,which decribe a multi-sig transaction . |
| 92 | + They need you sign on it too. |
| 93 | + |
| 94 | + builder = Builder(secret=secret) |
| 95 | + # or builder = Builder(secret=secret, network='public') for LIVENET. |
| 96 | + builder.import_from_xdr(xdr_string) |
| 97 | + builder.sign() |
| 98 | + builder.to_xdr() # generate new xdr string |
| 99 | + # or builder.submit() #submit to stellar network |
| 100 | + |
| 101 | + |
| 102 | + |
| 103 | +## A payment example without wrapper |
| 104 | + |
| 105 | + |
| 106 | + from stellar_base.keypair import Keypair |
| 107 | + from stellar_base.asset import Asset |
| 108 | + from stellar_base.operation import Payment |
| 109 | + from stellar_base.transaction import Transaction |
| 110 | + from stellar_base.transaction_envelope import TransactionEnvelope as Te |
| 111 | + from stellar_base.memo import TextMemo |
| 112 | + from stellar_base.horizon import horizon_testnet, horizon_pubic |
| 113 | + |
| 114 | + alice_seed = 'SAZJ3EDATROKTNNN4WZBZPRC34AN5WR43VEHAFKT5D66UEZTKDNKUHOK' |
| 115 | + bob_address = 'GDLP3SP4WP72L4BAJWZUDZ6SAYE4NAWILT5WQDS7RWC4XCUNUQDRB2A4' |
| 116 | + CNY_ISSUER = 'GDVDKQFP665JAO7A2LSHNLQIUNYNAAIGJ6FYJVMG4DT3YJQQJSRBLQDG' |
| 117 | + amount = '100' |
| 118 | + |
| 119 | + Alice = Keypair.from_seed(alice_seed) |
| 120 | + horizon = horizon_testnet() |
| 121 | + |
| 122 | + asset = Asset('CNY', CNY_ISSUER) |
| 123 | + # create op |
| 124 | + op = Payment({ |
| 125 | + # 'source' : Alice.address().decode(), |
| 126 | + 'destination': bob_address, |
| 127 | + 'asset': asset, |
| 128 | + 'amount': amount |
| 129 | + }) |
| 130 | + # create a memo |
| 131 | + msg = TextMemo('Have a nice day!') |
| 132 | + |
| 133 | + # get sequence of Alice |
| 134 | + sequence = horizon.account(Alice.address()).get('sequence') |
| 135 | + |
| 136 | + # construct Tx |
| 137 | + tx = Transaction( |
| 138 | + source=Alice.address().decode(), |
| 139 | + opts={ |
| 140 | + 'sequence': sequence, |
| 141 | + # 'timeBounds': [], |
| 142 | + 'memo': msg, |
| 143 | + # 'fee': 100, |
| 144 | + 'operations': [ |
| 145 | + op, |
| 146 | + ], |
| 147 | + }, |
| 148 | + ) |
| 149 | + |
| 150 | + |
| 151 | + # build envelope |
| 152 | + envelope = Te(tx=tx, opts={"network_id": "TESTNET"}) |
| 153 | + # sign |
| 154 | + envelope.sign(Alice) |
| 155 | + # submit |
| 156 | + xdr = envelope.xdr() |
| 157 | + horizon.submit(xdr) |
| 158 | + |
0 commit comments