-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'develop' of github.com:cardano-foundation/cf-identity-w…
…allet into feature/app-offline-mode
- Loading branch information
Showing
9 changed files
with
1,674 additions
and
71 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
102 changes: 102 additions & 0 deletions
102
src/core/cardano/walletConnect/identityWalletConnect.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
import { Buffer } from "buffer"; | ||
import { | ||
Paginate, | ||
Cip30DataSignature, | ||
IWalletInfo, | ||
} from "@fabianbormann/cardano-peer-connect/dist/src/types"; | ||
import { CardanoPeerConnect } from "@fabianbormann/cardano-peer-connect"; | ||
import { Signer } from "signify-ts"; | ||
import { Agent } from "../../agent/agent"; | ||
|
||
class IdentityWalletConnect extends CardanoPeerConnect { | ||
static readonly IDENTIFIER_ID_NOT_LOCATED = | ||
"The id doesn't correspond with any stored identifier"; | ||
static readonly NO_IDENTIFIERS_STORED = "No stored identifiers"; | ||
|
||
getIdentifierOobi: () => Promise<string>; | ||
sign: (identifier: string, payload: string) => Promise<string>; | ||
|
||
signerCache: Map<string, Signer>; | ||
|
||
constructor( | ||
walletInfo: IWalletInfo, | ||
seed: string | null, | ||
announce: string[], | ||
discoverySeed?: string | null | ||
) { | ||
super(walletInfo, { | ||
seed: seed, | ||
announce: announce, | ||
discoverySeed: discoverySeed, | ||
logLevel: "info", | ||
}); | ||
|
||
this.signerCache = new Map(); | ||
|
||
this.getIdentifierOobi = async (): Promise<string> => { | ||
const identifiers = await Agent.agent.identifiers.getIdentifiers(); | ||
if (!(identifiers && identifiers.length > 0)) { | ||
throw new Error(IdentityWalletConnect.NO_IDENTIFIERS_STORED); | ||
} | ||
|
||
return Agent.agent.connections.getOobi(identifiers[0].signifyName); | ||
}; | ||
|
||
this.sign = async ( | ||
identifier: string, | ||
payload: string | ||
): Promise<string> => { | ||
if (this.signerCache.get(identifier) === undefined) { | ||
this.signerCache.set( | ||
identifier, | ||
await Agent.agent.identifiers.getSigner(identifier) | ||
); | ||
} | ||
return this.signerCache.get(identifier)!.sign(Buffer.from(payload)).qb64; | ||
}; | ||
} | ||
|
||
protected getNetworkId(): Promise<number> { | ||
throw new Error("Method not implemented."); | ||
} | ||
protected getUtxos( | ||
amount?: string | undefined, | ||
paginate?: Paginate | undefined | ||
): Promise<string[] | null> { | ||
throw new Error("Method not implemented."); | ||
} | ||
protected getCollateral( | ||
params?: { amount?: string | undefined } | undefined | ||
): Promise<string[] | null> { | ||
throw new Error("Method not implemented."); | ||
} | ||
protected getBalance(): Promise<string> { | ||
throw new Error("Method not implemented."); | ||
} | ||
protected getUsedAddresses(): Promise<string[]> { | ||
throw new Error("Method not implemented."); | ||
} | ||
protected getUnusedAddresses(): Promise<string[]> { | ||
throw new Error("Method not implemented."); | ||
} | ||
protected getChangeAddress(): Promise<string> { | ||
throw new Error("Method not implemented."); | ||
} | ||
protected async getRewardAddresses(): Promise<string[]> { | ||
throw new Error("Method not implemented."); | ||
} | ||
protected signTx(tx: string, partialSign: boolean): Promise<string> { | ||
throw new Error("Method not implemented."); | ||
} | ||
protected async signData( | ||
addr: string, | ||
payload: string | ||
): Promise<Cip30DataSignature> { | ||
throw new Error("Method not implemented."); | ||
} | ||
protected submitTx(tx: string): Promise<string> { | ||
throw new Error("Method not implemented."); | ||
} | ||
} | ||
|
||
export { IdentityWalletConnect }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
import { IConnectMessage } from "@fabianbormann/cardano-peer-connect/dist/src/types"; | ||
import { ExperimentalContainer } from "@fabianbormann/cardano-peer-connect"; | ||
import { SecureStorage } from "@aparajita/capacitor-secure-storage"; | ||
import { IdentityWalletConnect } from "./identityWalletConnect"; | ||
import { ExperimentalAPIFunctions } from "./peerConnection.types"; | ||
import packageInfo from "../../../../package.json"; | ||
import ICON_BASE64 from "../../../assets/icon-only"; | ||
import { KeyStoreKeys } from "../../storage"; | ||
|
||
class PeerConnection { | ||
static readonly PEER_CONNECTION_START_PENDING = | ||
"The PeerConnection.start() has not been called yet"; | ||
|
||
private walletInfo = { | ||
address: "", | ||
name: "idw_p2p", | ||
icon: ICON_BASE64, | ||
version: packageInfo.version, | ||
requestAutoconnect: true, | ||
}; | ||
|
||
private announce = [ | ||
"wss://tracker.openwebtorrent.com", | ||
"wss://dev.tracker.cf-identity-wallet.metadata.dev.cf-deployments.org", | ||
"wss://tracker.files.fm:7073/announce", | ||
"ws://tracker.files.fm:7072/announce", | ||
"wss://tracker.openwebtorrent.com:443/announce", | ||
]; | ||
|
||
private identityWalletConnect: IdentityWalletConnect | undefined; | ||
private connected = false; | ||
|
||
async start() { | ||
let meerkatSeed = null; | ||
|
||
try { | ||
meerkatSeed = (await SecureStorage.get( | ||
KeyStoreKeys.MEERKAT_SEED | ||
)) as string; | ||
} catch { | ||
meerkatSeed = null; | ||
} | ||
|
||
this.identityWalletConnect = new IdentityWalletConnect( | ||
this.walletInfo, | ||
meerkatSeed, | ||
this.announce | ||
); | ||
this.identityWalletConnect.setOnConnect( | ||
(connectMessage: IConnectMessage) => { | ||
this.connected = true; | ||
} | ||
); | ||
|
||
this.identityWalletConnect.setOnDisconnect( | ||
(connectMessage: IConnectMessage) => { | ||
this.connected = false; | ||
} | ||
); | ||
|
||
this.identityWalletConnect.setEnableExperimentalApi( | ||
new ExperimentalContainer<ExperimentalAPIFunctions>({ | ||
getIdentifierOobi: this.identityWalletConnect.getIdentifierOobi, | ||
sign: this.identityWalletConnect.sign, | ||
}) | ||
); | ||
} | ||
|
||
connectWithDApp(dAppIdentifier: string) { | ||
if (this.identityWalletConnect === undefined) { | ||
throw new Error(PeerConnection.PEER_CONNECTION_START_PENDING); | ||
} | ||
|
||
const seed = this.identityWalletConnect.connect(dAppIdentifier); | ||
SecureStorage.set(KeyStoreKeys.MEERKAT_SEED, seed); | ||
this.connected = true; | ||
} | ||
|
||
disconnectDApp(dAppIdentifier: string) { | ||
if (this.identityWalletConnect === undefined) { | ||
throw new Error(PeerConnection.PEER_CONNECTION_START_PENDING); | ||
} | ||
|
||
this.identityWalletConnect.disconnect(dAppIdentifier); | ||
this.connected = false; | ||
} | ||
|
||
isConnected() { | ||
return this.connected; | ||
} | ||
} | ||
|
||
export { PeerConnection }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
interface ExperimentalAPIFunctions { | ||
getIdentifierOobi: () => Promise<string>; | ||
sign: (identifier: string, payload: string) => Promise<string>; | ||
} | ||
|
||
export type { ExperimentalAPIFunctions }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters