Skip to content

Commit

Permalink
Merge pull request #13 from randlabs/MAC-147
Browse files Browse the repository at this point in the history
Docs for tealSign method
  • Loading branch information
Martin Picco authored Jun 2, 2022
2 parents 4a332bd + bf90b69 commit d1e37e9
Show file tree
Hide file tree
Showing 7 changed files with 206 additions and 20 deletions.
50 changes: 39 additions & 11 deletions docs/getting-started/myalgo-connect-api.md
Original file line number Diff line number Diff line change
Expand Up @@ -174,21 +174,49 @@ signLogicSig(logic: Uint8Array | Base64, address: Address): Promise<Uint8Array>;

#### Params

`Logic`: TEAL program to be signed by the user.
- `Logic`: TEAL program to be signed by the user.

`Address`: Signer’s Address.
- `Address`: Signer’s Address.

#### Response

```json
// Uint8Array
{
"0": 248,
"1": 77,
"2": 132,
...
"61": 28,
"62": 131,
"63": 14
}
[ 248, 77, 132, ..., 28, 131, 14]
```

## tealSign()

Sign an arbitrary piece of data which can be verified by a smart contract through the [ed25519verify](https://developer.algorand.org/docs/get-details/dapps/avm/teal/opcodes/#ed25519verify) opcode.

See also: [algosdk tealSign](https://algorand.github.io/js-algorand-sdk/modules.html#tealSign)

#### Signature and types

```jsx
export type Address = string;
export type Base64 = string;

tealSign(data: Uint8Array | Base64, contractAddress: Address, address: Address): Promise<Uint8Array>;
```

#### Params

- `data`: The arbitrary piece of data to be signed

- `contractAddress`: Contract address/TEAL program hash, which can verify the signature.

- `address`: Signer address

#### Response

Returns the signature of the data

```js
// Uint8Array
[ 248, 77, 132, ..., 28, 131, 14 ]
```

#### Considerations

- This operation is supported only by mnemonic accounts. Ledger accounts are not supported.
18 changes: 18 additions & 0 deletions docs/interactive-examples/TealSign.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
---
sidebar_position: 12
---

import TealSignExample from "../../src/components/TealSignExample"

# Sign arbitrary data for teal program

Sign an arbitrary piece of data which can be verified by a smart contract through the [ed25519verify](https://developer.algorand.org/docs/get-details/dapps/avm/teal/opcodes/#ed25519verify) opcode.

See also: [algosdk tealSign](https://algorand.github.io/js-algorand-sdk/modules.html#tealSign)

### Preconditions

* The user has already shared the account to sign the data with.

### Interactive Example
<TealSignExample />
24 changes: 20 additions & 4 deletions package-lock.json

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

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "homepage",
"version": "1.1.4",
"version": "1.2.0",
"private": true,
"scripts": {
"docusaurus": "docusaurus",
Expand All @@ -17,7 +17,7 @@
"@docusaurus/core": "^2.0.0-beta.3",
"@docusaurus/preset-classic": "^2.0.0-beta.3",
"@mdx-js/react": "^1.6.21",
"@randlabs/myalgo-connect": "^1.1.3",
"@randlabs/myalgo-connect": "^1.2.0",
"@svgr/webpack": "^5.5.0",
"algosdk": "^1.10.1",
"bootstrap": "^5.0.2",
Expand Down
3 changes: 2 additions & 1 deletion sidebars.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ module.exports = {
'interactive-examples/connect','interactive-examples/PaymentTransaction', 'interactive-examples/GroupedTransaction',
'interactive-examples/ASATransacation', 'interactive-examples/ApplOptIn', 'interactive-examples/ApplCloseOut',
'interactive-examples/PaymentWithTeal', 'interactive-examples/ApplCreateTransaction', 'interactive-examples/ApplDeleteTransaction',
'interactive-examples/ApplUpdateTransaction', 'interactive-examples/PaymentTransactionOverrideSigner'
'interactive-examples/ApplUpdateTransaction', 'interactive-examples/PaymentTransactionOverrideSigner',
'interactive-examples/TealSign'
],
},
],
Expand Down
122 changes: 122 additions & 0 deletions src/components/TealSignExample.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
import ExecutionEnvironment from '@docusaurus/ExecutionEnvironment';
import React, { useContext, useState } from "react";
import { Button, Col, Label, Nav, NavItem, NavLink, Row, TabContent, TabPane } from "reactstrap";
import PreLoadDataContextComponent, { PreLoadDataContext } from '../context/preLoadedData';
import Address from "./commons/Address";
import PrismCode from './commons/Code';
import AccountDropdown from "./commons/FromDropdown";
import Note from "./commons/Note";
import "./interactive-examples.scss";

const tealSignCode =
`
import MyAlgoConnect from '@randlabs/myalgo-connect';
const myAlgoConnect = new MyAlgoConnect();
const data = new Uint8Array([...]);
const contractAddress = '46Q...';
const signer = 'MKJ...';
const signature = await myAlgoConnect.tealSign(data, contractAddress, signer);
`

function TealSignExample(): JSX.Element {
const preLoadedData = useContext(PreLoadDataContext);
const accountslist = ExecutionEnvironment.canUseDOM && window.sharedAccounts && Array.isArray(window.sharedAccounts) ? window.sharedAccounts : [];
const [accounts, setAccount] = useState(accountslist);
const [accountSelected, selectAccount] = useState("");
const [data, setData] = useState<Uint8Array | undefined>();
const [contractAddress, setContractAddress] = useState("");
const [response, setResponse] = useState<string>("");
const [activeTab, setActiveTab] = useState('1');

const toggle = (tab: React.SetStateAction<string>) => {
if (activeTab !== tab) setActiveTab(tab);
}

const onSignData = async () => {
try {
const signature = await preLoadedData.myAlgoWallet.tealSign(data || [], contractAddress, accountSelected);

setResponse(Buffer.from(signature).toString('base64'));
}
catch (err) {
console.error(err)
setResponse(JSON.stringify(err, null, 4));
}
}

return (
<div className="interactive-example">
<Nav tabs>
<NavItem>
<NavLink
className={activeTab === '1' ? "active active-tab" : ""}
onClick={() => { toggle('1'); }}>
Form
</NavLink>
</NavItem>
<NavItem>
<NavLink
className={activeTab === '2' ? "active active-tab" : ""}
onClick={() => { toggle('2'); }}>
Code
</NavLink>
</NavItem>
</Nav>
<TabContent activeTab={activeTab}>
<TabPane tabId="1">
<Row className="mt-3">
<Col xs="12" lg="6" className="mt-2">
<AccountDropdown onSelectAccount={selectAccount} accounts={accounts} />
<Address label="Contract Address" onChangeAddress={setContractAddress} />
<Note label="Data" onChangeNote={setData} />
<Button color="primary" block className="mt-2" onClick={onSignData} disabled={accounts.length === 0}>
Sign
</Button>
</Col>
<Col xs="12" lg="6" className="mt-2 mt-xs-2">
<Label className="tx-label">
Response
</Label>
<div className="response-base txn-payment-response">
<PrismCode
code={response}
language="js"
plugins={["response"]}
/>
</div>
<Button
className="button-margin"
color="primary"
block
disabled={!response}
onClick={() => setResponse("")}>
Clear Response
</Button>
</Col>
</Row>
{accounts.length === 0 &&
<div className="error-connect mt-3"> In order to run this example, you need to execute connect() method. </div>
}
</TabPane>
<TabPane tabId="2">
<Row className="mt-3">
<Col>
<Label className="tx-label">
Sign data for teal program
</Label>
<PrismCode
code={tealSignCode}
language="js"
/>
</Col>
</Row>
</TabPane>
</TabContent>
</div>
)
}

export default () => <PreLoadDataContextComponent><TealSignExample /></PreLoadDataContextComponent>;
5 changes: 3 additions & 2 deletions src/components/commons/Note.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { FormGroup, Input, Label } from "reactstrap";
interface NoteProps {
disabled?: boolean;
onChangeNote(note: Uint8Array): void;
label?: string
}

export default function Note(props: NoteProps): JSX.Element {
Expand All @@ -17,12 +18,12 @@ export default function Note(props: NoteProps): JSX.Element {
return <Fragment>
<FormGroup>
<Label className="tx-label">
Note
{props.label || "Note"}
</Label>
<Input
className="tx-input note"
type="textarea"
placeholder="Note"
placeholder={props.label || "Note"}
value={note}
onChange={onChangeNote}
disabled={props.disabled}
Expand Down

0 comments on commit d1e37e9

Please sign in to comment.