Skip to content

Commit

Permalink
feat: add api docs
Browse files Browse the repository at this point in the history
  • Loading branch information
jurevans committed Feb 27, 2024
1 parent a18b87f commit e27ca84
Show file tree
Hide file tree
Showing 8 changed files with 1,326 additions and 28 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
"eslint": "^8.49.0",
"git-commit-msg-linter": "^5.0.4",
"husky": "^8.0.3",
"jsdoc-to-markdown": "^8.0.1",
"lint-staged": "^15.2.0",
"prettier": "^3.1.0",
"prettier-plugin-organize-imports": "^3.2.4",
Expand Down
2 changes: 2 additions & 0 deletions packages/sdk/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,5 @@ async function init(): Promise<void> {

init();
```

For more information, read the [Specs](./docs/specs.md) or the [API](./docs/api.md) documentation.
235 changes: 235 additions & 0 deletions packages/sdk/docs/api.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,235 @@
## Classes

<dl>
<dt><a href="#Ledger">Ledger</a></dt>
<dd><p>Functionality for interacting with NamadaApp for Ledger Hardware Wallets</p></dd>
<dt><a href="#Mnemonic">Mnemonic</a></dt>
<dd><p>Class for accessing mnemonic functionality from wasm</p></dd>
<dt><a href="#Sdk">Sdk</a></dt>
<dd><p>API for interacting with Namada SDK</p></dd>
<dt><a href="#Signing">Signing</a></dt>
<dd><p>Non-Tx signing functions</p></dd>
</dl>

## Members

<dl>
<dt><a href="#initLedgerUSBTransport">initLedgerUSBTransport</a> ⇒ <code>Transport</code></dt>
<dd><p>Initialize HID transport</p></dd>
</dl>

## Functions

<dl>
<dt><a href="#initLedgerUSBTransport">initLedgerUSBTransport()</a> ⇒ <code>Transport</code></dt>
<dd><p>Initialize USB transport</p></dd>
</dl>

<a name="Ledger"></a>

## Ledger
<p>Functionality for interacting with NamadaApp for Ledger Hardware Wallets</p>

**Kind**: global class

* [Ledger](#Ledger)
* [new Ledger(namadaApp)](#new_Ledger_new)
* _instance_
* [.status()](#Ledger+status) ⇒ <code>LedgerStatus</code>
* [.getAddressAndPublicKey([path])](#Ledger+getAddressAndPublicKey) ⇒ <code>AddressAndPublicKey</code>
* [.showAddressAndPublicKey([path])](#Ledger+showAddressAndPublicKey) ⇒ <code>AddressAndPublicKey</code>
* [.sign(tx, [path])](#Ledger+sign) ⇒ <code>ResponseSign</code>
* [.queryErrors()](#Ledger+queryErrors) ⇒ <code>string</code>
* [.closeTransport()](#Ledger+closeTransport) ⇒ <code>void</code>
* _static_
* [.init([transport])](#Ledger.init)[<code>Ledger</code>](#Ledger)

<a name="new_Ledger_new"></a>

### new Ledger(namadaApp)

| Param | Type | Description |
| --- | --- | --- |
| namadaApp | <code>NamadaApp</code> | <p>Inititalized NamadaApp class from Zondax package</p> |

<a name="Ledger+status"></a>

### ledger.status() ⇒ <code>LedgerStatus</code>
<p>Return status and version info of initialized NamadaApp.
Throw exception if app is not initialized.</p>

**Kind**: instance method of [<code>Ledger</code>](#Ledger)
<a name="Ledger+getAddressAndPublicKey"></a>

### ledger.getAddressAndPublicKey([path]) ⇒ <code>AddressAndPublicKey</code>
<p>Get address and public key associated with optional path, otherwise, use default path
Throw exception if app is not initialized.</p>

**Kind**: instance method of [<code>Ledger</code>](#Ledger)

| Param | Type | Description |
| --- | --- | --- |
| [path] | <code>string</code> | <p>Bip44 path for deriving key</p> |

<a name="Ledger+showAddressAndPublicKey"></a>

### ledger.showAddressAndPublicKey([path]) ⇒ <code>AddressAndPublicKey</code>
<p>Prompt user to get address and public key associated with optional path, otherwise, use default path.
Throw exception if app is not initialized.</p>

**Kind**: instance method of [<code>Ledger</code>](#Ledger)

| Param | Type | Description |
| --- | --- | --- |
| [path] | <code>string</code> | <p>Bip44 path for deriving key</p> |

<a name="Ledger+sign"></a>

### ledger.sign(tx, [path]) ⇒ <code>ResponseSign</code>
<p>Sign tx bytes with the key associated with the provided (or default) path.
Throw exception if app is not initialized.</p>

**Kind**: instance method of [<code>Ledger</code>](#Ledger)

| Param | Type | Description |
| --- | --- | --- |
| tx | <code>Uint8Array</code> | <p>tx data blob to sign</p> |
| [path] | <code>string</code> | <p>Bip44 path for signing account</p> |

<a name="Ledger+queryErrors"></a>

### ledger.queryErrors() ⇒ <code>string</code>
<p>Query status to determine if device has thrown an error.
Throw exception if app is not initialized.</p>

**Kind**: instance method of [<code>Ledger</code>](#Ledger)
<a name="Ledger+closeTransport"></a>

### ledger.closeTransport() ⇒ <code>void</code>
<p>Close the initialized transport, which may be needed if Ledger needs to be reinitialized due to error state
Throw exception if app is not initialized.</p>

**Kind**: instance method of [<code>Ledger</code>](#Ledger)
<a name="Ledger.init"></a>

### Ledger.init([transport]) ⇒ [<code>Ledger</code>](#Ledger)
<p>Initialize and return Ledger class instance with initialized Transport</p>

**Kind**: static method of [<code>Ledger</code>](#Ledger)

| Param | Type | Description |
| --- | --- | --- |
| [transport] | <code>Transport</code> | <p>Ledger transport</p> |

<a name="Mnemonic"></a>

## Mnemonic
<p>Class for accessing mnemonic functionality from wasm</p>

**Kind**: global class

* [Mnemonic](#Mnemonic)
* [new Mnemonic(cryptoMemory)](#new_Mnemonic_new)
* [.generate([size])](#Mnemonic+generate) ⇒ <code>Array.&lt;string&gt;</code>
* [.toSeed(phrase, [passphrase])](#Mnemonic+toSeed) ⇒ <code>Uint8Array</code>
* [.validateMnemonic(phrase)](#Mnemonic+validateMnemonic) ⇒ <code>void</code>

<a name="new_Mnemonic_new"></a>

### new Mnemonic(cryptoMemory)

| Param | Type | Description |
| --- | --- | --- |
| cryptoMemory | <code>WebAssembly.Memory</code> | <p>Memory accessor for crypto lib</p> |

<a name="Mnemonic+generate"></a>

### mnemonic.generate([size]) ⇒ <code>Array.&lt;string&gt;</code>
<p>Generate a new 12 or 24 word mnemonic</p>

**Kind**: instance method of [<code>Mnemonic</code>](#Mnemonic)
**Returns**: <code>Array.&lt;string&gt;</code> - <p>array of words</p>

| Param | Type | Default | Description |
| --- | --- | --- | --- |
| [size] | <code>PhraseSize</code> | <code>12</code> | <p>Mnemonic length</p> |

<a name="Mnemonic+toSeed"></a>

### mnemonic.toSeed(phrase, [passphrase]) ⇒ <code>Uint8Array</code>
<p>Convert mnemonic to seed bytes</p>

**Kind**: instance method of [<code>Mnemonic</code>](#Mnemonic)

| Param | Type | Description |
| --- | --- | --- |
| phrase | <code>string</code> | |
| [passphrase] | <code>string</code> | <p>Bip39 passphrase</p> |

<a name="Mnemonic+validateMnemonic"></a>

### mnemonic.validateMnemonic(phrase) ⇒ <code>void</code>
<p>Validate a mnemonic string, raise an exception providing reason
for failure if invalid, otherwise return nothing</p>

**Kind**: instance method of [<code>Mnemonic</code>](#Mnemonic)

| Param | Type |
| --- | --- |
| phrase | <code>string</code> |

<a name="Sdk"></a>

## Sdk
<p>API for interacting with Namada SDK</p>

**Kind**: global class

* [Sdk](#Sdk)
* [new Sdk(sdk, query, cryptoMemory, url, nativeToken)](#new_Sdk_new)
* [.rpc](#Sdk+rpc) ⇒ <code>Rpc</code>
* [.tx](#Sdk+tx) ⇒ <code>Tx</code>
* [.mnemonic](#Sdk+mnemonic)[<code>Mnemonic</code>](#Mnemonic)
* [.keys](#Sdk+keys) ⇒ <code>Keys</code>
* [.signing](#Sdk+signing)[<code>Signing</code>](#Signing)
* [.init(url, [token])](#Sdk+init)[<code>Sdk</code>](#Sdk)
* [.getRpc()](#Sdk+getRpc) ⇒ <code>Rpc</code>
* [.getTx()](#Sdk+getTx) ⇒ <code>Tx</code>
* [.getMnemonic()](#Sdk+getMnemonic)[<code>Mnemonic</code>](#Mnemonic)
* [.getKeys()](#Sdk+getKeys) ⇒ <code>Keys</code>
* [.getSigning()](#Sdk+getSigning)[<code>Signing</code>](#Signing)
* [.initLedger([transport])](#Sdk+initLedger)[<code>Ledger</code>](#Ledger)

<a name="new_Sdk_new"></a>

### new Sdk(sdk, query, cryptoMemory, url, nativeToken)

| Param | Type | Description |
| --- | --- | --- |
| sdk | <code>SdkWasm</code> | <p>Instance of Sdk struct from wasm lib</p> |
| query | <code>QueryWasm</code> | <p>Instance of Query struct from wasm lib</p> |
| cryptoMemory | <code>WebAssembly.Memory</code> | <p>Memory accessor for crypto lib</p> |
| url | <code>string</code> | <p>RPC url</p> |
| nativeToken | <code>string</code> | <p>Address of chain's native token</p> |

<a name="Sdk+rpc"></a>

### sdk.rpc ⇒ <code>Rpc</code>
<p>Define rpc getter to use with destructuring assignment</p>

**Kind**: instance property of [<code>Sdk</code>](#Sdk)
<a name="Sdk+tx"></a>

### sdk.tx ⇒ <code>Tx</code>
<p>Define tx getter to use with destructuring assignment</p>

**Kind**: instance property of [<code>Sdk</code>](#Sdk)
<a name="Sdk+mnemonic"></a>

### sdk.mnemonic ⇒ [<code>Mnemonic</code>](#Mnemonic)
<p>Define mnemonic getter to use with destructuring assignment</p>

**Kind**: instance property of [<code>Sdk</code>](#Sdk)
<a name="Sdk+keys"></a>

### sdk.keys
5 changes: 5 additions & 0 deletions packages/sdk/docs/specs.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# @namada/sdk specs

TODO: Specs for `@namada/sdk` package

[API](./api.md)
20 changes: 20 additions & 0 deletions packages/sdk/jsdoc2md.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"source": {
"includePattern": ".+\\.ts(doc|x)?$",
"excludePattern": ".+\\.(test|spec).ts"
},
"plugins": ["plugins/markdown", "../../node_modules/jsdoc-babel"],
"babel": {
"extensions": ["ts"],
"ignore": ["**/*.(test|spec).ts"],
"babelrc": false,
"presets": [
["@babel/preset-env", { "targets": { "node": true } }],
"@babel/preset-typescript"
],
"plugins": [
"@babel/proposal-class-properties",
"@babel/proposal-object-rest-spread"
]
}
}
13 changes: 9 additions & 4 deletions packages/sdk/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
"wasm:build:multicore": "node ./scripts/build.js --release --multicore",
"wasm:build:dev": "node ./scripts/build.js",
"wasm:build:dev:multicore": "node ./scripts/build.js --multicore",
"wasm:build:node": "./scripts/build-node.sh"
"wasm:build:node": "./scripts/build-node.sh",
"build:docs": "npx jsdoc2md --files ./src/*.ts --configure ./jsdoc2md.json > ./docs/api.md"
},
"dependencies": {
"@cosmjs/encoding": "^0.29.0",
Expand All @@ -34,9 +35,12 @@
"typescript": "^5.1.3"
},
"devDependencies": {
"@babel/core": "^7.23.7",
"@babel/preset-env": "^7.23.8",
"@babel/preset-typescript": "^7.18.6",
"@babel/cli": "^7.23.9",
"@babel/core": "^7.23.9",
"@babel/plugin-proposal-class-properties": "^7.18.6",
"@babel/plugin-proposal-object-rest-spread": "^7.20.7",
"@babel/preset-env": "^7.23.9",
"@babel/preset-typescript": "^7.23.3",
"@release-it/conventional-changelog": "^8.0.1",
"@types/bn.js": "^5.1.1",
"@types/jest": "^29.0.3",
Expand All @@ -48,6 +52,7 @@
"eslint-plugin-import": "^2.27.5",
"eslint-plugin-react": "^7.33.0",
"jest": "^29.0.3",
"jsdoc-babel": "^0.5.0",
"release-it": "^17.0.1",
"rimraf": "^5.0.5",
"ts-jest": "^29.0.1",
Expand Down
22 changes: 11 additions & 11 deletions packages/sdk/src/ledger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ export class Ledger {
/**
* @param {NamadaApp} namadaApp - Inititalized NamadaApp class from Zondax package
*/
constructor(public readonly namadaApp: NamadaApp) {}
constructor(public readonly namadaApp: NamadaApp) { }

/**
* Initialize and return Ledger class instance with initialized Transport
Expand Down Expand Up @@ -95,17 +95,16 @@ export class Ledger {
* Get address and public key associated with optional path, otherwise, use default path
* Throw exception if app is not initialized.
* @async
* @param {string} [bip44Path] Bip44 path for deriving key
* @param {string} [path] Bip44 path for deriving key
* @returns {AddressAndPublicKey}
*/
public async getAddressAndPublicKey(
bip44Path?: string
path: string = DEFAULT_LEDGER_BIP44_PATH
): Promise<AddressAndPublicKey> {
if (!this.namadaApp) {
throw new Error("NamadaApp is not initialized!");
}

const path = bip44Path || DEFAULT_LEDGER_BIP44_PATH;
const { address, publicKey } =
await this.namadaApp.getAddressAndPubKey(path);

Expand All @@ -121,18 +120,16 @@ export class Ledger {
* Prompt user to get address and public key associated with optional path, otherwise, use default path.
* Throw exception if app is not initialized.
* @async
* @param {string} [bip44Path] Bip44 path for deriving key
* @param {string} [path] Bip44 path for deriving key
* @returns {AddressAndPublicKey}
*/
public async showAddressAndPublicKey(
bip44Path?: string
path: string = DEFAULT_LEDGER_BIP44_PATH
): Promise<AddressAndPublicKey> {
if (!this.namadaApp) {
throw new Error("NamadaApp is not initialized!");
}

const path = bip44Path || DEFAULT_LEDGER_BIP44_PATH;

try {
const { address, publicKey } =
await this.namadaApp.showAddressAndPubKey(path);
Expand All @@ -153,15 +150,17 @@ export class Ledger {
* Throw exception if app is not initialized.
* @async
* @param {Uint8Array} tx - tx data blob to sign
* @param {string} [bip44Path] Bip44 path for signing account
* @param {string} [path] Bip44 path for signing account
* @returns {ResponseSign}
*/
public async sign(tx: Uint8Array, bip44Path?: string): Promise<ResponseSign> {
public async sign(
tx: Uint8Array,
path: string = DEFAULT_LEDGER_BIP44_PATH
): Promise<ResponseSign> {
if (!this.namadaApp) {
throw new Error("NamadaApp is not initialized!");
}
const buffer = Buffer.from(tx);
const path = bip44Path || DEFAULT_LEDGER_BIP44_PATH;

return await this.namadaApp.sign(path, buffer);
}
Expand All @@ -185,6 +184,7 @@ export class Ledger {

/**
* Close the initialized transport, which may be needed if Ledger needs to be reinitialized due to error state
* Throw exception if app is not initialized.
* @async
* @returns {void}
*/
Expand Down
Loading

0 comments on commit e27ca84

Please sign in to comment.