Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: update #4

Merged
merged 1 commit into from
Oct 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
106 changes: 55 additions & 51 deletions rfc/0046-walletconnect-namespaces/0047-walletconnect-namespaces.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,37 +101,8 @@ Get a Transaction[^3] over the provided instructions.

```ts
{
data: {
cell_deps: {
out_point: {
tx_hash: string
index: string
}
dep_type: string
}[]
header_deps: string[]
inputs: {
previous_output: {
index: string
tx_hash: string
}
since: string
}[]
outputs: {
capacity: string
lock: {
args: string
code_hash: string
hash_type: 'type' | 'data'
},
type: {
args: string
code_hash: string
hash_type: 'type' | 'data'
}
}[]
outputs_data: string[]
},
transaction: Transaction
actionType: 'sign' | 'signAndSend'
description: string
}
```
Expand All @@ -140,7 +111,8 @@ Get a Transaction[^3] over the provided instructions.

```ts
{
transaction: Transaction
transaction?: SignedTransaction
hash?: string
}
```

Expand Down Expand Up @@ -224,25 +196,57 @@ interface Address {

```ts
interface Transaction {
type: 'send' | 'receive' | 'create' | 'destroy'
createdAt: string
updatedAt: string
timestamp: string
value: string
hash: string
description: string
blockNumber: string
status: 'pending' | 'success' | 'failed'
nervosDao: boolean
sudtInfo?: {
sUDT: Record<'tokenID' | 'tokenName' | 'symbol' | 'decimal', string>
amount: string
}
nftInfo?: {
type: 'send' | 'receive'
data: string
}
assetAccountType?: 'CKB' | 'sUDT' | string
cellDeps: {
outPoint: {
txHash: string;
index: string;
};
depType: string;
}[];
headerDeps: string[];
inputs: {
previousOutput: {
txHash: string;
index: string;
};
since: string;
capacity: string;
lock: {
args: string;
codeHash: string;
hashType: 'type' | 'data';
};
lockHash: string;
}[];
outputs: {
capacity: string;
lock: {
args: string;
codeHash: string;
hashType: 'type' | 'data';
};
type: {
args: string;
codeHash: string;
hashType: 'type' | 'data';
};
}[];
outputsData: string[];
description: string;
[key: string]: any;
}
```

#### SignedTransaction

```ts
interface interface SignedTransaction {
transaction: Transaction;
signatures: {
[hash: string]: string[];
};
description: string;
[key: string]: any;
}
```

Expand Down
156 changes: 98 additions & 58 deletions sdk/ckb-walletconnect-wallet-sdk.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,86 +11,126 @@ WalletConnect provides a more secure and convenient experience for users and red
## Requirements
Create an account on Wallet Connect website and then create a new Project, it's super easy, with just a few fields on the form. After that, you will be able to get your projectId and use it on your wallet.

## Installation
Install the dependency on your client-side application
## Getting Started

### Install

```
npm i @nervosnetwork/ckb-walletconnect-wallet-sdk
npm install ckb-walletconnect-wallet-sdk
```

## Setup
Initialize the SDK with the following code:
```
import CkbWallet from '@nervosnetwork/ckb-walletconnect-wallet-sdk'

const ckbWallet = new CkbWallet({
clientOptions: {
projectId: '<project id>', // the ID of your project on Wallet Connect website
metadata: {
name: 'MyWalletnName',
description: 'My Wallet description',
url: 'https://mywalletdescription.app/',
icons: ['https://mywalletdescription.app/mywalleticon.png'],
},
},
method: {
ckb_getAddresses: <getAddresses function>,
ckb_signTransaction: <signTransaction function>,
ckb_signMessage: <signMessage function>,
},
})
### Usage

await ckbWallet.init()
```
- Initialization

## Using the SDK
```javascript
import { Core } from "@walletconnect/core";
import { CkbWallet, getSdkError, CKBWalletAdapter, GetAddressesParams, SignTransactionParams, SignMessageParams } from "ckb-walletconnect-wallet-sdk";

### Connect to the Dapp
Start the process of establishing a new connection. It will create a new proposal that needs to be accepted or rejected.
class Adapter implements CKBWalletAdapter {
async ckb_getAddresses(params: GetAddressesParams) {
// ...
}

```
await ckbWallet.connect(
'wc:1d47f6e0-b21c-4c06-8cfe-142926fa1740@1?bridge=https%3A%2F%2F0.bridge.walletconnect.org&key=55433f7778ae7437fcc0579828c770b0517f592b81a76b4c289d09eb964d7091'
)
```
async ckb_signTransaction(params: SignTransactionParams) {
// ...
}

### Disconnect from a specific dapp session
async ckb_signMessage(params: SignMessageParams) {
// ...
}
}

const core = new Core({
projectId: process.env.PROJECT_ID,
});

const walletSDK = await CkbWallet.init({
core,
metadata: {
name: "MyWalletnName",
description: "My Wallet description",
url: "https://mywalletdescription.com",
icons: ["https://mywalletdescription.com/mywalleticon.png"],
},
adapter: new Adapter()
});
```
const session = ckbWallet.sessions[0]
await ckbWallet.disconnect(session)

- Connect

```javascript
await walletSDK.connect(uri);
```
- Proposals

### Approve a specific connection proposal
```javascript
const proposals = walletSDK.proposals
```
const proposal = ckbWallet.proposals[0]
await ckbWallet.approveProposal(proposal, {
account: "ckb:testnet:ckt1qzda0cr08m85hc8jlnfp3zer7xulejywt49kt2rr0vthywaa50xwsqwh8z6fkne05j0emqeen59qnn8a6xkm3fs0xf9en"
})

- Sessions

```javascript
const sessions = walletSDK.sessions
```

### Reject a specific connection proposal
- Session Requests

```javascript
const requests = walletSDK.requests()
```
const proposal = ckbWallet.proposals[0]
await ckbWallet.rejectProposal(proposal)

- Session Approval

```javascript
const session = await walletSDK.approve({
id: proposal.id,
chain: 'testnet',
identity: 'xxxxxxxxxx',
scriptBases: ['0x9bd7e06f3ecf4be0f2fcd2188b23f1b9fcc88e5d4b65a8637b17723bbda3cce8']
});
```

### Approve a specific session request
- Session Rejection

```javascript
const session = await walletSDK.reject(proposal.id);
```
const request = ckbWallet.requests[0]
await ckbWallet.approveRequest(request)

- Session Disconnect

```javascript
await walletSDK.disconnect(session.topic);
```

### Reject a specific session request
- Responding to Session Requests

```javascript
// Approve
await walletSDK.approveRequest(request);

// or Reject
await walletSDK.rejectRequest(request);

```
const request = ckbWallet.requests[0]
await ckbWallet.rejectRequest(request)

- Emit Session Events

```javascript
await walletSDK.emitSessionEvent({
topic,
event: {
name: "accountsChanged",
data: ["xxxxxxxxxx"],
},
chainId: "ckb:testnet",
});
```

### Emit a event to a specific dapp session
- Handle Events

```javascript
walletSDK.emitter.on("proposals", handler);
walletSDK.emitter.on("sessions", handler);
walletSDK.emitter.on("requests", handler);
```
const session = ckbWallet.sessions[0]
await ckbWallet.emitEvent(session, {
name: 'chainChanged',
data: ['ckb:testnet']
})
```