Skip to content

Commit

Permalink
Merge pull request #66 from randlabs/tealSign
Browse files Browse the repository at this point in the history
Add tealSign method
  • Loading branch information
Martin Picco committed Jun 2, 2022
2 parents c6e69e0 + 3d933a6 commit 41844ce
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 2 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# MyAlgo Connect Change log

## [1.2.0] - 2022-06-02

### Added

- Added new function `tealSign` to generate `ed25519` signatures that can be verified by a teal program. See also: [ed25519verify opcode](https://developer.algorand.org/docs/get-details/dapps/avm/teal/opcodes/#ed25519verify) and [algosdk.tealSign](https://algorand.github.io/js-algorand-sdk/modules.html#tealSign)

## [1.1.3] - 2022-04-13

### Added
Expand Down
10 changes: 10 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -208,4 +208,14 @@ export default class MyAlgoConnect {
* @returns Returns signed teal
*/
signLogicSig(logic: Uint8Array | Base64, address: Address): Promise<Uint8Array>;

/**
* @async
* @description Creates a signature the data that can later be verified by the contract through the ed25519verify opcode
* @param data Arbitrary data to sign
* @param contractAddress Contract address/TEAL program hash
* @param address Signer Address
* @returns Returns the data signature
*/
tealSign(data: Uint8Array | Base64, contractAddress: Address, address: Address): Promise<Uint8Array>;
}
57 changes: 57 additions & 0 deletions lib/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,13 @@ class MyAlgoConnect {
*/
this.currentSignLogicSigPopup = null;

/**
* @access private
* @description This is used to reuse the current tealsign opened popup
* @type {Window|null}
*/
this.currentTealSignPopup = null;

/**
* @access private
* @description Replace default bridge options
Expand Down Expand Up @@ -424,6 +431,56 @@ class MyAlgoConnect {
}
}

/**
* @async
* @access public
* @description Open a new window to sign data to verify in a teal program.
* @param {Uint8Array|Base64} data Data to sign
* @param {Address} contractAddress Address of the contract that will verify the data
* @param {Address} address Signer Address
* @returns {Uint8Array} Returns data signature
*/
async tealSign(data, contractAddress, address) {

if (this.currentTealSignPopup) {
if (this.currentTealSignPopup.closed) {
this.currentTealSignPopup = null;
}
else {
this.focusWindow(this.currentTealSignPopup);
}
}

try {
this.currentTealSignPopup = openPopup(this.url + "/tealsign.html");
await this.waitForWindowToLoad(this.currentTealSignPopup);

// Send program
let dataInBase64 = data;
if (data.constructor === Uint8Array)
dataInBase64 = Buffer.from(data).toString("base64");

const res = await this.bridge.sendMessage(
this.currentTealSignPopup,
{ method: "tealsign", params: { data: dataInBase64, contractAddress: contractAddress, address } },
this.url, this.options
);

this.closeWindow(this.currentTealSignPopup);
this.currentTealSignPopup = null;

if (res.status === "error")
throw new Error(res.message);

return new Uint8Array(Buffer.from(res.data.signature, "base64"));
}
catch (err) {
this.closeWindow(this.currentTealSignPopup);
this.currentTealSignPopup = null;
throw err;
}
}

/**
* @async
* @access private
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@randlabs/myalgo-connect",
"version": "1.1.3",
"version": "1.2.0",
"description": "",
"main": "index.js",
"types": "index.d.ts",
Expand Down

0 comments on commit 41844ce

Please sign in to comment.