Skip to content

Commit

Permalink
refactor(wallet-rpc)!: rewrite to TypeScript
Browse files Browse the repository at this point in the history
BREAKING CHANGE: `txObject` parameter of `onSign` callback is removed
Use `unpackTx(tx)` on wallet side instead.
  • Loading branch information
davidyuk committed Jun 7, 2022
1 parent 290436a commit 930e7d7
Show file tree
Hide file tree
Showing 7 changed files with 254 additions and 187 deletions.
6 changes: 3 additions & 3 deletions src/account/multiple.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import AccountResolver, { _AccountResolver, Account } from './resolver'
import { UnavailableAccountError } from '../utils/errors'
import type stampit from '@stamp/it' // eslint-disable-line @typescript-eslint/no-unused-vars

class _AccountMultiple extends _AccountResolver {
export class _AccountMultiple extends _AccountResolver {
accounts: { [key: EncodedData<'ak'>]: _AccountBase }
selectedAddress?: EncodedData<'ak'>

Expand Down Expand Up @@ -60,8 +60,8 @@ class _AccountMultiple extends _AccountResolver {
* @return {String[]}
* @example addresses()
*/
addresses (): string[] {
return Object.keys(this.accounts)
addresses (): Array<EncodedData<'ak'>> {
return Object.keys(this.accounts) as Array<EncodedData<'ak'>>
}

/**
Expand Down
4 changes: 2 additions & 2 deletions src/node-pool/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ interface NodePool {
readonly api: Node['api']
}

class _NodePool implements NodePool {
export class _NodePool implements NodePool {
readonly api: Node['api']
pool: Map<string, Node>
selectedNode?: Node
Expand Down Expand Up @@ -159,7 +159,7 @@ export default stampit<NodePool>({
selectNode: _NodePool.prototype.selectNode,
getNodeInfo: _NodePool.prototype.getNodeInfo,
isNodeConnected: _NodePool.prototype.isNodeConnected,
getNetworkId: _NodePool.prototype.getNetworkId,
getNetworkId,
getNodesInPool: _NodePool.prototype.getNodesInPool
}
})
11 changes: 7 additions & 4 deletions src/utils/aepp-wallet-communication/rpc/RpcClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,11 @@ interface JsonRpcResponse {
}
}

type RpcApiHandler = (p?: any, origin?: string) => any | undefined
type RpcApi<Keys> = { [k in keyof Keys]: RpcApiHandler }
type RpcApiHandler = (p?: any) => any | undefined
type RpcApi<Api> = { [k in keyof Api]: RpcApiHandler }
type WithOrigin<Api extends RpcApi<Api>> = {
[k in keyof Api]: (p: Parameters<Api[k]>[0], origin: string) => ReturnType<Api[k]>
}

/**
* Contain functionality for using RPC conection
Expand All @@ -36,12 +39,12 @@ export default class RpcClient <
connection: BrowserConnection
#callbacks = new Map<number, { resolve: (v: any) => void, reject: (e: Error) => void }>()
#messageId = 0
#methods: LocalApi
#methods: WithOrigin<LocalApi>

constructor (
connection: BrowserConnection,
onDisconnect: () => void,
methods: LocalApi
methods: WithOrigin<LocalApi>
) {
this.connection = connection
this.#methods = methods
Expand Down
8 changes: 4 additions & 4 deletions src/utils/aepp-wallet-communication/rpc/aepp-rpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import AccountRpc from '../../../account/rpc'
import { decode, EncodedData } from '../../encoder'
import { Accounts, WalletInfo, Network, WalletApi, AeppApi } from './types'
import RpcClient from './RpcClient'
import { METHODS, VERSION } from '../schema'
import { METHODS, VERSION, SUBSCRIPTION_TYPES } from '../schema'
import {
AlreadyConnectedError,
NoWalletConnectedError,
Expand Down Expand Up @@ -155,12 +155,12 @@ abstract class _AeppRpc extends _AccountResolver {

/**
* Subscribe for addresses from wallet
* @param type Should be one of 'current' (the selected account), 'connected' (all)
* @param value Subscription action
* @param type Subscription type
* @param value Should be one of 'current' (the selected account), 'connected' (all)
* @return Accounts from wallet
*/
async subscribeAddress (
type: 'current' | 'connected', value: 'subscribe' | 'unsubscribe'
type: SUBSCRIPTION_TYPES, value: 'current' | 'connected'
): Promise<ReturnType<WalletApi[METHODS.subscribeAddress]>> {
this._ensureConnected()
const result = await this.rpcClient.request(METHODS.subscribeAddress, { type, value })
Expand Down
18 changes: 8 additions & 10 deletions src/utils/aepp-wallet-communication/rpc/types.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { EncodedData } from '../../encoder'
import { METHODS, WALLET_TYPE } from '../schema'
import { METHODS, SUBSCRIPTION_TYPES, WALLET_TYPE } from '../schema'

export interface WalletInfo {
id: string
Expand Down Expand Up @@ -29,25 +29,23 @@ type Icons = Array<{ src: string, sizes?: string, type?: string, purpose?: strin
export interface WalletApi {
[METHODS.connect]: (
p: { name: string, icons?: Icons, version: 1, connectNode: boolean }
) => WalletInfo & { node?: Node }
) => Promise<WalletInfo & { node?: Node }>

[METHODS.closeConnection]: (p: any) => void

[METHODS.subscribeAddress]: (
p: { type: 'connected' | 'current', value: 'subscribe' | 'unsubscribe' }
) => { subscription: Array<'subscribe' | 'unsubscribe'>, address: Accounts }
p: { type: SUBSCRIPTION_TYPES, value: 'connected' | 'current' }
) => Promise<{ subscription: Array<'connected' | 'current'>, address: Accounts }>

[METHODS.address]: () => Array<EncodedData<'ak'>>
[METHODS.address]: () => Promise<Array<EncodedData<'ak'>>>

[METHODS.sign]: ((
p: { tx: EncodedData<'tx'>, onAccount: EncodedData<'ak'>, returnSigned: false }
) => { transactionHash: EncodedData<'th'> }) & ((
p: { tx: EncodedData<'tx'>, onAccount: EncodedData<'ak'>, returnSigned: true }
) => { signedTransaction: EncodedData<'tx'> })
p: { tx: EncodedData<'tx'>, onAccount: EncodedData<'ak'>, returnSigned: boolean }
) => Promise<{ transactionHash?: EncodedData<'th'>, signedTransaction?: EncodedData<'tx'> }>)

[METHODS.signMessage]: (
p: { message: string, onAccount: EncodedData<'ak'> }
) => { signature: string }
) => Promise<{ signature: string }>
}

export interface AeppApi {
Expand Down
Loading

0 comments on commit 930e7d7

Please sign in to comment.