Exemplo n.º 1
0
def init(seed=None):
    token = None
    json_file = os.path.join(ROOT, ".json", "blockfrost.token")
    tokens = data.loadJson(json_file)

    available = [t for t in tokens if tokens[t] == rest.cfg.network]
    if available:
        try:
            sys.stdout.write("Available token [0=last]:\n")
            for t in available:
                sys.stdout.write(" - %d : %s\n" % (available.index(t)+1, t))
            token = available[int(input("Select token number > "))-1]
        except Exception:
            pass
        else:
            sys.stdout.write("Blockfrost account: %s\n" % token)

    if not token:
        token = input("Paste your blockfrost.io token > ")

    cfg.headers["project_id"] = token

    check = rest.GET.health.clock().get("status", False) == 200
    if check:
        tokens[token] = rest.cfg.network
        data.dumpJson(tokens, json_file)

    return check
Exemplo n.º 2
0
def _storage_clean_up():
    if not hasattr(rest.cfg, "pubkeyHash"):
        return

    path = os.path.join(dposlib.ROOT, ".registry", rest.cfg.network)
    if not os.path.exists(path):
        return

    for name in os.listdir(path):
        address = dposlib.core.crypto.getAddress(name)
        filepath = os.path.join(path, name)
        nonce = int(
            rest.GET.api.wallets(address).get("data", {}).get("nonce", 0))

        data = loadJson(filepath)
        msg = []
        for txid in list(data.keys()):
            if data[txid].get("nonce", 0) <= nonce:
                data.pop(txid)
                msg.append(txid)

        if len(data):
            dumpJson(data, filepath)
        else:
            os.remove(filepath)

        if len(msg):
            flask.flash("%s - nonce expiration cleanup:\n%s" %
                        (rest.cfg.network, "\n   ".join(msg)))
Exemplo n.º 3
0
 def dump(self):
     """Dumps transaction in current registry."""
     if "id" in self:
         id_ = self["id"]
         pathfile = Transaction.path()
         registry = loadJson(pathfile)
         registry[self["id"]] = self
         dumpJson(registry, pathfile)
Exemplo n.º 4
0
 def create(peer, event, target, conditions):
     data = dposlib.rest.POST.api.webhooks(
         peer=peer, event=event, target=target, conditions=conditions,
         returnKey="data"
     )
     if "token" in data:
         dumpJson(data, os.path.join(
             dposlib.ROOT,
             ".webhooks",
             dposlib.rest.cfg.network, data["token"][32:]
         ))
     return Webhook(data["id"], peer=peer)
Exemplo n.º 5
0
def pop(network, tx):
    """
    Remove a transaction from registry. Wallet registry is removed if empty.

    Args:
        network (:class:`str`): blockchain name
        publicKey (:class:`str`): encoded-compresed public key as hex string
    """
    path = os.path.join(dposlib.ROOT, ".registry", network,
                        tx["senderPublicKey"])
    registry = loadJson(path)
    tx.pop("id", False)
    registry.pop(identify(tx), False)
    if not (len(registry)):
        os.remove(path)
    else:
        dumpJson(registry, path)
Exemplo n.º 6
0
def dump(network, tx):
    """
    Add a transaction into registry. ``senderPublicKey`` field is used to
    create registry if it does not exist.

    Args:
        network (:class:`str`):
            blockchain name
        tx (:class:`dict` or :class:`dposlib.blockchain.Transaction`):
            transaction to store
    """
    path = os.path.join(dposlib.ROOT, ".registry", network,
                        tx["senderPublicKey"])
    registry = loadJson(path)
    tx.pop("id", False)
    id_ = identify(tx)
    registry[id_] = tx
    dumpJson(registry, path)
    return id_
Exemplo n.º 7
0
def _ark_srv_synch():
    data = {}
    if not hasattr(rest.cfg, "pubkeyHash"):
        return
    path = os.path.join(SYNCH_FOLDER, "data.%d" % rest.cfg.pubkeyHash)
    try:
        for server in loadJson(os.path.join(__path__[0], "server.json")):
            req = rest.GET.transactions(peer=server)
            if req:
                pendings = [
                    _fix_tx(elem["data"]) for elem in req
                    if elem["data"]["network"] == rest.cfg.pubkeyHash
                ]
                data.update(
                    dict([tx["senderPublicKey"], {
                        identify(tx): tx
                    }] for tx in pendings))
                dumpJson(data, path)
    except Exception as error:
        sys.stdout.write("%r\n" % error)
Exemplo n.º 8
0
 def create(peer, event, target, *conditions):
     conditions = [(Webhook.condition(cond) if isinstance(cond, str) else
                    cond if isinstance(cond, dict) else {})
                   for cond in conditions]
     data = dposlib.rest.POST.api.webhooks(
         peer=peer,
         event=event,
         target=target,
         conditions=[cond for cond in conditions if cond],
         returnKey="data")
     if "token" in data:
         print("security token :", data["token"])
         # build the security hash and keep only second token part and
         # save the used peer to be able to delete it later
         data["dump"] = Webhook.dump(data.pop("token"))
         data["peer"] = peer
         dumpJson(
             data,
             os.path.join(dposlib.ROOT, ".webhooks",
                          dposlib.rest.cfg.network, data["id"] + ".json"))
         return Webhook(data["id"], peer=peer)
     else:
         raise Exception("webhook not created")
Exemplo n.º 9
0
def createWebhook(peer, event, target, conditions, folder=None):
	data = rest.POST.api.webhooks(peer=peer, event=event, target=target, conditions=conditions, returnKey="data")
	if "token" in data:
		dumpJson(data, os.path.join(ROOT if not folder else folder, "%s.whk" % data["token"]))
	return data