Skip to content

Commit

Permalink
feat: add assetOrVC test case for did spaces (#244)
Browse files Browse the repository at this point in the history
  • Loading branch information
skypesky committed Jan 22, 2024
1 parent 2f329e0 commit 948138a
Show file tree
Hide file tree
Showing 9 changed files with 238 additions and 122 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 0.36.73 (January 22, 2024)

- feat: add assetOrVC test case for did spaces

## 0.36.72 (January 09, 2024)

- chore: bump deps to latest
Expand Down
1 change: 1 addition & 0 deletions api/functions/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ walletHandlers.attach(Object.assign({ app: router }, require('../routes/auth/cla
walletHandlers.attach(Object.assign({ app: router }, require('../routes/auth/test-vc-claim-filter')));
walletHandlers.attach(Object.assign({ app: router }, require('../routes/auth/test-nft-claim-filter')));
walletHandlers.attach(Object.assign({ app: router }, require('../routes/auth/test-nft-or-vc-filter')));
walletHandlers.attach(Object.assign({ app: router }, require('../routes/auth/test-nft-or-vc-filter-only-did-spaces')));
walletHandlers.attach(Object.assign({ app: router }, require('../routes/auth/claim-dynamic')));
walletHandlers.attach(Object.assign({ app: router }, require('../routes/auth/claim-connect-only')));
walletHandlers.attach(Object.assign({ app: router }, require('../routes/auth/claim-connect-optional-vc')));
Expand Down
95 changes: 95 additions & 0 deletions api/routes/auth/test-nft-or-vc-filter-only-did-spaces.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
const { fromPublicKey } = require('@ocap/wallet');
const { toAddress, fromBase58, toBuffer } = require('@ocap/util');
const { toTypeInfo } = require('@arcblock/did');
const { verifyAssetClaim } = require('../../libs/util');
const { wallet } = require('../../libs/auth');

const validateAgentProof = claim => {
const ownerDid = toAddress(claim.ownerDid);
const ownerPk = fromBase58(claim.ownerPk);
if (!claim.agentProof) {
throw new Error('agent proof is empty');
}

if (typeof claim.agentProof === 'string') {
claim.agentProof = JSON.parse(claim.agentProof);
}

logger.info('claim.agentProof.nonce', claim.agentProof.nonce);
logger.info('claim.agentProof.signature', claim.agentProof.signature);

const { nonce } = claim.agentProof;
if (nonce < Math.ceil(Date.now() / 1000) - 5 * 60) {
throw new Error('agent proof is expired: ttl is 5 minutes');
}

const message = Buffer.concat([toBuffer(nonce.toString()), toBuffer(wallet.address)]);
const signer = fromPublicKey(ownerPk, toTypeInfo(ownerDid));
const signature = fromBase58(claim.agentProof.signature);

if (claim.type === 'asset') {
if (!signer.verify(message, signature)) {
throw new Error('agent proof is invalid for asset');
}
}

if (claim.type === 'verifiableCredential') {
if (!signer.verify(message, signature)) {
throw new Error('agent proof is invalid for vc');
}
}
};

module.exports = {
action: 'test_nft_or_vc_filter_only_did_spaces',
claims: {
assetOrVC: () => {
return {
description: "Please provide DID Spaces's NFT or VC to continue",
optional: false,
filters: [
{
tag: 'did-space-purchase-nft', // 用于筛选 NFT
},
{
type: ['PersonalSpaceVerifiableCredential', 'EnterpriseSpaceVerifiableCredential'], // 用于筛选 VC
},
],
meta: {
purpose: 'DidSpace',
},
};
},
},
// eslint-disable-next-line consistent-return
onAuth: async ({ claims, challenge }) => {
const asset = claims.find(x => x.type === 'asset' && x.meta.purpose === 'DidSpace');
const vc = claims.find(x => x.type === 'verifiableCredential' && x.meta.purpose === 'DidSpace');

if (!asset && !vc) {
throw new Error('Neither NFT nor VC is provided');
}

if (asset) {
logger.info('claim.assetOrVC.onAuth.asset', asset);

validateAgentProof(asset, challenge);

const assetState = await verifyAssetClaim({ claim: asset, challenge });
return { successMessage: `You provided asset: ${assetState.address}` };
}

if (vc) {
logger.info('claim.assetOrVC.onAuth.vc', vc);

validateAgentProof(vc, challenge);

const presentation = JSON.parse(vc.presentation);
if (challenge !== presentation.challenge) {
throw Error('Verifiable credential presentation does not have correct challenge');
}

return { successMessage: 'You provided vc' };
}
},
};
1 change: 1 addition & 0 deletions api/routes/auth/test-nft-or-vc-filter.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ module.exports = {
};
},
},
// eslint-disable-next-line consistent-return
onAuth: async ({ claims, challenge }) => {
const asset = claims.find(x => x.type === 'asset');
const vc = claims.find(x => x.type === 'verifiableCredential');
Expand Down
2 changes: 1 addition & 1 deletion blocklet.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ files:
- blocklet.md
- README.md
- build
version: 0.36.72
version: 0.36.73
author:
name: ArcBlock
email: blocklet@arcblock.io
Expand Down
17 changes: 9 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"blocklet.yml",
"README.md"
],
"version": "0.36.72",
"version": "0.36.73",
"author": "wangshijun <shijun@arcblock.io> https://github.com/wangshijun",
"keywords": [
"dapp",
Expand All @@ -31,17 +31,17 @@
"@arcblock/did": "^1.18.108",
"@arcblock/did-auth": "^1.18.108",
"@arcblock/did-auth-storage-nedb": "^1.7.1",
"@arcblock/did-connect": "2.9.6",
"@arcblock/did-playground": "2.9.6",
"@arcblock/did-connect": "2.9.14",
"@arcblock/did-playground": "2.9.14",
"@arcblock/did-util": "^1.18.108",
"@arcblock/jwt": "^1.18.108",
"@arcblock/nft": "^1.18.108",
"@arcblock/nft-template": "^1.18.108",
"@arcblock/react-hooks": "2.9.6",
"@arcblock/ux": "^2.9.6",
"@arcblock/react-hooks": "2.9.14",
"@arcblock/ux": "^2.9.14",
"@arcblock/vc": "^1.18.108",
"@blocklet/sdk": "1.16.21",
"@blocklet/ui-react": "2.9.6",
"@blocklet/sdk": "1.16.22",
"@blocklet/ui-react": "2.9.14",
"@emotion/react": "^11.10.5",
"@emotion/styled": "^11.10.5",
"@mui/icons-material": "^5.10.2",
Expand Down Expand Up @@ -111,6 +111,7 @@
"workbox-webpack-plugin": "^6.5.4"
},
"scripts": {
"dev": "blocklet dev",
"lint": "eslint src api",
"lint:fix": "eslint src api --fix",
"precommit": "npm run lint",
Expand Down Expand Up @@ -178,4 +179,4 @@
"@ocap/wallet": "1.18.108"
},
"license": "Apache-2.0"
}
}
11 changes: 11 additions & 0 deletions src/pages/full.js
Original file line number Diff line number Diff line change
Expand Up @@ -1353,6 +1353,17 @@ export default function IndexPage() {
success: 'Verified',
}}
/>
<AuthButton
button="出示 NFT 或 VC(DID Spaces)"
action="test_nft_or_vc_filter_only_did_spaces"
extraParams={{ type: 'either-nft-or-vc' }}
messages={{
title: 'Provide NFT of VC(DID Spaces)',
scan: 'Connect your DID Wallet to continue',
confirm: 'Confirm on your DID Wallet',
success: 'Verified',
}}
/>
</div>
</section>
<section className="section">
Expand Down
2 changes: 1 addition & 1 deletion version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.36.72
0.36.73

0 comments on commit 948138a

Please sign in to comment.