From c3a9a81bfdfebca1af4b45d91666179cf2d5b8a9 Mon Sep 17 00:00:00 2001 From: Barton Rhodes Date: Mon, 10 Oct 2022 22:58:40 -0400 Subject: [PATCH 1/2] init cosmos --- index.js | 12 +++++++++++- utils/EthereumAuthProviderDefault.ts | 2 +- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index 6c0b699..8573d8a 100644 --- a/index.js +++ b/index.js @@ -2,7 +2,7 @@ import { CeramicClient } from '@ceramicnetwork/http-client'; import { TileDocument } from '@ceramicnetwork/stream-tile'; import { DIDSession } from 'did-session' -import { SolanaAuthProvider } from '@ceramicnetwork/blockchain-utils-linking' +import { SolanaAuthProvider, CosmosAuthProvider } from '@ceramicnetwork/blockchain-utils-linking' import { EthereumWebAuth, getAccountId } from '@didtools/pkh-ethereum' /** To generate dids from a Seed */ @@ -357,6 +357,16 @@ export class Orbis { } } + + async testConnectCosmos() { + let provider; + if ('keplr' in window) { + provider = window.keplr?.cosmoshub; + } else { + console.log("You're out of space, I'm out of time"); + } + } + /** Test function to check the status of the Solana provider */ async testConnectSolana() { let provider; diff --git a/utils/EthereumAuthProviderDefault.ts b/utils/EthereumAuthProviderDefault.ts index 226f218..527dae1 100644 --- a/utils/EthereumAuthProviderDefault.ts +++ b/utils/EthereumAuthProviderDefault.ts @@ -1,4 +1,4 @@ -import { EthereumAuthProvider, SolanaAuthProvider } from '@ceramicnetwork/blockchain-utils-linking' +import { EthereumAuthProvider, SolanaAuthProvider, CosmosAuthProvider } from '@ceramicnetwork/blockchain-utils-linking' /** Replacing default EthereumAuthProvider class to force connection to an existing did if any. */ From 61b1ccbfcbbde1d8083b3c4ae11d89e5ca69ef0a Mon Sep 17 00:00:00 2001 From: Barton Rhodes Date: Mon, 10 Oct 2022 23:15:46 -0400 Subject: [PATCH 2/2] mmm, yummy copypasta --- index.js | 68 +++++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 67 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index 8573d8a..ee3a485 100644 --- a/index.js +++ b/index.js @@ -361,10 +361,76 @@ export class Orbis { async testConnectCosmos() { let provider; if ('keplr' in window) { - provider = window.keplr?.cosmoshub; + provider = window.keplr; } else { console.log("You're out of space, I'm out of time"); } + + const resp = await provider.connect(); + let address = resp.publicKey.toString(); + console.log("Connected to Keplr with address: " + address); + + /** Step 2: Create a Cosmic authProvider object using the address connected */ + let authProvider; + try { + authProvider = new CosmosAuthProvider(provider, address); + } catch(e) { + console.log("Error creating CosmosAuthProvider: " + e); + return { + status: 300, + error: e, + result: "Error creating Cosmos provider object for Ceramic." + } + } + + /** Step 3: Create a new session for Cosmic DID */ + let did; + try { + /** Expire session in 30 days by default */ + const oneMonth = 60 * 60 * 24 * 31; + + this.session = await DIDSession.authorize( + authProvider, + { + resources: [`ceramic://*`], + expiresInSecs: oneMonth + } + ); + did = this.session.did; + } catch(e) { + return { + status: 300, + error: e, + result: "Error creating a session for the DiD." + } + } + /** Step 4: Assign did to Ceramic object */ + this.ceramic.did = did; + console.log("Connected to Ceramic using: " + this.session.id); + + /** Step 6: Force index did to retrieve blockchain details automatically */ + let _resDid = await forceIndexDid(this.session.id); + + /** Step 7: Get user profile details */ + let { data, error, status } = await this.getProfile(this.session.id); + + let details; + if(data) { + details = data.details; + } else { + details = { + did: this.session.id, + profile: null + } + } + + /** Return result */ + return { + status: 200, + did: this.session.id, + details: details, + result: "Success connecting to the DiD." + } } /** Test function to check the status of the Solana provider */