Skip to content

Commit

Permalink
algod REST API: Add support for algod /v2/teal/disassemble (#702)
Browse files Browse the repository at this point in the history
* Add test for algod /v2/teal/disassemble

* Merge develop into PR (#736)

* ABI:  Refactor ABI encoding test to round-trip (#701)

* bump version and add to changelog

* update README.md for new version

* Packaging: Improve source map and browser usage for external bundlers (#707)

* bump version and add to changelog

* update README.md for new version

* v2: Make breaking changes from v1 to v2.0.0  (#717)

* bump version and add to changelog

* update README.md for new version

* remove enhancement section of recent changelog

* Enhancement: Add foreign array objects to ATC `addMethodCall` (#725)

* Add foreign array objects to ATC addmethodcall

* Copy array value so that inputs are not modified

Co-authored-by: Michael Diamant <[email protected]>
Co-authored-by: Lucky Baar <[email protected]>
Co-authored-by: Jason Paulos <[email protected]>
Co-authored-by: Jack Smith <[email protected]>
Co-authored-by: Barbara Poon <[email protected]>

* Explicitly import Buffer

* Revert test branch back to master

Co-authored-by: algochoi <[email protected]>
Co-authored-by: Lucky Baar <[email protected]>
Co-authored-by: Jason Paulos <[email protected]>
Co-authored-by: Jack Smith <[email protected]>
Co-authored-by: Barbara Poon <[email protected]>
  • Loading branch information
6 people authored Jan 18, 2023
1 parent e2f42c1 commit 1ee2ba3
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
.DS_Store

.idea/
# Webstorm
*.iml
.vscode/*
!.vscode/settings.json
!.vscode/extensions.json
Expand Down
17 changes: 17 additions & 0 deletions src/client/v2/algod/algod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import {
} from '../../urlTokenBaseHTTPClient';
import LightBlockHeaderProof from './lightBlockHeaderProof';
import StateProof from './stateproof';
import Disassemble from './disassemble';

/**
* Algod client connects an application to the Algorand blockchain. The algod client requires a valid algod REST endpoint IP address and algod token from an Algorand node that is connected to the network you plan to interact with.
Expand Down Expand Up @@ -397,6 +398,22 @@ export default class AlgodClient extends ServiceClient {
return new Compile(this.c, source);
}

/**
* Given the program bytes, return the TEAL source code in plain text.
*
* #### Example
* ```typescript
* const bytecode = "TEAL bytecode";
* const disassembledSource = await algodClient.disassemble(bytecode).do();
* ```
*
* @remarks This endpoint is only enabled when a node's configuration file sets EnableDeveloperAPI to true.
* @param source
*/
disassemble(source: string | Uint8Array) {
return new Disassemble(this.c, source);
}

/**
* Provides debugging information for a transaction (or group).
*
Expand Down
46 changes: 46 additions & 0 deletions src/client/v2/algod/disassemble.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { Buffer } from 'buffer';
import JSONRequest from '../jsonrequest';
import HTTPClient from '../../client';

/**
* Sets the default header (if not previously set)
* @param headers - A headers object
*/
export function setHeaders(headers = {}) {
let hdrs = headers;
if (Object.keys(hdrs).every((key) => key.toLowerCase() !== 'content-type')) {
hdrs = { ...headers };
hdrs['Content-Type'] = 'text/plain';
}
return hdrs;
}

/**
* Executes disassemble
*/
export default class Disassemble extends JSONRequest {
constructor(c: HTTPClient, private source: string | Uint8Array) {
super(c);
this.source = source;
}

// eslint-disable-next-line class-methods-use-this
path() {
return `/v2/teal/disassemble`;
}

/**
* Executes disassemble
* @param headers - A headers object
*/
async do(headers = {}) {
const txHeaders = setHeaders(headers);
const res = await this.c.post(
this.path(),
Buffer.from(this.source),
txHeaders,
this.query
);
return res.body;
}
}
1 change: 1 addition & 0 deletions tests/cucumber/integration.tags
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
@auction
@c2c
@compile
@compile.disassemble
@compile.sourcemap
@dryrun
@kmd
Expand Down
14 changes: 14 additions & 0 deletions tests/cucumber/steps/steps.js
Original file line number Diff line number Diff line change
Expand Up @@ -4550,6 +4550,20 @@ module.exports = function getSteps(options) {
}
);

Then(
'disassembly of {string} matches {string}',
async function (bytecodeFilename, sourceFilename) {
const bytecode = await loadResource(bytecodeFilename);
const resp = await this.v2Client.disassemble(bytecode).do();
const expectedSource = await loadResource(sourceFilename);

assert.deepStrictEqual(
resp.result.toString('UTF-8'),
expectedSource.toString('UTF-8')
);
}
);

When(
'we make a GetLightBlockHeaderProof call for round {int}',
async function (int) {
Expand Down

0 comments on commit 1ee2ba3

Please sign in to comment.