Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

algod REST API: Add support for algod /v2/teal/disassemble #702

Merged
merged 5 commits into from
Jan 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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