refactor: Use hash instead of path when referencing orbitdb addresses.

This commit is contained in:
Hayden Young 2023-06-27 14:06:31 +01:00
parent cdbe76d708
commit 44752a464e
3 changed files with 10 additions and 10 deletions

View File

@ -58,21 +58,21 @@ const parseAddress = (address) => {
} }
const OrbitDBAddress = (address) => { const OrbitDBAddress = (address) => {
if (address && address.protocol === 'orbitdb' && address.path) { if (address && address.protocol === 'orbitdb' && address.hash) {
return address return address
} }
const protocol = 'orbitdb' const protocol = 'orbitdb'
const path = address.replace('/orbitdb/', '').replace('\\orbitdb\\', '') const hash = address.replace('/orbitdb/', '').replace('\\orbitdb\\', '')
const toString = () => { const toString = () => {
return posixJoin('/', protocol, path) return posixJoin('/', protocol, hash)
} }
return { return {
protocol, protocol,
path, hash,
address, address,
toString toString
} }

View File

@ -115,7 +115,7 @@ const OrbitDB = async ({ ipfs, id, identities, directory } = {}) => {
if (isValidAddress(address)) { if (isValidAddress(address)) {
// If the address given was a valid OrbitDB address, eg. '/orbitdb/zdpuAuK3BHpS7NvMBivynypqciYCuy2UW77XYBPUYRnLjnw13' // If the address given was a valid OrbitDB address, eg. '/orbitdb/zdpuAuK3BHpS7NvMBivynypqciYCuy2UW77XYBPUYRnLjnw13'
const addr = OrbitDBAddress(address) const addr = OrbitDBAddress(address)
manifest = await manifestStore.get(addr.path) manifest = await manifestStore.get(addr.hash)
const acType = manifest.accessController.split('/', 2).pop() const acType = manifest.accessController.split('/', 2).pop()
const acAddress = manifest.accessController.replaceAll(`/${acType}/`, '') const acAddress = manifest.accessController.replaceAll(`/${acType}/`, '')
AccessController = getAccessController(acType)() AccessController = getAccessController(acType)()

View File

@ -18,7 +18,7 @@ describe('Address', function () {
it('has a path', () => { it('has a path', () => {
const address = '/orbitdb/zdpuAuK3BHpS7NvMBivynypqciYCuy2UW77XYBPUYRnLjnw13' const address = '/orbitdb/zdpuAuK3BHpS7NvMBivynypqciYCuy2UW77XYBPUYRnLjnw13'
const addr = OrbitDBAddress(address) const addr = OrbitDBAddress(address)
strictEqual(addr.path, 'zdpuAuK3BHpS7NvMBivynypqciYCuy2UW77XYBPUYRnLjnw13') strictEqual(addr.hash, 'zdpuAuK3BHpS7NvMBivynypqciYCuy2UW77XYBPUYRnLjnw13')
}) })
}) })
@ -38,7 +38,7 @@ describe('Address', function () {
it('has a path', () => { it('has a path', () => {
const address = 'zdpuAuK3BHpS7NvMBivynypqciYCuy2UW77XYBPUYRnLjnw13' const address = 'zdpuAuK3BHpS7NvMBivynypqciYCuy2UW77XYBPUYRnLjnw13'
const addr = OrbitDBAddress(address) const addr = OrbitDBAddress(address)
strictEqual(addr.path, 'zdpuAuK3BHpS7NvMBivynypqciYCuy2UW77XYBPUYRnLjnw13') strictEqual(addr.hash, 'zdpuAuK3BHpS7NvMBivynypqciYCuy2UW77XYBPUYRnLjnw13')
}) })
}) })
@ -61,7 +61,7 @@ describe('Address', function () {
}) })
it('has a path', () => { it('has a path', () => {
strictEqual(addr2.path, 'zdpuAuK3BHpS7NvMBivynypqciYCuy2UW77XYBPUYRnLjnw13') strictEqual(addr2.hash, 'zdpuAuK3BHpS7NvMBivynypqciYCuy2UW77XYBPUYRnLjnw13')
}) })
}) })
@ -128,7 +128,7 @@ describe('Address', function () {
const result = parseAddress(address) const result = parseAddress(address)
strictEqual(result.protocol, 'orbitdb') strictEqual(result.protocol, 'orbitdb')
strictEqual(result.path, 'zdpuAuK3BHpS7NvMBivynypqciYCuy2UW77XYBPUYRnLjnw13') strictEqual(result.hash, 'zdpuAuK3BHpS7NvMBivynypqciYCuy2UW77XYBPUYRnLjnw13')
strictEqual(result.toString().indexOf('/orbitdb'), 0) strictEqual(result.toString().indexOf('/orbitdb'), 0)
strictEqual(result.toString().indexOf('zdpuAuK3BHpS7NvMBivynypqciYCuy2UW77XYBPUYRnLjnw13'), 9) strictEqual(result.toString().indexOf('zdpuAuK3BHpS7NvMBivynypqciYCuy2UW77XYBPUYRnLjnw13'), 9)
@ -139,7 +139,7 @@ describe('Address', function () {
const result = parseAddress(address) const result = parseAddress(address)
strictEqual(result.protocol, 'orbitdb') strictEqual(result.protocol, 'orbitdb')
strictEqual(result.path, 'Qmdgwt7w4uBsw8LXduzCd18zfGXeTmBsiR8edQ1hSfzcJC') strictEqual(result.hash, 'Qmdgwt7w4uBsw8LXduzCd18zfGXeTmBsiR8edQ1hSfzcJC')
strictEqual(result.toString().indexOf('/orbitdb'), 0) strictEqual(result.toString().indexOf('/orbitdb'), 0)
strictEqual(result.toString().indexOf('Qmdgwt7w4uBsw8LXduzCd18zfGXeTmBsiR8edQ1hSfzcJC'), 9) strictEqual(result.toString().indexOf('Qmdgwt7w4uBsw8LXduzCd18zfGXeTmBsiR8edQ1hSfzcJC'), 9)