Browse Source

fix the local ETHR resolver code to work in Docker (because it loaded as undefined)

master
Trent Larson 4 months ago
parent
commit
a23f6d10ad
  1. 3
      CHANGELOG.md
  2. 1
      Dockerfile
  3. 1
      README.md
  4. 4
      src/server.ts
  5. 47
      src/vc/did-eth-local-resolver.js
  6. 45
      src/vc/did-eth-local-resolver.ts

3
CHANGELOG.md

@ -10,7 +10,6 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Nothing - Nothing
## [1.2.2]
### Added ### Added
- Replacement of an existing file - Replacement of an existing file
- Local resolver for did:ethr - Local resolver for did:ethr
@ -18,6 +17,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Testing for file deletion - Testing for file deletion
### Fixed ### Fixed
- Incorrect check for others who recorded same image - Incorrect check for others who recorded same image
### Changed
- Dockerfile uses a builder image
### Changed in DB or environment ### Changed in DB or environment
- New SQL migration (for the new file deletion feature) - New SQL migration (for the new file deletion feature)

1
Dockerfile

@ -7,6 +7,7 @@ RUN apk add git
RUN git clone https://gitea.anomalistdesign.com/log-trade/image-api.git RUN git clone https://gitea.anomalistdesign.com/log-trade/image-api.git
WORKDIR image-api WORKDIR image-api
RUN git checkout $IMAGE_API_VERSION RUN git checkout $IMAGE_API_VERSION
# dev dependencies like TypeScript are needed to build
RUN pnpm install RUN pnpm install
RUN pnpm build RUN pnpm build
RUN pnpm install --prod RUN pnpm install --prod

1
README.md

@ -39,6 +39,7 @@ make -C test -j
```shell ```shell
# run this first command in a directory where `npm install did-jwt` has been run # run this first command in a directory where `npm install did-jwt` has been run
CODE='OWNER_DID="did:ethr:0x0000694B58C2cC69658993A90D3840C560f2F51F"; OWNER_PRIVATE_KEY_HEX="2b6472c026ec2aa2c4235c994a63868fc9212d18b58f6cbfe861b52e71330f5b"; didJwt = require("did-jwt"); didJwt.createJWT({ exp: Math.floor(Date.now() / 1000) + 60, iat: Math.floor(Date.now() / 1000), iss: OWNER_DID }, { issuer: OWNER_DID, signer: didJwt.SimpleSigner(OWNER_PRIVATE_KEY_HEX) }).then(console.log)' CODE='OWNER_DID="did:ethr:0x0000694B58C2cC69658993A90D3840C560f2F51F"; OWNER_PRIVATE_KEY_HEX="2b6472c026ec2aa2c4235c994a63868fc9212d18b58f6cbfe861b52e71330f5b"; didJwt = require("did-jwt"); didJwt.createJWT({ exp: Math.floor(Date.now() / 1000) + 60, iat: Math.floor(Date.now() / 1000), iss: OWNER_DID }, { issuer: OWNER_DID, signer: didJwt.SimpleSigner(OWNER_PRIVATE_KEY_HEX) }).then(console.log)'
JWT=`node -e "$CODE"`; curl -X GET -H "Authorization: Bearer $JWT" http://localhost:3001/image-limits
JWT=`node -e "$CODE"`; curl -X POST -H "Authorization: Bearer $JWT" -F "image=@./test.png" http://localhost:3001/image JWT=`node -e "$CODE"`; curl -X POST -H "Authorization: Bearer $JWT" -F "image=@./test.png" http://localhost:3001/image
JWT=`node -e "$CODE"`; curl -X DELETE -H "Authorization: Bearer $JWT" http://localhost:3001/image/https%3A%2F%2Fgifts-image-test.s3.amazonaws.com%2F4599145c3a8792a678f458747f2d8512c680e8680bf5563c35b06cd770051ed2.png JWT=`node -e "$CODE"`; curl -X DELETE -H "Authorization: Bearer $JWT" http://localhost:3001/image/https%3A%2F%2Fgifts-image-test.s3.amazonaws.com%2F4599145c3a8792a678f458747f2d8512c680e8680bf5563c35b06cd770051ed2.png
``` ```

