Getting all choices for a given Party or Contract

You can also do what the Navigator does in the background: Decode the Daml package and interpret its contents.

You can download the Daml package (DALF) you need using the JSON API. As documented here, DALF packages are just binary encoded protobuf messages from the Daml-LF schema.

You can use protobuf.js to decode DALFs, thought to make it comfortable, you’ll want to load the proto schema files, which you can download on GitHub releases. You may need to hack around a bit to deal with relative vs absolute paths in the proto files and rename the constructor field in Enum since protobuf.js doesn’t deal with it, but once you put it all together, you can get your hands on the payload as a JS object, and then access template, choice, and type information dynamically.

A PoC grade way of getting at the package info here:

<head>
    <script src="https://cdn.rawgit.com/dcodeIO/protobuf.js/6.11.3/dist/protobuf.js"></script>
</head>
<body>
    <h1>Daml Archive Reader</h1>
    <p></p>

    <script>
        document.getElementsByTagName('p')[0].innerText = "foo";

        function withArchive(path, callback) {
            var req = new XMLHttpRequest();
            req.open("GET", path, true);
            req.responseType = 'arraybuffer';
            req.onload = function(e) {
                if (this.status == 200) {
                    callback(new Uint8Array(this.response)); 
                }
            }
            req.send();
        }
        
        protobuf.load("protos-2.2.0/com/daml/daml_lf_1_14/daml_lf.proto", function(err, root) {
            if (err)
                throw err;

            withArchive(".daml/dist/create-daml-app-0.1.0-1f477a11f7fd9914437b909e89ea15775e705e182d2296dfa60fb0414fb0b74c/create-daml-app-0.1.0-1f477a11f7fd9914437b909e89ea15775e705e182d2296dfa60fb0414fb0b74c.dalf", function(buffer) {
                // Get the archive
                var Archive = root.lookupType("daml_lf_1_13.Archive");
                var archive = Archive.decode(buffer);
                // Get the payload

                var Payload = root.lookupType("daml_lf_1_13.ArchivePayload");
                var payload = Payload.decode(archive.payload);

                document.getElementsByTagName('p')[0].innerText = JSON.stringify(payload);
            })
        });

    </script>
</body>
3 Likes