Skip to content

Commit

Permalink
feat(namers/w3): local resolutions of names
Browse files Browse the repository at this point in the history
useful for checking the latest value
  • Loading branch information
tabcat committed Sep 15, 2023
1 parent 6450bf5 commit 8fd4760
Showing 1 changed file with 22 additions and 7 deletions.
29 changes: 22 additions & 7 deletions src/namers/w3.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,14 @@ export interface RevisionState {
}

const ipfsPrefix = '/ipfs/'
const revision2cid = (revision: string): CID => {
if (!revision.startsWith(ipfsPrefix)) {
throw new Error('invalid revision: missing /ipfs/ prefix')
}

return CID.parse(revision.slice(ipfsPrefix.length))
}
const cid2revision = (cid: CID): string => ipfsPrefix + cid.toString()

export const revisionState = (datastore: Datastore): RevisionState => {
const get: RevisionState['get'] = async (peerId): Promise<Name.Revision | undefined> => {
Expand All @@ -40,14 +48,14 @@ const pid2Name = (peerId: Ed25519PeerId): Name.Name =>

const publish =
(service: W3NameService, revisions: RevisionState): Namer['publish'] =>
async (peerId: Ed25519PeerId, value: CID) => {
async (peerId: Ed25519PeerId, cid: CID) => {
if (peerId.privateKey == null) {
throw new Error('namers/w3: unable to publish, peerId.privateKey undefined')
}

const name = new Name.WritableName(await keys.unmarshalPrivateKey(peerId.privateKey))

const revisionValue = `${ipfsPrefix}${value.toString()}`
const revisionValue = cid2revision(cid)
const existing = await revisions.get(peerId)
let updated: Name.Revision
if (existing == null) {
Expand All @@ -61,19 +69,26 @@ const publish =
}

const resolve =
(service: W3NameService): Namer['resolve'] =>
(service: W3NameService, revisions: RevisionState, localResolutions: boolean): Namer['resolve'] =>
async (peerId: Ed25519PeerId) => {
let revision: Name.Revision | undefined = await revisions.get(peerId)

if (revision != null && localResolutions) {
return revision2cid(revision.value)
}

try {
const revision = await Name.resolve(pid2Name(peerId), service)
return CID.parse(revision.value.slice(ipfsPrefix.length))
revision = await Name.resolve(pid2Name(peerId), service)
} catch {
throw new Error('unable to resolve peerId to value')
}

return revision2cid(revision.value)
}

export function w3Namer (service: W3NameService, revisions: RevisionState): Namer {
export function w3Namer (service: W3NameService, revisions: RevisionState, options?: { localResolutions: boolean }): Namer {
return {
publish: publish(service, revisions),
resolve: resolve(service)
resolve: resolve(service, revisions, Boolean(options?.localResolutions))
}
}

0 comments on commit 8fd4760

Please # to comment.