Bitcoin Forum
November 14, 2024, 07:08:17 AM *
News: Latest Bitcoin Core release: 28.0 [Torrent]
 
   Home   Help Search Login Register More  
Pages: [1]
  Print  
Author Topic: Getting started using armoryengine.py python utilities  (Read 1798 times)
etotheipi (OP)
Legendary
*
Offline Offline

Activity: 1428
Merit: 1093


Core Armory Developer


View Profile WWW
April 27, 2013, 07:15:23 PM
 #1

I've been meaning to put together a short how-to about getting setup with armoryengine, which is the base of Armory.  Anything Armory does, armoryengine can do.  But it can be pain to setup in Windows, and some people need a little push  with some example code to make it happen.  So I'm going to explain how to get into the interactive environment in both Windows and Linux, then some sample code to see how you can access armoryengine.py functionality:



Setting up in Ubuntu: 

This is just five commands as posted on the Building Armory from Source page.  It works on a fresh boot Ubuntu, as long as you have an internet connection:

Quote
   $ sudo apt-get install git-core build-essential pyqt4-dev-tools swig libqtcore4 libqt4-dev python-qt4 python-dev python-twisted python-psutil
   $ git clone git://github.com/etotheipi/BitcoinArmory.git
   $ cd BitcoinArmory
   $ make
   $ python
   $ >>> from armoryengine import *

Now you have access to all armoryengine has to offer.



Setting up in Windows: 