4
src/server.ts

@ -65,8 +65,8 @@ app.get('/image-limits', async (req, res) => {
nextWeekBeginDateTime: limitsResult.nextWeekBeginDateTime nextWeekBeginDateTime: limitsResult.nextWeekBeginDateTime
}); });
} catch (e) { } catch (e) {
console.error('Error getting image limits:', e, ' ... with this string: ' + e); console.error('Error getting image limits:', e, ' ... with this string: ' + JSON.stringify(e));
return res.status(500).send({ success: false, message: 'Got this error retrieving limits: ' + e }); return res.status(500).send({ success: false, message: 'Got this error retrieving limits: ' + JSON.stringify(e) });
} }
}); });

47
src/vc/did-eth-local-resolver.js

@ -1,47 +0,0 @@
const { DIDResolutionResult } = require('did-resolver');
/**
* This did:ethr resolver instructs the did-jwt machinery to use the
* EcdsaSecp256k1RecoveryMethod2020Uses verification method which adds the recovery bit to the
* signature to recover the DID's public key from a signature.
*
* This effectively hard codes the did:ethr DID resolver to use the address as the public key.
* @param did : string
* @returns {Promise<DIDResolutionResult>}
*
* Similar code resides in endorser-ch
*/
const didEthLocalResolver = async(did) => {
const didRegex = /^did:ethr:(0x[0-9a-fA-F]{40})$/;
const match = did.match(didRegex);
if (match) {
const address = match[1]; // Extract eth address: 0x...
const publicKeyHex = address; // Use the address directly as a public key placeholder
return {
didDocumentMetadata: {},
didResolutionMetadata: {
contentType: "application/did+ld+json"
},
didDocument: {
'@context': [
'https://www.w3.org/ns/did/v1',
"https://w3id.org/security/suites/secp256k1recovery-2020/v2"
],
id: did,
verificationMethod: [{
id: `${did}#controller`,
type: 'EcdsaSecp256k1RecoveryMethod2020',
controller: did,
blockchainAccountId: "eip155:1:" + publicKeyHex,
}],
authentication: [`${did}#controller`],
assertionMethod: [`${did}#controller`],
},
};
}
throw new Error(`Unsupported DID format: ${did}`);
};
module.exports = { didEthLocalResolver };

45
src/vc/did-eth-local-resolver.ts

@ -1,5 +1,46 @@
import {DIDResolutionResult} from "did-resolver"; import {DIDResolutionResult} from "did-resolver";
declare module './did-eth-local-resolver.js' { /**
export function didEthLocalResolver(jwt: string): Promise<DIDResolutionResult>; * This did:ethr resolver instructs the did-jwt machinery to use the
* EcdsaSecp256k1RecoveryMethod2020Uses verification method which adds the recovery bit to the
* signature to recover the DID's public key from a signature.
*
* This effectively hard codes the did:ethr DID resolver to use the address as the public key.
* @param did : string
* @returns {Promise<DIDResolutionResult>}
*
* Similar code resides in endorser-ch
*/
export const didEthLocalResolver = async(did: string): Promise<DIDResolutionResult> => {
const didRegex = /^did:ethr:(0x[0-9a-fA-F]{40})$/;
const match = did.match(didRegex);
if (match) {
const address = match[1]; // Extract eth address: 0x...
const publicKeyHex = address; // Use the address directly as a public key placeholder
return {
didDocumentMetadata: {},
didResolutionMetadata: {
contentType: "application/did+ld+json"
},
didDocument: {
'@context': [
'https://www.w3.org/ns/did/v1',
"https://w3id.org/security/suites/secp256k1recovery-2020/v2"
],
id: did,
verificationMethod: [{
id: `${did}#controller`,
type: 'EcdsaSecp256k1RecoveryMethod2020',
controller: did,
blockchainAccountId: "eip155:1:" + publicKeyHex,
}],
authentication: [`${did}#controller`],
assertionMethod: [`${did}#controller`],
},
};
} }
throw new Error(`Unsupported DID format: ${did}`);
};

Loading…
Cancel
Save