Skip to content

Commit

Permalink
Migrated to did-sessions 1.x.x and started using getAccountId to auto…
Browse files Browse the repository at this point in the history
…matically reconnect users to their existing did
  • Loading branch information
baptistegreve committed Sep 30, 2022
1 parent e19c746 commit e1c0344
Show file tree
Hide file tree
Showing 5 changed files with 354 additions and 415 deletions.
47 changes: 26 additions & 21 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
import { CeramicClient } from '@ceramicnetwork/http-client';
import { TileDocument } from '@ceramicnetwork/stream-tile';
import { DIDSession } from 'did-session'
import { EthereumAuthProvider, SolanaAuthProvider } from '@ceramicnetwork/blockchain-utils-linking'
import { SolanaAuthProvider } from '@ceramicnetwork/blockchain-utils-linking'
import { EthereumWebAuth, getAccountId } from '@didtools/pkh-ethereum'

/** To generate dids from a Seed */
import { DID } from 'dids'
Expand All @@ -15,7 +16,7 @@ import { connectLitClient, generateLitSignature, generateLitSignatureV2, generat

/** Internal helpers */
import { indexer } from './lib/indexer-db.js';
import { forceIndex, forceIndexDid, sleep, randomSeed } from "./utils/index.js";
import { forceIndex, forceIndexDid, sleep, randomSeed, sortByKey } from "./utils/index.js";

/** Initiate the node URLs for the two networks */
const MAINNET_NODE_URL = "https://node1.orbis.club/";
Expand Down Expand Up @@ -117,25 +118,29 @@ export class Orbis {
}

/** Step 2: Check if user already has an active account on Orbis */
let authProvider;
let authMethod;
let defaultChain = "1";
let address = addresses[0].toLowerCase();
/*let {data: existingDids, error: errorDids} = await getDids(address);
if(errorDids) {
console.log("Error retrieving existing dids: ", errorDids);
}
let accountId = await getAccountId(provider, address)

/** Check if the user trying to connect already has an existing did on Orbis */
let {data: existingDids, error: errorDids} = await this.getDids(address);
if(existingDids && existingDids.length > 0) {
let _didArr = existingDids[0].did.split(":");
let default_network = _didArr[2];
if(default_network == "eip155") {
let default_chain = _didArr[3];
console.log("Default chain to use: ", default_chain);
let sortedDids = sortByKey(existingDids, "count_followers");
let _didArr = sortedDids[0].did.split(":");
let defaultNetwork = _didArr[2];
if(defaultNetwork == "eip155") {
defaultChain = _didArr[3];
}
} else {
}*/
}

/** Step 2: Create an authProvider object using the address connected */
/** Update the default accountId used to connect */
console.log("Default chain to use: ", defaultChain);
accountId.chainId.reference = defaultChain.toString();

/** Step 2: Create an authMethod object using the address connected */
try {
authProvider = new EthereumAuthProvider(provider, address)
authMethod = await EthereumWebAuth.getAuthMethod(provider, accountId)
} catch(e) {
return {
status: 300,
Expand All @@ -147,14 +152,14 @@ export class Orbis {
/** Step 3: Create a new session for this did */
let did;
try {
/** Expire session in 30 days by default */
const oneMonth = 60 * 60 * 24 * 31;
/** Expire session in 90 days by default */
const threeMonths = 60 * 60 * 24 * 90;

this.session = await DIDSession.authorize(
authProvider,
authMethod,
{
resources: [`ceramic://*`],
expiresInSecs: oneMonth
expiresInSecs: threeMonths
}
);
did = this.session.did;
Expand Down Expand Up @@ -1088,7 +1093,7 @@ export class Orbis {

/** Get user reaction for a post */
async getReaction(post_id, did) {
let { data, error, status } = await this.api.from("orbis_reactions").select('type').eq('post_id', post_id).eq('creator', did);
let { data, error, status } = await this.api.from("orbis_reactions").select('type').eq('post_id', post_id).eq('creator', did).single();

/** Return results */
return({ data, error, status });
Expand Down
Loading

0 comments on commit e1c0344

Please sign in to comment.