Quite a bit more involved, because you need to install all the python dependencies manually, and install Armory itself to get a .dll you need (but which isn't bundled in the git repo).  But I just tested this on a fresh Windows VM, so this should work reliably.


    (1-7) Follow the Windows instructions on the  Building Armory from Source page, but only through installing zope.interface.  Stop once you get to the SWIG step (you don't need swig or anything else there)
    (Cool Grab the latest 64-bit version of Armory.  You need two files from it to run the python utilities.
    (9) Copy _CppBlockUtils.pyd and CppBlockUtils.py from "C:\Program Files (x86)\Armory\Armory Bitcoin Client\" into your cloned git repo. 
    (10) Open "Git Bash" put on your desktop and "cd" your way to the git repo.
    (11) Type "/c/Python27/python.exe"
    (12) Type "from armoryengine import *"





Some simple things you can do with armoryengine.py:

Here's a simple script showing a bunch of things you can do.  You can use it to play with addresses, transactions, binary strings, hex, binary, base58, etc. 

Code:
from armoryengine import *

print 'Current time is: ', RightNow()
print 'Current time is: ', unixTimeToFormatStr(RightNow())
print 'Easy display of times and bytes:   "%d"B is "%s"' % (10**7, bytesToHumanSize(10**7))

strBinary = '\x00\x00\x03\x0a'
strHex = binary_to_hex(strBinary)
print ''
print 'Original binary string in hex:     ', strHex
print 'Switch endianness of a result:     ', hex_switchEndian(strHex)
print 'Integer (little-endian is default):', hex_to_int(strHex)
print 'Integer (force big-endian):        ', hex_to_int(strHex, BIGENDIAN)
print 'Convert numbers to hex or binary:  ', int_to_hex(251)
print 'Convert numbers to hex or binary:  ', int_to_hex(251, widthBytes=4)
print 'Convert numbers to hex or binary:  ', int_to_hex(251, widthBytes=4, endOut=BIGENDIAN)

print ''
print 'Also convert Base58<-->Binary:     ', binary_to_base58(strBinary)
print 'Also convert Base58<-->Binary:     ', binary_to_hex(base58_to_binary('1ArmoryX'))
print 'Also convert Base58<-->Binary:     ', binary_to_base58('\x00\x57\x60\xd5\xde\x82')

print ''
print 'Pack and unpack VAR_INTs: (158):   ', binary_to_hex(packVarInt(158)[0])
print 'Pack and unpack VAR_INTs: (1883):  ', binary_to_hex(packVarInt(1883)[0])
print 'Pack and unpack VAR_INTs: (2^53): ',  unpackVarInt('\xfd\x5b\x07')
print 'Pack and unpack VAR_INTs: (2^53): ',  binary_to_hex(packVarInt(2**53)[0])

print ''
print 'Make sure to only has binary strings, never hex!  Always returns binary!'
print 'sha256(strHex):            ', binary_to_hex( sha256(strBinary) )
print 'sha256(sha256(strHex)):    ', binary_to_hex( hash256(strBinary) )
print 'ripemd160(sha256(strHex)): ', binary_to_hex( hash160(strBinary) )

print ''
print 'Play with transactions:'
txHex = ('01000000012cd3dbb674a379211607838af7caa165f36f5823367208c51fc1a0'
         '344e96a3f1010000008b483045022100d857839cd7f2da84c8993f4c72afbe07'
         '4a5c77a44b2b43b1f8df4601df71e54602207a2647d11c7bafa03345dd41298e'
         '2d5b54f524fcbe846dd8114ed1ed7cc2d34e0141043753f29064339c7f616b3e'
         'baa27873be06bde832a603f014421f6270de0371aed18bf82ac65ec26eef758e'
         '452fd30dee1d43d34d452b030ccbb8bc777664e6d5ffffffff0200e1f5050000'
         '00001976a91416b31981c5aed9fcb85c863b9f808638a8bab72288ac806e5a43'
         '000000001976a914ac4e0288884f2ca6866ccd5085c3e6e85c53bbf788ac0000'
         '0000')

print 'View large blocks of hex conveniently (a full tx):',
print prettyHex(txHex)
tx = PyTx().unserialize( hex_to_binary( txHex ))
tx.pprint()

print 'Printing all inputs:'
for txin in tx.inputs:
   txin.pprint()

print 'Printing all outputs:'
for txout in tx.outputs:
   txout.pprint()


print ''
print 'Play with addresses: (note private and public keys are SecureBinaryData objects)'
privKey = SecureBinaryData('\xfa\xb3\x00\x39'*8)
addr = PyBtcAddress().createFromPlainKeyData(privKey)
print 'FullPrivKey:', binary_to_hex(addr.binPrivKey32_Plain.toBinStr())
print 'FullPubKey: ', binary_to_hex(addr.binPublicKey65.toBinStr())
print 'Hash160:    ', binary_to_hex(addr.getAddr160())
print 'AddrStr:    ', addr.getAddrStr()

The output of this script is:
Code:
Current time is:  1367089117.34
Current time is:  2013-Apr-27 02:58pm
Easy display of times and bytes:   "10000000"B is "9.5 MB"

Original binary string in hex:      0000030a
Switch endianness of a result:      0a030000
Integer (little-endian is default): 167968768
Integer (force big-endian):         778
Convert numbers to hex or binary:   fb
Convert numbers to hex or binary:   fb000000
Convert numbers to hex or binary:   000000fb

Also convert Base58<-->Binary:      11ER
Also convert Base58<-->Binary:      005760d5de82
Also convert Base58<-->Binary:      1ArmoryX

Pack and unpack VAR_INTs: (158):    9e
Pack and unpack VAR_INTs: (1883):   fd5b07
Pack and unpack VAR_INTs: (2^53):  [1883, 3]
Pack and unpack VAR_INTs: (2^53):  ff0000000000002000

Make sure to only has binary strings, never hex!  Always returns binary!
sha256(strHex):             666945b30a69280a62d9c5215ff1417edb86c538669b497f62c35d2e66b388f3
sha256(sha256(strHex)):     df4279ed267b3541eb5dcbb328219d3d409e6e1d22256ddf5b0ea2fb608ea47c
ripemd160(sha256(strHex)):  57e4edbd695ca33612537f4a62fde541bd65a1f3

Play with transactions:
View large blocks of hex conveniently (a full tx):
0x0000:  01000000 012cd3db b674a379 21160783 8af7caa1 65f36f58 23367208 c51fc1a0
0x0020:  344e96a3 f1010000 008b4830 45022100 d857839c d7f2da84 c8993f4c 72afbe07
0x0040:  4a5c77a4 4b2b43b1 f8df4601 df71e546 02207a26 47d11c7b afa03345 dd41298e
0x0060:  2d5b54f5 24fcbe84 6dd8114e d1ed7cc2 d34e0141 043753f2 9064339c 7f616b3e
0x0080:  baa27873 be06bde8 32a603f0 14421f62 70de0371 aed18bf8 2ac65ec2 6eef758e
0x00a0:  452fd30d ee1d43d3 4d452b03 0ccbb8bc 777664e6 d5ffffff ff0200e1 f5050000
0x00c0:  00001976 a91416b3 1981c5ae d9fcb85c 863b9f80 8638a8ba b72288ac 806e5a43
0x00e0:  00000000 1976a914 ac4e0288 884f2ca6 866ccd50 85c3e6e8 5c53bbf7 88ac0000
0x0100:  0000
Transaction:
   TxHash:    d33a63e76e6ccb4783a1c843191ce3c317df8a0390eee4a91405802b1ab7dd00 (BE)
   Version:   1
   nInputs:   1
   nOutputs:  2
   LockTime:  0
   Inputs:
      PyTxIn:
         PrevTxHash: f1a3964e34a0c11fc508723623586ff365a1caf78a8307162179a374b6dbd32c (BE)
         TxOutIndex: 1
         Script:     (483045022100d857839cd7f2da84c8993f4c72afbe074a5c77a44b2b43b1f8df)
         Sender:     1Gi4gDcPbUU2RiET5dNgZFVdj1Zyw3pdyM
         Seq:        4294967295
   Outputs:
      TxOut:
         Value:    100000000 ( 1.0 )
         Script:   OP_DUP OP_HASH (1352RHJqxaY8zF6S41drgYSqHNqYXJsYMq) OP_EQUAL OP_CHECKSIG
      TxOut:
         Value:    1130000000 ( 11.3 )
         Script:   OP_DUP OP_HASH (1Gi4gDcPbUU2RiET5dNgZFVdj1Zyw3pdyM) OP_EQUAL OP_CHECKSIG
Printing all inputs:
PyTxIn:
   PrevTxHash: f1a3964e34a0c11fc508723623586ff365a1caf78a8307162179a374b6dbd32c (BE)
   TxOutIndex: 1
   Script:     (483045022100d857839cd7f2da84c8993f4c72afbe074a5c77a44b2b43b1f8df)
   Sender:     1Gi4gDcPbUU2RiET5dNgZFVdj1Zyw3pdyM
   Seq:        4294967295
Printing all outputs:
TxOut:
   Value:    100000000 ( 1.0 )
   Script:   OP_DUP OP_HASH (1352RHJqxaY8zF6S41drgYSqHNqYXJsYMq) OP_EQUAL OP_CHECKSIG
TxOut:
   Value:    1130000000 ( 11.3 )
   Script:   OP_DUP OP_HASH (1Gi4gDcPbUU2RiET5dNgZFVdj1Zyw3pdyM) OP_EQUAL OP_CHECKSIG

Play with addresses: (note private and public keys are SecureBinaryData objects)
FullPrivKey: fab30039fab30039fab30039fab30039fab30039fab30039fab30039fab30039
FullPubKey:  04b5bdd137eb8001c3478402b04e2a5536af8511aa9e40c2c058e6af6470181130e28deff4fc5e9689f95e10410657c89dad9a5bbae4f5a081d6185a796d5b2600
Hash160:     12788165043cbd1bb914ae4c74f400531c5101d6
AddrStr:     12gfXKTYd1gqmyJCg7Sj36GQyt8KGuVsEz

Founder and CEO of Armory Technologies, Inc.
Armory Bitcoin Wallet: Bringing cold storage to the average user!
Only use Armory software signed by the Armory Offline Signing Key (0x98832223)

Please donate to the Armory project by clicking here!    (or donate directly via 1QBDLYTDFHHZAABYSKGKPWKLSXZWCCJQBX -- yes, it's a real address!)
newcn
Full Member
***
Offline Offline

Activity: 143
Merit: 100


View Profile
February 20, 2014, 03:02:46 AM
 #2

hello,
    I want to know if armoryengine can be used to access other coin's blockchain? for example, blockchain of pts?

BTC:1NzzfeHCgN8fF6mSG1UeBFCVd2cxKbGyHk
NXT:13187911577562526278
etotheipi (OP)
Legendary
*
Offline Offline

Activity: 1428
Merit: 1093


Core Armory Developer


View Profile WWW
February 20, 2014, 03:09:49 AM
 #3

Wow, I totally forgot this thread was here!  For anyone who stumbles on this, I posted some starter info on our webpage for using armoryengine:

https://bitcoinarmory.com/developers/python-scripting/

@newcn:  currently Armory only supports Bitcoin.  If your other coin is close enough to Bitcoin, you may only have to change the magic numbers:

https://github.com/etotheipi/BitcoinArmory/blob/master/armoryengine.py#L337

Founder and CEO of Armory Technologies, Inc.
Armory Bitcoin Wallet: Bringing cold storage to the average user!
Only use Armory software signed by the Armory Offline Signing Key (0x98832223)

Please donate to the Armory project by clicking here!    (or donate directly via 1QBDLYTDFHHZAABYSKGKPWKLSXZWCCJQBX -- yes, it's a real address!)
newcn
Full Member
***
Offline Offline

Activity: 143
Merit: 100


View Profile
February 20, 2014, 03:28:18 AM
 #4

thank you for your reply, I shall look into it and have a try

BTC:1NzzfeHCgN8fF6mSG1UeBFCVd2cxKbGyHk
NXT:13187911577562526278
oinquer
Member
**
Offline Offline

Activity: 94
Merit: 10


View Profile
March 26, 2014, 12:38:57 AM
 #5

was trying this....but keep getting an error.

Code:
Python 2.7.6 (default, Nov 10 2013, 19:24:24) [MSC v.1500 64 bit (AMD64)] on win
32
Type "help", "copyright", "credits" or "license" for more information.
>>> from armoryengine import *
(ERROR) armoryengine.py:12319 - No BDM method: StartCppLogging
(ERROR) Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "armoryengine.py", line 13518, in <module>
    TheBDM.StartCppLogging(cppLogFile, 3)
  File "armoryengine.py", line 12320, in __getattr__
    raise AttributeError
AttributeError

Error in sys.excepthook:
Traceback (most recent call last):
  File "armoryengine.py", line 618, in logexcept_override
    sys.__excepthook__(type, value, tback)
AttributeError: 'NoneType' object has no attribute '__excepthook__'

Original exception was:
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "armoryengine.py", line 13518, in <module>
    TheBDM.StartCppLogging(cppLogFile, 3)
  File "armoryengine.py", line 12320, in __getattr__
    raise AttributeError
AttributeError
>>>

I think i followed every step just like it says...any ideas?
vinboy
Member
**
Offline Offline

Activity: 77
Merit: 10


View Profile
April 07, 2014, 09:59:21 AM
 #6

Im getting some errors:
Error in sys.excepthook:
Traceback (most recent call last):
  File "/usr/lib/armory/armoryengine.py", line 618, in logexcept_override
    sys.__excepthook__(type, value, tback)
AttributeError: 'NoneType' object has no attribute '__excepthook__'


  File "/usr/lib/armory/armoryengine.py", line 10916, in <module>
    from jsonrpc import ServiceProxy, authproxy
ImportError: cannot import name authproxy

carl.byington
Newbie
*
Offline Offline

Activity: 13
Merit: 0


View Profile
October 06, 2017, 05:21:13 PM
 #7

I have a python script that generates a receiving address for an invoice.

Code:
wallet      = PyBtcWallet().readWalletFile(w)
receive     = wallet.getNextUnusedAddress()
parms       = urllib.urlencode({'amount':'%1.8f'%btc, 'label':comment})
url         = "bitcoin:%s?%s" % (receive.getAddrStr(), parms)
wallet.setComment(receive.getAddr160(), fullcomment)

Recently (0.96.x ??) that fails in getAddrStr(). Is there a replacement function?  I presume this is a side effect of the multiple address formats, segwit stuff.
Pages: [1]
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.19 | SMF © 2006-2009, Simple Machines Valid XHTML 1.0! Valid CSS!