npm package: @secux/app-luna
Description: SecuX Hardware Wallet LUNA API
Author: SecuX Technology Inc.
Latest version: 3.0.0

[![lerna](https://img.shields.io/badge/maintained%20with-lerna-cc00ff.svg)](https://lerna.js.org/)
[![view on npm](https://badgen.net/npm/v/@secux/app-luna](https://www.npmjs.com/package/@secux/app-luna)
[![npm module downloads](https://badgen.net/npm/dt/@secux/app-luna)](https://www.npmjs.org/package/@secux/app-luna)

# `@secux/app-luna`

> SecuX Hardware Wallet LUNA API

## Usage

```ts
import { SecuxLUNA } from "@secux/app-luna";
```

First, create instance of ITransport
- [Web Usb](https://www.npmjs.com/package/@secux/transport-webusb)
- [Web Bluetooth](https://www.npmjs.com/package/@secux/transport-webble)
- [React Native Bluetooth](https://www.npmjs.com/package/@secux/transport-reactnative)

<br />

## Examples
1. Get account address
```
const path = "m/44'/330'/0'/0/0";
const address = await device.getAddress(path);

/*

// transfer data to hardware wallet by custom transport layer
const data = SecuxLUNA.prepareAddress(path);
const response = await device.Exchange(data);
const address = SecuxLUNA.resolveAddress(response);

*/
```

2. Sign transaction
    - transfer asset (MsgSend)
    ```ts
    const signer = {
        path: "m/44'/330'/0'/0/0",
        accountNumber: 12345,
        sequence: 1,
    };
    const params = {
        fee: { uluna: 3000 },
        gasLimit: 12345,
    };
    const send = new SecuxLUNA.MsgSend(from, to, { uluna: 1e6 });

    const { multi_command, serialized } = await device.sign(
        [signer],
        [send],
        params
    );
    const responseList = [];
    for (const data of multi_command) {
        const rsp = await device.Exchange(data);
        responseList.push(rsp);
    }
    const raw_tx = SecuxLUNA.resolveTransaction(responseList, serialized);

    /*

    // transfer data to hardware wallet by custom transport layer.
    const { commands, serialized } = SecuxLUNA.prepareSign(
        [
            { ...signer, publickey: "02acb4bc267db7774614bf6011c59929b006c2554386a3090baff0b3fc418ec044" }
        ],
        [send],
        params
    });
    const responseList = [];
    for (const data of commands) {
        const rsp = await device.Exchange(data);
        responseList.push(rsp);
    }
    const raw_tx = SecuxLUNA.resolveTransaction(responseList, serialized);

    */
    ```

    - execute contract
    ```ts
    const swap = new SecuxLUNA.MsgExecuteContract(
        "terra1amdttz2937a3dytmxmkany53pp6ma6dy4vsllv", 
        "terra156v8s539wtz0sjpn8y8a8lfg8fhmwa7fy22aff", 
        {
            swap: {
                offer_asset: {
                    amount: 1e6,
                    info: {
                        native_token: { denom: "uluna" },
                    },
                },
            },
        },
        { uluna: 1e6 }
    );

    const { multi_command, serialized } = await device.sign(
        [signer],
        [swap],
        params
    );
    
    // ... (same as above)
    ```

    - delegate
    ```ts
    const delegate = new SecuxLUNA.MsgDelegate(
        "terra1amdttz2937a3dytmxmkany53pp6ma6dy4vsllv", 
        "terravaloper1vf2209f5y7s4a66n5ng7wmup5gcc2kghhzy89w", 
        "1000000"
    );

    const { multi_command, serialized } = await device.sign(
        [signer],
        [delegate],
        params
    );
    
    // ... (same as above)
    ```

    - withdraw
    ```ts
    const withdraw = new SecuxLUNA.MsgWithdrawDelegatorReward(
        "terra1amdttz2937a3dytmxmkany53pp6ma6dy4vsllv", 
        "terravaloper1vf2209f5y7s4a66n5ng7wmup5gcc2kghhzy89w" 
    );

    const { multi_command, serialized } = await device.sign(
        [signer],
        [withdraw],
        params
    );
    
    // ... (same as above)
    ```

    - undelegate
    ```ts
    const undelegate = new SecuxLUNA.MsgUndelegate(
        "terra1amdttz2937a3dytmxmkany53pp6ma6dy4vsllv", 
        "terravaloper1vf2209f5y7s4a66n5ng7wmup5gcc2kghhzy89w",
        "1000000"
    );

    const { multi_command, serialized } = await device.sign(
        [signer],
        [undelegate],
        params
    );
    
    // ... (same as above)
    ```

    - redelegate
    ```ts
    const redelegate = new SecuxLUNA.MsgBeginRedelegate(
        "terra1amdttz2937a3dytmxmkany53pp6ma6dy4vsllv", 
        "terravaloper1vf2209f5y7s4a66n5ng7wmup5gcc2kghhzy89w",
        "terravaloper1vk20anceu6h9s00d27pjlvslz3avetkvnwmr35",
        "1000000"
    );

    const { multi_command, serialized } = await device.sign(
        [signer],
        [redelegate],
        params
    );
    
    // ... (same as above)
    ```


# API Reference
LUNA package for SecuX device

**Kind**: global class  

* [SecuxLUNA](#SecuxLUNA)
    * [.addressConvert(publickey, type)](#SecuxLUNA.addressConvert) ⇒ <code>string</code>
    * [.prepareAddress(path)](#SecuxLUNA.prepareAddress) ⇒ [<code>communicationData</code>](#communicationData)
    * [.resolveAddress(response, type)](#SecuxLUNA.resolveAddress) ⇒ <code>string</code>
    * [.preparePublickey(path)](#SecuxLUNA.preparePublickey) ⇒ [<code>communicationData</code>](#communicationData)
    * [.resolvePublickey(response)](#SecuxLUNA.resolvePublickey) ⇒ <code>string</code>
    * [.prepareXPublickey(path)](#SecuxLUNA.prepareXPublickey) ⇒ [<code>communicationData</code>](#communicationData)
    * [.resolveXPublickey(response, path)](#SecuxLUNA.resolveXPublickey) ⇒ <code>string</code>
    * [.prepareSign(signers, messages, params)](#SecuxLUNA.prepareSign) ⇒ [<code>prepared</code>](#prepared)
    * [.resolveSignatureList(response)](#SecuxLUNA.resolveSignatureList) ⇒ <code>Array.&lt;string&gt;</code>
    * [.resolveTransaction(response, serialized)](#SecuxLUNA.resolveTransaction) ⇒ <code>string</code>
    * [.simulate(signers, messages, [params])](#SecuxLUNA.simulate) ⇒ <code>string</code>

<br/>
<a name="SecuxLUNA.addressConvert"></a>

### **SecuxLUNA.addressConvert(publickey, type) ⇒ <code>string</code>**
*Convert secp256k1 publickey to LUNA address.*

**Returns**: <code>string</code> - LUNA address  

| Param | Type | Description |
| --- | --- | --- |
| publickey | <code>string</code> \| <code>Buffer</code> | secp256k1 publickey |
| type | [<code>AddressType</code>](#AddressType) | account/validator/pubkey address |

<br/>
<a name="SecuxLUNA.prepareAddress"></a>

### **SecuxLUNA.prepareAddress(path) ⇒ [<code>communicationData</code>](#communicationData)**
*Prepare data for address generation.*

**Returns**: [<code>communicationData</code>](#communicationData) - data for sending to device  

| Param | Type | Description |
| --- | --- | --- |
| path | <code>string</code> | BIP32 path, ex: m/44'/330'/0'/0/0 |

<br/>
<a name="SecuxLUNA.resolveAddress"></a>

### **SecuxLUNA.resolveAddress(response, type) ⇒ <code>string</code>**
*Generate address from response data.*

**Returns**: <code>string</code> - LUNA address  

| Param | Type | Description |
| --- | --- | --- |
| response | [<code>communicationData</code>](#communicationData) | data from device |
| type | [<code>AddressType</code>](#AddressType) | account/validator/pubkey address |

<br/>
<a name="SecuxLUNA.preparePublickey"></a>

### **SecuxLUNA.preparePublickey(path) ⇒ [<code>communicationData</code>](#communicationData)**
*Prepare data for secp256k1 publickey.*

**Returns**: [<code>communicationData</code>](#communicationData) - data for sending to device  

| Param | Type | Description |
| --- | --- | --- |
| path | <code>string</code> | BIP32 path, ex: m/44'/330'/0'/0/0 |

<br/>
<a name="SecuxLUNA.resolvePublickey"></a>

### **SecuxLUNA.resolvePublickey(response) ⇒ <code>string</code>**
*Resolve secp256k1 publickey from response data.*

**Returns**: <code>string</code> - secp256k1 publickey (base64-encoded string)  

| Param | Type | Description |
| --- | --- | --- |
| response | [<code>communicationData</code>](#communicationData) | data from device |

<br/>
<a name="SecuxLUNA.prepareXPublickey"></a>

### **SecuxLUNA.prepareXPublickey(path) ⇒ [<code>communicationData</code>](#communicationData)**
*Prepare data for xpub.*

**Returns**: [<code>communicationData</code>](#communicationData) - data for sending to device  

| Param | Type | Description |
| --- | --- | --- |
| path | <code>string</code> | BIP32 path, ex: m/44'/330'/0'/0/0 |

<br/>
<a name="SecuxLUNA.resolveXPublickey"></a>

### **SecuxLUNA.resolveXPublickey(response, path) ⇒ <code>string</code>**
*Resolve xpub from response data.*

**Returns**: <code>string</code> - xpub  

| Param | Type | Description |
| --- | --- | --- |
| response | [<code>communicationData</code>](#communicationData) | data from device |
| path | <code>string</code> | BIP32 path, ex: m/44'/330'/0'/0/0 |

<br/>
<a name="SecuxLUNA.prepareSign"></a>

### **SecuxLUNA.prepareSign(signers, messages, params) ⇒ [<code>prepared</code>](#prepared)**
*Prepare data for signing.*


| Param | Type | Description |
| --- | --- | --- |
| signers | [<code>Signer</code>](#Signer) | array of signer |
| messages | [<code>Array.&lt;IMessage&gt;</code>](#IMessage) | each message represents a instruction |
| params | [<code>TxOption</code>](#TxOption) |  |

<br/>
<a name="SecuxLUNA.resolveSignatureList"></a>

### **SecuxLUNA.resolveSignatureList(response) ⇒ <code>Array.&lt;string&gt;</code>**
*Reslove signature from response data.*

**Returns**: <code>Array.&lt;string&gt;</code> - signature array of base64-encoded string  

| Param | Type | Description |
| --- | --- | --- |
| response | [<code>communicationData</code>](#communicationData) | data from device |

<br/>
<a name="SecuxLUNA.resolveTransaction"></a>

### **SecuxLUNA.resolveTransaction(response, serialized) ⇒ <code>string</code>**
*Serialize transaction wtih signature for broadcasting.*

**Returns**: <code>string</code> - signed raw transaction  

| Param | Type | Description |
| --- | --- | --- |
| response | [<code>communicationData</code>](#communicationData) \| [<code>Array.&lt;communicationData&gt;</code>](#communicationData) | data from device |
| serialized | [<code>communicationData</code>](#communicationData) |  |

<br/>
<a name="SecuxLUNA.simulate"></a>

### **SecuxLUNA.simulate(signers, messages, [params]) ⇒ <code>string</code>**
*Simulate a transaction for estimating gas.*

**Returns**: <code>string</code> - simulated transaction  

| Param | Type | Description |
| --- | --- | --- |
| signers | [<code>Array.&lt;Signer&gt;</code>](#Signer) | array of signer |
| messages | [<code>Array.&lt;IMessage&gt;</code>](#IMessage) | each message represents a instruction |
| [params] | [<code>TxOption</code>](#TxOption) |  |

<br/>

<br/>
<br/>
<a name="AddressType"></a>

## AddressType : <code>enum</code>
**Properties**

| Name | Type | Description |
| --- | --- | --- |
| account | <code>string</code> | account |
| validator | <code>string</code> | validator |
| pubkey | <code>string</code> | pubkey |

<br/>
<a name="Signer"></a>

## Signer : <code>object</code>
**Properties**

| Name | Type | Description |
| --- | --- | --- |
| path | <code>string</code> | BIP32 path, ex: m/44'/330'/0'/0/0 |
| publickey | <code>string</code> \| <code>Buffer</code> | secp256k1 publickey from `path` |
| sequence | <code>number</code> | the number of transactions sent from this address |
| accountNumber | <code>number</code> | the account number from blockchain |

<br/>
<a name="IMessage"></a>

## IMessage : <code>interface</code>
**Properties**

| Name | Type |
| --- | --- |
| toAmino | <code>function</code> | 
| toData | <code>function</code> | 
| toProto | <code>function</code> | 
| packAny | <code>function</code> | 

<br/>
<a name="TxOption"></a>

## TxOption : <code>object</code>
**Properties**

| Name | Type | Description |
| --- | --- | --- |
| fee | <code>string</code> \| <code>Coins</code> | the amount of coins to be paid as a fee |
| gasLimit | <code>number</code> | the maximum gas that can be used in transaction processing |
| [chainId] | <code>string</code> | blockchain network identifier |
| [memo] | <code>string</code> |  |
| [timeoutHeight] | <code>string</code> | timeout height relative to the current block height |
| [payer] | <code>string</code> | payer’s account address |
| [granter] | <code>string</code> | granter’s account address |

<br/>
<a name="prepared"></a>

## prepared : <code>object</code>
**Properties**

| Name | Type | Description |
| --- | --- | --- |
| commands | [<code>Array.&lt;communicationData&gt;</code>](#communicationData) | data for sending to device |
| serialized | [<code>communicationData</code>](#communicationData) | unsigned raw transaction |

<br/>

* * *

&copy; 2018-22 SecuX Technology Inc.

authors:<br/>
andersonwu@secuxtech.com