Simple Python watch-only login example

Here we demonstrate how you can implement a simple Python application which logs in to GreenAddress wallet using custom login, retrieves a P2SH deposit address, and logs out.

Requirements

To implement this example, we use the following libraries, so make sure you install them first:

Overview

To log in to a GreenAddress wallet using custom login, you need a the username and password to the watch_only() call. To retrieve a P2SH deposit address, you simply call fund().

Using the GreenAddress WAMP server

Logging in

You can log in using custom login:

from twisted.internet import defer, reactor
from autobahn.wamp.auth import compute_wcs
from autobahn.wamp.types import CallOptions
from autobahn.twisted.wamp import ApplicationSession, ApplicationRunner

class GreenAddressClientProtocol(ApplicationSession):

    def __init__(self):
        super(GreenAddressClientProtocol, self).__init__()

    def onConnect(self):
        """
        Implements :func:`autobahn.wamp.interfaces.ISession.onConnect`
        """
        self.join(
            u"realm1"
        )

    @defer.inlineCallbacks
    def onJoin(self, data):
        yield self.call(
            "com.greenaddress.login.watch_only",
            "custom",
            {"username": "[your username]",
             "password": "[your password]"},
            False
        )
        hex_script = yield self.call(
            "com.greenaddress.vault.fund",
            options=CallOptions(disclose_me=True)
        )
        print_p2sh(hex_script)  # print_p2sh is defined in next step below
        reactor.stop()


if __name__ == '__main__':
    factory = ApplicationRunner(
        u"wss://prodwss.greenaddress.it/v2/ws/",
        u"realm1",
        # debug_wamp=True,  # optional; log many WAMP details
        # debug=True,  # optional; log even more details
    )

    factory.run(
        make=lambda *args: GreenAddressClientProtocol()
    )

P2SH generation

The fund() call returns a hex-encoded script, so it needs to be hashed to convert it to a P2SH address:

import binascii
from pycoin.encoding import hash160, hash160_sec_to_bitcoin_address

def print_p2sh(hex_script):
   print hash160_sec_to_bitcoin_address(
      hash160(binascii.unhexlify(hex_script)), address_prefix=b'\5')