Skip to content

Commit

Permalink
Merge pull request #19 from marcuspuchalla/feature/#17-add-autoconnect
Browse files Browse the repository at this point in the history
Feature/#17 add autoconnect
  • Loading branch information
fabianbormann authored Mar 16, 2023
2 parents a1afdd5 + 592865b commit f55df10
Show file tree
Hide file tree
Showing 3 changed files with 8,078 additions and 26 deletions.
91 changes: 83 additions & 8 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,12 @@ export class DAppPeerConnect {
(address: string, walletInfo: IWalletInfo, callback: (args: IConnectMessage) => void) => {

if (!this.connectedWallet) {
const connectWallet = (granted: boolean) => {
const connectWallet = (granted: boolean, allowAutoConnect: boolean = false) => {

if(walletInfo.requestAutoconnect && granted && allowAutoConnect) {

AutoConnectHelper.addAutoConnectId(address)
}

if (granted) {

Expand All @@ -119,7 +124,8 @@ export class DAppPeerConnect {
dApp: this.dAppInfo,
address: address,
connected: true,
error: false
error: false,
autoConnect: allowAutoConnect
});

this.generateIdenticon()
Expand All @@ -135,20 +141,32 @@ export class DAppPeerConnect {
address: address,
connected: false,
error: true,
errorMessage: `User denied connection to ${address}`
errorMessage: `User denied connection to ${address}`,
autoConnect: allowAutoConnect
})

this.logger.info(`User denied connection to ${address}`);
}
};

if (typeof verifyConnection !== 'undefined') {
verifyConnection({
...walletInfo,
address: address
}, connectWallet);

if(AutoConnectHelper.isAutoConnectId(address)) {

connectWallet(true);

} else {

verifyConnection({
...walletInfo,
address: address
}, connectWallet);
}

} else {

connectWallet(true);

}
} else if (this.connectedWallet === address) {

Expand Down Expand Up @@ -642,7 +660,7 @@ export abstract class CardanoPeerConnect {
}


class PeerConnectIdenticon {
export class PeerConnectIdenticon {

public static getBase64Identicon = (hash: string): string | null => {

Expand All @@ -663,3 +681,60 @@ class PeerConnectIdenticon {
}).toDataURL()
}
}

export class AutoConnectHelper {

private static storageKey = 'cardano-peer-autoconnect-id'

public static addAutoConnectId = (id: string) :void => {

let autoConnectIds = []

const ids = localStorage.getItem(this.storageKey)

if(ids !== null) {
autoConnectIds = JSON.parse(ids)
}

if(this.isAutoConnectId(id)) {
return
}

autoConnectIds.push(id)

localStorage.setItem(this.storageKey, JSON.stringify(autoConnectIds));
}

public static getAutoConnectIds = (): string[] => {

return JSON.parse(localStorage.getItem(this.storageKey) ?? '[]')
}

public static isAutoConnectId = (id: string): boolean => {

return this.getAutoConnectIds().includes(id)
}

public static resetAutoConnectIds = ():void => {

localStorage.setItem(this.storageKey, JSON.stringify([]));
}

public static removeAutoConnectId = (id: string): void => {
let autoConnectIds = []
const ids = localStorage.getItem(this.storageKey)

if(ids !== null) {
autoConnectIds = JSON.parse(ids)
}

const index = autoConnectIds.indexOf(id)

if(index !== -1) {
autoConnectIds = autoConnectIds.splice(index, 1)

localStorage.setItem(this.storageKey, JSON.stringify(autoConnectIds));
return
}
}
}
Loading

0 comments on commit f55df10

Please sign in to comment.