Skip to content

Commit

Permalink
Merge pull request #15 from marcuspuchalla/feature/identicon
Browse files Browse the repository at this point in the history
Feature/identicon
  • Loading branch information
fabianbormann authored Mar 13, 2023
2 parents f2b0d44 + 460d5c2 commit a1afdd5
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 7,973 deletions.
58 changes: 57 additions & 1 deletion index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import type {
} from './types';
import QRCode from 'qrcode-svg';
import Logger from '@fabianbormann/meerkat/dist/logger';

import { identicon } from '@basementuniverse/marble-identicons';
export class DAppPeerConnect {

private meerkat: Meerkat;
Expand All @@ -23,6 +23,8 @@ export class DAppPeerConnect {

private readonly dAppInfo: IDAppInfos

protected identicon: string | null = null

protected onConnect?: (address: string) => void;
protected onDisconnect?: (address: string) => void;
protected onApiEject?: (name: string, address: string) => void;
Expand Down Expand Up @@ -120,6 +122,8 @@ export class DAppPeerConnect {
error: false
});

this.generateIdenticon()

if (this.onConnect) {

this.onConnect(address);
Expand Down Expand Up @@ -405,6 +409,15 @@ export class DAppPeerConnect {
getSeed() {
return this.meerkat.seed;
}

public generateIdenticon = () => {

this.identicon = PeerConnectIdenticon.getBase64Identicon(this.connectedWallet + this.getAddress())
}

public getIdenticon = () => {
return this.identicon
}
}

export abstract class CardanoPeerConnect {
Expand All @@ -415,6 +428,7 @@ export abstract class CardanoPeerConnect {
protected onDisconnect: (connectMessage: IConnectMessage) => void
protected onServerShutdown: (connectMessage: IConnectMessage) => void
protected onApiInject: (connectMessage: IConnectMessage) => void
protected identicon: string | null = null

protected meerkat : Meerkat | null = null

Expand Down Expand Up @@ -568,6 +582,8 @@ export abstract class CardanoPeerConnect {
)
}

this.generateIdenticon()

this.onConnect(connectStatus)
});
});
Expand All @@ -576,6 +592,19 @@ export abstract class CardanoPeerConnect {
return this.meerkat.seed;
}

public generateIdenticon = () => {

if(!this.meerkat?.address()) {
throw new Error('Server meerkat address not defined.')
}

if(!this.meerkat?.identifier) {
throw new Error('Client meerkat address not defined.')
}

this.identicon = PeerConnectIdenticon.getBase64Identicon(this.meerkat?.address() + this.meerkat?.identifier)
}


public disconnect(address: string) {

Expand All @@ -595,6 +624,10 @@ export abstract class CardanoPeerConnect {
})
}

public getIdenticon = () => {
return this.identicon
}

protected abstract getNetworkId(): Promise<number>;
protected abstract getUtxos(amount?: Cbor, paginate?: Paginate): Promise<Cbor[] | null>;
protected abstract getCollateral(params?: { amount?: Cbor }): Promise<Cbor[] | null>;
Expand All @@ -607,3 +640,26 @@ export abstract class CardanoPeerConnect {
protected abstract signData(addr: string, payload: Bytes): Promise<Cip30DataSignature>;
protected abstract submitTx(tx: Cbor): Promise<string>;
}


class PeerConnectIdenticon {

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

if(hash.length < 68) {

console.warn('Meerkat connection hash is to short. Not generating identicon.')

return null
}

return identicon(
hash.split('').reverse().map((char: string, index: number) => (index > 0 && index % 10 === 0) ? '-': char).join(''),
{
size: 100,
baseSeed: 'cardano-peer-connect',
fontSize: 0.17,
initialsColours: ['#000000', '#FF0000', '#0000FF']
}).toDataURL()
}
}
Loading

0 comments on commit a1afdd5

Please sign in to comment.