# msafe-iframe

> iframe sdk of msafe

Latest version **1.1.7** (published 2022-12-12) · MIT license · 0 weekly downloads

## Install

```sh
npm install msafe-iframe
pnpm add msafe-iframe
yarn add msafe-iframe
bun add msafe-iframe
```

## Health

**Score 30/100 (F)** — status: abandoned.

Positive: has types; esm support; no vulnerabilities; high quality score.

Warnings: low downloads.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.1.7 |
| Published | 2022-12-12 |
| First published | 2022-10-29 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 2 |
| Unpacked size | 309.4 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | LeviHHH |
| Maintainers | levihhh |
| Keywords | Msafe, Msafe Iframe, Msafe SDK |

## Links

- npm: https://www.npmjs.com/package/msafe-iframe
- Repository: https://github.com/Momentum-Safe/msafe-iframe
- Issues: https://github.com/Momentum-Safe/msafe-iframe/issues
- npm.io page: https://npm.io/package/msafe-iframe

## Dependencies (2)

- [buffer](https://npm.io/package/buffer.md) ^6.0.3
- [json-rpc-protocol](https://npm.io/package/json-rpc-protocol.md) ^0.13.2

## Recent versions

- 1.1.7 (latest) — 2022-12-12
- 1.0.10 — 2022-12-10
- 1.1.6 — 2022-12-10
- 1.1.5 — 2022-12-09
- 1.1.4 — 2022-12-09
- 1.1.3 — 2022-12-09
- 1.1.2 — 2022-12-09
- 1.1.1 — 2022-12-09
- 1.1.0 — 2022-12-09
- 1.0.9 — 2022-11-24
- 1.0.8 — 2022-11-03
- 1.0.7 — 2022-11-03
- 1.0.6 — 2022-11-03
- 1.0.5 — 2022-10-30
- 1.0.4 — 2022-10-30
- … 4 more at https://npm.io/package/msafe-iframe/versions

## README

# Msafe Iframe SDK
Msafe Iframe SDK is used to integrate any dapp into msafe multi-sign wallet.  
The frontend of dapp will run in a sub-iframe of msafe, this SDK can be used for the interaction between dapp and msafe wallet. 

## Install

Installation of the [npm package]:

```
> npm install --save msafe-iframe
```

## Usage(Dapp Side)
### Init msafe wallet
You should initialize it once and use it later.
```typescript
import { MsafeWallet } from "msafe-iframe";
const msafe = await MsafeWallet.new();
```

### Connect/Disconnect to a msafe account
Connect/Disconnect to an account.
```typescript
import { MsafeWallet } from "msafe-iframe";
const msafe = await MsafeWallet.new();
const account = await msafe.connect(); // {addres:string, publicKey:string}
await msafe.isConnected(); // true
await msafe.disconnect();
await msafe.isConnected(); // false
```

### Get Network
Get current network.
```typescript
import { MsafeWallet } from "msafe-iframe";
const msafe = await MsafeWallet.new();
const network:string = await msafe.network(); // mainnet
```

### Get Account
Get current msafe account.
```typescript
import { MsafeWallet } from "msafe-iframe";
const msafe = await MsafeWallet.new();
const account:Account = await msafe.account();
console.log("address:", account.address);
console.log("public key:", account.publicKey);
```

### Get ChainId
Get current ChainId.
```typescript
import { MsafeWallet } from "msafe-iframe";
const msafe = await MsafeWallet.new();
const account = await msafe.chainId();  // 1
```

### Submit Transaction
Request a signature and send a transaction to the blockchain.
- `payload` - mandatory parameter containing the transaction body.
- `option` - optional parameter that overrides transaction parameters.
> - For arguments of type vector, you can pass in an array.
> - For `vector<u8>`, you can pass in `Uint8Array`.
> - You can also pass in a BCS serialized transaction as payload(`Uint8Array`), which ignores option.
```typescript
import { MsafeWallet } from "msafe-iframe";
const msafe = await MsafeWallet.new();
const payload = {
    function: "0x1::coin::transfer",
    type_arguments: ["0x1::aptos_coin::AptosCoin"],
    arguments: ["0x997b38d2127711011462bc42e788a537eae77806404769188f20d3dc46d72750", 50]
};
const option = {
   sender: account.address,
   sequence_number: "1",
   max_gas_amount: "4000",
   gas_unit_price: "1",
   // Unix timestamp, in seconds + 10 seconds
   expiration_timestamp_secs: (Math.floor(Date.now() / 1000) + 10).toString(),
}
const txid:Uint8Array = await msafe.signAndSubmit(payload, option); // 32 bytes tx hash
```

### Sign Transaction
Request a signature of transaction.
- `payload` - mandatory parameter containing the transaction body.
- `option` - optional parameter that overrides transaction parameters.
> - For arguments of type vector, you can pass in an array.
> - For `vector<u8>`, you can pass in `Uint8Array`.
> - You can also pass in a BCS serialized transaction as payload(`Uint8Array`), which ignores option.

```typescript
import { MsafeWallet } from "msafe-iframe";
const msafe = await MsafeWallet.new();
const payload = {
    function: "0x1::coin::transfer",
    type_arguments: ["0x1::aptos_coin::AptosCoin"],
    arguments: ["0x997b38d2127711011462bc42e788a537eae77806404769188f20d3dc46d72750", 50]
};
const option = {
   sender: msafeAddress.hex(),
   sequence_number: "1",
   max_gas_amount: "4000",
   gas_unit_price: "1",
   // Unix timestamp, in seconds + 10 seconds
   expiration_timestamp_secs: (Math.floor(Date.now() / 1000) + 10).toString(),
}
const signedTxn:Uint8Array = await msafe.signTransaction(payload, option); // BCS serialized signed transaction
```

### Sign Message
Unsupported now.

### Network Change Event
Network change event.
```typescript
import { MsafeWallet } from "msafe-iframe";
const msafe = await MsafeWallet.new();
msafe.onChangeAccount((network:string)=>{
    console.log("network change to:", network)
});
```

### Account Change Event
Account change event.
```typescript
import { MsafeWallet } from "msafe-iframe";
const msafe = await MsafeWallet.new();
msafe.onChangeAccount((account:Account)=>{
    console.log("address:", account.address);
    console.log("public key:", account.publicKey);
});
```

## Usage(Msafe Server Side)
### Accept dapp connection
```typescript
import { Connector,MsafeServer } from "msafe-iframe";
// cleaner is a function that used to remove listener.
const cleaner = Connector.accepts(dappUrl, (connector:Connector) => {

})
```
### Create Msafe Wallet Service
```typescript
import { Connector,MsafeServer,WalletAPI } from "msafe-iframe";
const connector:Connector = await Connector.accept(dappUrl);
const server = new MsafeServer(connector, {
    async connect(): Promise<Account> {
        // ...
        return {address:'0x1', publicKey:'0x1234...'};
    },
    // ... signAndSubmit(),signTransaction()
    async signMessage(
        message: string | Uint8Array
    ): Promise<Uint8Array> {
        throw Error("unsupport");
    },
} as WalletAPI);
```
### Emit Network Change Event
```typescript
import { Connector,MsafeServer,WalletAPI } from "msafe-iframe";
const server = new MsafeServer(...);
await server.changeNetwork('Testnet');
```
### Emit Account Change Event
```typescript
import { Connector,MsafeServer,WalletAPI } from "msafe-iframe";
const server = new MsafeServer(...);
await server.changeAccount({address:'0x1234...', publicKey:'0xabce...'});
```


## Development

```
# Install dependencies
> npm install

# Build
> npm run build

# Test
> npm run test

# Publish
> npm publish
```

[npm package]: https://www.npmjs.com/package/msafe-iframe

---
_Source: https://npm.io/package/msafe-iframe · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
