Skip to content

Commit

Permalink
now sending multiAddress of Alice for an onboarding Bob, which connec…
Browse files Browse the repository at this point in the history
…ts to relay and Alice at the same time and receives her contact data
  • Loading branch information
silkroadnomad committed Mar 7, 2024
1 parent c150502 commit d9bf04b
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 39 deletions.
26 changes: 19 additions & 7 deletions src/lib/components/OnBoarding.svelte
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
<script>
import { did } from "../../routes/router.js";
import { did, query } from "../../routes/router.js";
import { multiaddr } from '@multiformats/multiaddr'
import {
orbitdb,
libp2p,
dbMyAddressBook,
myAddressBook,
progressText,
Expand All @@ -10,21 +12,31 @@
} from "../../stores.js";
import { requestAddress } from "../../lib/network/p2p-operations.js"
const aliceMultiAddress = JSON.parse(decodeURI($query).split("=")[1])
console.log("aliceMultiAddress",aliceMultiAddress[0])
$:{
if($progressState>3 && //if connected
if($libp2p && $connectedPeers>0){
$libp2p.dial(multiaddr(aliceMultiAddress[0])).then((info) => {
console.log("connected peer",info)}).catch( (e) => {
console.log("e",e)
})
}
}
$:{
if($connectedPeers>1 && //if connected
$did!==undefined && $orbitdb!==undefined && $dbMyAddressBook.access!==undefined)
requestAddress($did)
}
let scannedContact
$:{
if($myAddressBook.length>0){
scannedContact = $myAddressBook?.filter((it) => {
console.log("it.value?.owner", it.owner)
return it.owner === $did
})
if($myAddressBook.length>0){ //if a record arrives in my address book show it on the page //TODO go to ContactList (would be better)
scannedContact = $myAddressBook?.filter((it) => {return it.owner === $did})
console.log("scannedContact",scannedContact[0])
}
}
</script>
<div class="content">
<h1>Thank you for scanning my address {$did}</h1>
Expand Down
16 changes: 14 additions & 2 deletions src/lib/components/QRCodeModal.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,29 @@
import { createEventDispatcher } from "svelte";
const dispatch = createEventDispatcher();
import { clickToCopy } from "../../utils/utils.js"
import {connectedPeers, libp2p} from "../../stores.js";
const page_url = import.meta.env.VITE_PAGE_URL?import.meta.env.VITE_PAGE_URL+'/#':'https://deContact.xyz/#';
export let qrCodeData
export let qrCodeOpen
let multiAddrString
$:{
if($connectedPeers>0){
const multiaddrs = $libp2p.getMultiaddrs().map((ma) => ma.toString())
multiAddrString = encodeURI(JSON.stringify(multiaddrs))
}
}
let text = ''; //TODO when clicking use timeout to reset text to '' (maybe an animation)
let linkUrl = `${page_url}/onboarding/${qrCodeData || '' }`
let fullDeContactUrl
$: linkUrl = !fullDeContactUrl?`${page_url}/onboarding/${qrCodeData || '' }`:qrCodeData
$: linkUrl = !fullDeContactUrl?`${page_url}/onboarding/${qrCodeData+'?multiaddr='+multiAddrString || '' }`:qrCodeData
</script>
<svelte:window on:copysuccess={ () => text = "Copied!"} on:copyerror={ (e) => text = `Error! ${e.detail}`}/>
<svelte:window on:copysuccess={ () => {
text = "Copied!"
dispatch('close')
}} on:copyerror={ (e) => text = `Error! ${e.detail}`}/>
<Modal bind:open={ qrCodeOpen }
modalHeading="Scan (DID) - Decentralized Identity"
primaryButtonText="OK"
Expand Down
54 changes: 27 additions & 27 deletions src/lib/network/p2p-operations.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,32 +50,6 @@ async function getAddressRecords() {
}
}

/**
* Loop through our address book and filter all addresses where others are the owners
* @param ourDID our DID
* @returns {Promise<void>}
*/
async function initReplicationOfSubscriberDBs(ourDID) {
console.log("replicateSubsriberDBs")
const addressRecords = await _dbMyAddressBook.all();
_subscriberList = addressRecords.filter((addr)=> {
return addr.value.owner !== ourDID && addr.value.sharedAddress!==undefined //sharedAddress undefined for all not decentralized addresses
})
console.log("_subscriberList",_subscriberList)
subscriberList.set(_subscriberList)
for (const s in _subscriberList) {
const dbAddress = _subscriberList[s].value.sharedAddress
console.log("loading subscribers db s",dbAddress)
try{
_subscriberList[s].db = await _orbitdb.open(dbAddress, {type: 'documents',sync: true})
_subscriberList[s].db.all().then((records)=> { //replicate the addresses of Bob, Peter etc.
console.log(`dbAddress: ${dbAddress} records`,records)
})
}catch(e){console.log(`error while loading ${dbAddress} `)}

}
}

/**
* Starting libp2p, Helia, OrbitDB with an Identity (e.g. DID generated by the seed phrase)
* Starting replication of myAddressbook with my other devices and launching storage protocol to receive message requests from others.
Expand Down Expand Up @@ -139,7 +113,7 @@ export async function startNetwork() {
sync: true,
AccessController: OrbitDBAccessController({ write: [_orbitdb.identity.id]})
})
console.log("dbMyAddressBook",_dbMyAddressBook)
// console.log("dbMyAddressBook",_dbMyAddressBook)
dbMyAddressBook.set(_dbMyAddressBook)
window.dbMyAddressBook = _dbMyAddressBook
await getAddressRecords()
Expand All @@ -155,6 +129,32 @@ export async function startNetwork() {
getAddressRecords()
})
}

/**
* Loop through our address book and filter all other's addresses
* @param ourDID our DID
* @returns {Promise<void>}
*/
async function initReplicationOfSubscriberDBs(ourDID) {
// console.log("replicateSubsriberDBs")
const addressRecords = await _dbMyAddressBook.all();
_subscriberList = addressRecords.filter((addr)=> {
return addr.value.owner !== ourDID && addr.value.sharedAddress!==undefined //sharedAddress undefined for all not decentralized addresses
})
// console.log("_subscriberList",_subscriberList)
subscriberList.set(_subscriberList)
for (const s in _subscriberList) {
const dbAddress = _subscriberList[s].value.sharedAddress
// console.log("loading subscribers db s",dbAddress)
try{
_subscriberList[s].db = await _orbitdb.open(dbAddress, {type: 'documents',sync: true})
_subscriberList[s].db.all().then((records)=> { //replicate the addresses of Bob, Peter etc.
console.log(`dbAddress: ${dbAddress} records`,records)
})
}catch(e){console.log(`error while loading ${dbAddress} `)}

}
}
async function handleMessage (dContactMessage) {
console.log("dContactMessage",dContactMessage )
if (!dContactMessage) return;
Expand Down
2 changes: 1 addition & 1 deletion src/routes/+layout.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
'': Home,
'/onboarding': OnBoarding
}
// $: console.log("$hash",$hash)
$: view = routes[$hash]
// const urlParams = new URLSearchParams(window.location.search); //TODO url params we need when we "onboard" a new user via scanning a URL
Expand Down
2 changes: 2 additions & 0 deletions src/routes/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
import Statistics from "$lib/components/Statistics.svelte";
import {
libp2p,
connectedPeers,
orbitdb,
qrCodeOpen,
qrCodeData,
Expand Down
8 changes: 6 additions & 2 deletions src/routes/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,24 @@ import { browser } from '$app/environment';
function getHash () {
if(browser){
const hashWithDID = location.hash.replace(/^#/, '')
return hashWithDID.lastIndexOf('/')!==-1?hashWithDID.substring(0,hashWithDID.lastIndexOf('/')):hashWithDID
// console.log("hash",hashWithDID.indexOf('/',1)!==-1?hashWithDID.substring(0,hashWithDID.indexOf('/',1)):hashWithDID)
return hashWithDID.indexOf('/',1)!==-1?hashWithDID.substring(0,hashWithDID.indexOf('/',1)):hashWithDID
}
}

function getDID () {
if(browser){
const hashWithDID = location.hash.replace(/^#/, '')
return hashWithDID.lastIndexOf('/')!==-1?hashWithDID.substring(hashWithDID.lastIndexOf('/')+1):hashWithDID
if(!hashWithDID) return
// console.log("did",hashWithDID.split("/")[2].substring(0,hashWithDID.split("/")[2].indexOf("?")));
return hashWithDID.split("/")[2].substring(0,hashWithDID.split("/")[2].indexOf("?"))
}
}

function getQuery(){
if(browser) {
const hashWithQueryParam = location.hash.replace(/^#/, '')
// console.log("query", hashWithQueryParam.indexOf('?') !== -1 ? hashWithQueryParam.substring(hashWithQueryParam.indexOf('?') + 1, hashWithQueryParam.length) : undefined)
return hashWithQueryParam.indexOf('?') !== -1 ? hashWithQueryParam.substring(hashWithQueryParam.indexOf('?') + 1, hashWithQueryParam.length) : undefined
}
}
Expand Down

0 comments on commit d9bf04b

Please sign in to comment.