Fork of github.com/did-method-plc/did-method-plc
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

Helpers for formatting operations (#11)

* v0.0.2

* make ensureLastOp non-private

* some nicer operation helpers

* bump version

authored by

Daniel Holmgren and committed by
GitHub
19527154 a43b41f0

+58 -50
+1 -1
packages/lib/package.json
··· 1 1 { 2 2 "name": "@did-plc/lib", 3 - "version": "0.0.2", 3 + "version": "0.0.3", 4 4 "main": "src/index.ts", 5 5 "license": "MIT", 6 6 "scripts": {
+1 -1
packages/lib/src/client.ts
··· 93 93 return did 94 94 } 95 95 96 - private async ensureLastOp(did) { 96 + async ensureLastOp(did) { 97 97 const lastOp = await this.getLastOp(did) 98 98 if (check.is(lastOp, t.def.tombstone)) { 99 99 throw new Error('Cannot apply op to tombstone')
+56 -48
packages/lib/src/operations.ts
··· 88 88 ) 89 89 } 90 90 91 + export const createAtprotoUpdateOp = async ( 92 + lastOp: t.CompatibleOp, 93 + signer: Keypair, 94 + opts: Partial<{ 95 + signingKey: string 96 + handle: string 97 + pds: string 98 + rotationKeys: string[] 99 + }>, 100 + ) => { 101 + return createUpdateOp(lastOp, signer, (normalized) => { 102 + const updated = { ...normalized } 103 + if (opts.signingKey) { 104 + updated.verificationMethods = { 105 + ...normalized.verificationMethods, 106 + atproto: opts.signingKey, 107 + } 108 + } 109 + if (opts.handle) { 110 + const formatted = ensureAtprotoPrefix(opts.handle) 111 + const handleI = normalized.alsoKnownAs.findIndex((h) => 112 + h.startsWith('at://'), 113 + ) 114 + if (handleI < 0) { 115 + updated.alsoKnownAs = [formatted, ...normalized.alsoKnownAs] 116 + } else { 117 + updated.alsoKnownAs = [ 118 + ...normalized.alsoKnownAs.slice(0, handleI), 119 + formatted, 120 + ...normalized.alsoKnownAs.slice(handleI + 1), 121 + ] 122 + } 123 + } 124 + if (opts.pds) { 125 + const formatted = ensureHttpPrefix(opts.pds) 126 + updated.services = { 127 + ...normalized.services, 128 + atproto_pds: { 129 + type: 'AtprotoPersonalDataServer', 130 + endpoint: formatted, 131 + }, 132 + } 133 + } 134 + if (opts.rotationKeys) { 135 + updated.rotationKeys = opts.rotationKeys 136 + } 137 + return updated 138 + }) 139 + } 140 + 91 141 export const updateAtprotoKeyOp = async ( 92 142 lastOp: t.CompatibleOp, 93 143 signer: Keypair, 94 - atprotoKey: string, 144 + signingKey: string, 95 145 ): Promise<t.Operation> => { 96 - return createUpdateOp(lastOp, signer, (normalized) => ({ 97 - ...normalized, 98 - verificationMethods: { 99 - ...normalized.verificationMethods, 100 - atproto: atprotoKey, 101 - }, 102 - })) 146 + return createAtprotoUpdateOp(lastOp, signer, { signingKey }) 103 147 } 104 148 105 149 export const updateHandleOp = async ( ··· 107 151 signer: Keypair, 108 152 handle: string, 109 153 ): Promise<t.Operation> => { 110 - const formatted = ensureAtprotoPrefix(handle) 111 - return createUpdateOp(lastOp, signer, (normalized) => { 112 - const handleI = normalized.alsoKnownAs.findIndex((h) => 113 - h.startsWith('at://'), 114 - ) 115 - let aka: string[] 116 - if (handleI < 0) { 117 - aka = [formatted, ...normalized.alsoKnownAs] 118 - } else { 119 - aka = [ 120 - ...normalized.alsoKnownAs.slice(0, handleI), 121 - formatted, 122 - ...normalized.alsoKnownAs.slice(handleI + 1), 123 - ] 124 - } 125 - return { 126 - ...normalized, 127 - alsoKnownAs: aka, 128 - } 129 - }) 154 + return createAtprotoUpdateOp(lastOp, signer, { handle }) 130 155 } 131 156 132 157 export const updatePdsOp = async ( 133 158 lastOp: t.CompatibleOp, 134 159 signer: Keypair, 135 - endpoint: string, 160 + pds: string, 136 161 ): Promise<t.Operation> => { 137 - const formatted = ensureHttpPrefix(endpoint) 138 - return createUpdateOp(lastOp, signer, (normalized) => { 139 - return { 140 - ...normalized, 141 - services: { 142 - ...normalized.services, 143 - atproto_pds: { 144 - type: 'AtprotoPersonalDataServer', 145 - endpoint: formatted, 146 - }, 147 - }, 148 - } 149 - }) 162 + return createAtprotoUpdateOp(lastOp, signer, { pds }) 150 163 } 151 164 152 165 export const updateRotationKeysOp = async ( ··· 154 167 signer: Keypair, 155 168 rotationKeys: string[], 156 169 ): Promise<t.Operation> => { 157 - return createUpdateOp(lastOp, signer, (normalized) => { 158 - return { 159 - ...normalized, 160 - rotationKeys, 161 - } 162 - }) 170 + return createAtprotoUpdateOp(lastOp, signer, { rotationKeys }) 163 171 } 164 172 165 173 export const tombstoneOp = async (