statebox / purescript-studio

Statebox studio + core + API + REST client
https://studio.statebox.cloud
4 stars 0 forks source link

Decoding from API using stbx-js #86

Open wires opened 5 years ago

wires commented 5 years ago

From https://hackmd.io/xtnHs1NtTcKGWjyq95Fgiw;

It should now be possible to decode transactions "DHT" (Distributed Hash Table) style.

Related issues:

Decode transactions from hash

usecase: when surf to statebox.cloud/##:hash we want see the wiring diagram on the left hand side in statebox studio.

encode wiring diagram in the browser

API:

baseUrl = https://us-central1-process-io.cloudfunctions.net

Return format

{ hash: "caca1234" // transaction SHA512 hash hex encoded
, tx: "cacabeefbabe2883" } // transaction hex encoded

Example:

<html>
    <head>

        <script src="//unpkg.com/@statebox/stbx-js"></script>
    </head>
    <body>
    <script>

        /* example wiring */
        const mynet = {
            name: "a",
            partition: [0, 1, 0, 1, 0, 0],
            names: ["x", "y"]
        }

        const mydiagram = {
            name: "z",
            width: 1,
            pixels: [1, 2],
            names: ["s", "t"]
        }

        const wiring = {
            nets: [mynet],
            diagrams: [mydiagram],
            // make nodes in the diagram point to net "a"
            labels: [0, 0]
        }

        const transaction = {
            wiring: wiring,
            previous: "deadbeef"
        }

        /* encoding and hashing the wiring */
        const thex = Stbx.encode(transaction)
        const thash = Stbx.hash(thex)
        console.log({thex, thash})

        /* retrieving by hash from the API */
        const hash = "bf3cbe71dec6d4ed99426b14a7d1e3da82df379d2229837f08757fef6304235bd0d2ef9d66c39de7110f66bd2c2ab67ef208f78912d4aae4ebad2ee13f674172"
        const u = `https://us-central1-process-io.cloudfunctions.net/tx/get/${hash}`
        fetch(u)
            .then(function(response) {
                return response.json();
            })
            .then(function(transactionData) {
                let tx = transactionData.tx
                let hash = transactionData.hash
                console.log({tx, hash})
                let w = Stbx.decode(tx)
                console.log(w)
            });
    </script>
    </body>
</html>
epost commented 5 years ago

Additional resources:

<script src="//unpkg.com/@statebox/stbx-js"></script>

...

Stbx.hash("bla")`
Stbx.decode("0a3f0a04696e697410001001100210001001100010001002100010001a0573746172741a0d737461727450726f64756365721a0d7374617274436f6e73756d65720a460a0870726f64756365721000100110001001100010021003100010021000100110001003100010001a0d737461727450726f64756365721a01611a01621a0770726f636573730a460a08636f6e73756d65721000100110001001100310001002100010021000100110001000100310001a0d7374617274436f6e73756d65721a01611a01621a0770726f6365737312140a04726f6f741003180118011802180218031803180018011802"
epost commented 5 years ago

@wires this is blocked; the following URL gives Error: could not handle the request: https://us-central1-process-io.cloudfunctions.net/tx/get/bf3cbe71dec6d4ed99426b14a7d1e3da82df379d2229837f08757fef6304235bd0d2ef9d66c39de7110f66bd2c2ab67ef208f78912d4aae4ebad2ee13f674172

update: The hackmd now contains updated URLs that don't give that error

epost commented 5 years ago

Some impl notes: