Skip to content

Commit

Permalink
feat(AEX_2): Allow to connect without node (#991)
Browse files Browse the repository at this point in the history
* feat(AEX_2): Remove check for available network in Wallet `connect` handler

* feat(AEX_2): Fix RPC example apps.
  • Loading branch information
nduchak authored May 20, 2020
1 parent 4efd341 commit 87b9ef9
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 8 deletions.
5 changes: 3 additions & 2 deletions es/node.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,9 @@ async function remoteSwag (url, axiosConfig) {
* @rtype () => networkId: String
* @return {String} NetworkId
*/
export function getNetworkId ({ networkId } = {}) {
if (!networkId && !this.networkId && (!this.selectedNode || !this.selectedNode.networkId)) throw new Error('networkId is not provided')
export function getNetworkId ({ networkId, force = false } = {}) {
if (!force && !networkId && !this.networkId && (!this.selectedNode || !this.selectedNode.networkId)) throw new Error('networkId is not provided')
if (force && !networkId && !this.networkId && (!this.selectedNode || !this.selectedNode.networkId)) return null
return networkId || this.networkId || this.selectedNode.networkId
}

Expand Down
4 changes: 2 additions & 2 deletions es/utils/aepp-wallet-communication/rpc/aepp-rpc.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ export const AeppRpc = Ae.compose({
if (this.rpcClient && this.rpcClient.isConnected()) throw new Error('You are already connected to wallet ' + this.rpcClient)
this.rpcClient = RpcClient({
connection,
networkId: this.getNetworkId(),
networkId: this.getNetworkId({ force: true }),
...connection.connectionInfo,
id: uuid(),
handlers: [handleMessage(this), this.onDisconnect]
Expand Down Expand Up @@ -215,7 +215,7 @@ export const AeppRpc = Ae.compose({
METHODS.aepp.connect, {
name: this.name,
version: VERSION,
networkId: this.getNetworkId()
networkId: this.getNetworkId({ force: true })
}
)
},
Expand Down
6 changes: 3 additions & 3 deletions es/utils/aepp-wallet-communication/rpc/wallet-rpc.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ const REQUESTS = {
async [METHODS.aepp.connect] (callInstance, instance, client, { name, networkId, version, icons }) {
// Check if protocol and network is compatible with wallet
if (version !== VERSION) return { error: ERRORS.unsupportedProtocol() }
if (networkId !== instance.getNetworkId()) return { error: ERRORS.unsupportedNetwork() }
// if (networkId !== instance.getNetworkId()) return { error: ERRORS.unsupportedNetwork() }

// Store new AEPP and wait for connection approve
rpcClients.updateClientInfo(client.id, {
Expand Down Expand Up @@ -208,7 +208,7 @@ const handleMessage = (instance, id) => async (msg, origin) => {
* @return {Object}
*/
export const WalletRpc = Ae.compose(Accounts, Selector, {
init ({ name, onConnection, onSubscription, onSign, onDisconnect, onAskAccounts, onMessageSign }) {
init ({ name, onConnection, onSubscription, onSign, onDisconnect, onAskAccounts, onMessageSign, forceValidation = false }) {
const eventsHandlers = ['onConnection', 'onSubscription', 'onSign', 'onDisconnect', 'onMessageSign']
// CallBacks for events
this.onConnection = onConnection
Expand All @@ -219,7 +219,7 @@ export const WalletRpc = Ae.compose(Accounts, Selector, {
this.onMessageSign = onMessageSign

eventsHandlers.forEach(event => {
if (typeof this[event] !== 'function') throw new Error(`Call-back for ${event} must be an function!`)
if (!forceValidation && typeof this[event] !== 'function') throw new Error(`Call-back for ${event} must be an function!`)
})
//
this.name = name
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,14 @@
}[params.onAccount]
accept(null, { onAccount }) // provide this account for signing
},
onMessageSign: genConfirmCallback(() => 'sign message'),
onMessageSign (aepp, { accept, deny, params }, origin) {
// Get account outside of SDK if needed
const onAccount = {
[keypair.publicKey]: MemoryAccount({ keypair }),
[keypair2.publicKey]: MemoryAccount({ keypair: keypair2 }),
}[params.onAccount]
accept({ onAccount }) // provide this account for signing
},
onAskAccounts: genConfirmCallback(() => 'get accounts'),
onDisconnect (message, client) {
this.shareWalletInfo(connection.sendMessage.bind(connection))
Expand Down

0 comments on commit 87b9ef9

Please sign in to comment.