create-blocklet 0.9.5 → 0.9.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/common/.prettierrc +11 -1
- package/common/scripts/build-clean.mjs +0 -1
- package/common/scripts/bump-version.mjs +33 -29
- package/index.js +86 -10
- package/package.json +1 -1
- package/templates/base-readme.md +103 -0
- package/templates/did-connect-dapp/.eslintrc.js +4 -0
- package/templates/did-connect-dapp/README.md +143 -0
- package/templates/did-connect-dapp/api/dev.js +5 -0
- package/templates/did-connect-dapp/api/hooks/pre-start.js +33 -0
- package/templates/did-connect-dapp/api/index.js +48 -0
- package/templates/did-connect-dapp/api/libs/auth.js +25 -0
- package/templates/did-connect-dapp/api/libs/env.js +9 -0
- package/templates/did-connect-dapp/api/libs/logger.js +3 -0
- package/templates/did-connect-dapp/api/libs/utils.js +70 -0
- package/templates/did-connect-dapp/api/routes/auth/index.js +15 -0
- package/templates/did-connect-dapp/api/routes/auth/request-digest-signature.js +51 -0
- package/templates/did-connect-dapp/api/routes/auth/request-multiple-claims.js +52 -0
- package/templates/did-connect-dapp/api/routes/auth/request-multiple-steps.js +57 -0
- package/templates/did-connect-dapp/api/routes/auth/request-nft.js +82 -0
- package/templates/did-connect-dapp/api/routes/auth/request-payment.js +72 -0
- package/templates/did-connect-dapp/api/routes/auth/request-profile.js +25 -0
- package/templates/did-connect-dapp/api/routes/auth/request-text-signature.js +44 -0
- package/templates/did-connect-dapp/api/routes/auth/request-transaction-signature.js +62 -0
- package/templates/did-connect-dapp/blocklet.md +5 -0
- package/templates/did-connect-dapp/blocklet.yml +57 -0
- package/templates/did-connect-dapp/index.html +17 -0
- package/templates/did-connect-dapp/package.json +86 -0
- package/templates/did-connect-dapp/src/app.jsx +20 -0
- package/templates/did-connect-dapp/src/assets/get_wallet_en.png +0 -0
- package/templates/did-connect-dapp/src/assets/get_wallet_zh.png +0 -0
- package/templates/did-connect-dapp/src/components/connect-item.jsx +39 -0
- package/templates/did-connect-dapp/src/components/connects/request-digest-signature.jsx +45 -0
- package/templates/did-connect-dapp/src/components/connects/request-multiple-claims.jsx +55 -0
- package/templates/did-connect-dapp/src/components/connects/request-multiple-steps.jsx +54 -0
- package/templates/did-connect-dapp/src/components/connects/request-nft.jsx +75 -0
- package/templates/did-connect-dapp/src/components/connects/request-payment.jsx +82 -0
- package/templates/did-connect-dapp/src/components/connects/request-profile.jsx +72 -0
- package/templates/did-connect-dapp/src/components/connects/request-text-signature.jsx +44 -0
- package/templates/did-connect-dapp/src/components/connects/request-transaction-signature.jsx +44 -0
- package/templates/did-connect-dapp/src/components/info-row.jsx +39 -0
- package/templates/did-connect-dapp/src/components/layout.jsx +26 -0
- package/templates/did-connect-dapp/src/index.jsx +6 -0
- package/templates/did-connect-dapp/src/libs/session.js +11 -0
- package/templates/did-connect-dapp/src/libs/utils.js +4 -0
- package/templates/did-connect-dapp/src/locales/en.js +115 -0
- package/templates/did-connect-dapp/src/locales/index.js +5 -0
- package/templates/did-connect-dapp/src/locales/zh.js +115 -0
- package/templates/did-connect-dapp/src/pages/main.jsx +95 -0
- package/templates/did-connect-dapp/template-info.json +12 -0
- package/templates/did-connect-dapp/vite.config.mjs +11 -0
- package/templates/did-wallet-dapp/.eslintrc.js +7 -0
- package/templates/did-wallet-dapp/README.md +32 -0
- package/templates/did-wallet-dapp/api/dev.js +8 -0
- package/templates/did-wallet-dapp/api/functions/app.js +65 -0
- package/templates/did-wallet-dapp/api/index.js +13 -0
- package/templates/did-wallet-dapp/api/libs/constant.js +1 -0
- package/templates/did-wallet-dapp/api/routes/user.js +30 -0
- package/templates/did-wallet-dapp/blocklet.md +3 -0
- package/templates/did-wallet-dapp/blocklet.yml +58 -0
- package/templates/did-wallet-dapp/index.html +16 -0
- package/templates/did-wallet-dapp/package.json +84 -0
- package/templates/did-wallet-dapp/src/app.jsx +38 -0
- package/templates/did-wallet-dapp/src/assets/blocklet.svg +16 -0
- package/templates/did-wallet-dapp/src/assets/react.svg +1 -0
- package/templates/did-wallet-dapp/src/assets/vite.svg +1 -0
- package/templates/did-wallet-dapp/src/assets/wallet.png +0 -0
- package/templates/did-wallet-dapp/src/components/layout.jsx +28 -0
- package/templates/did-wallet-dapp/src/global.css +79 -0
- package/templates/did-wallet-dapp/src/index.jsx +6 -0
- package/templates/did-wallet-dapp/src/libs/api.js +7 -0
- package/templates/did-wallet-dapp/src/libs/session.js +13 -0
- package/templates/did-wallet-dapp/src/locales/en.js +13 -0
- package/templates/did-wallet-dapp/src/locales/index.js +4 -0
- package/templates/did-wallet-dapp/src/locales/zh.js +13 -0
- package/templates/did-wallet-dapp/src/pages/home/index.css +39 -0
- package/templates/did-wallet-dapp/src/pages/home/index.jsx +57 -0
- package/templates/did-wallet-dapp/src/pages/profile.jsx +136 -0
- package/templates/did-wallet-dapp/template-info.json +12 -0
- package/templates/did-wallet-dapp/vite.config.js +14 -0
- package/templates/did-wallet-dapp/vite.config.server.js +9 -0
- package/templates/express-api/README.md +2 -124
- package/templates/express-api/blocklet.yml +0 -1
- package/templates/express-api/package.json +9 -8
- package/templates/html-static/README.md +3 -125
- package/templates/html-static/blocklet.yml +0 -1
- package/templates/html-static/package.json +4 -3
- package/templates/monorepo/README.md +8 -8
- package/templates/monorepo/package.json +4 -3
- package/templates/monorepo/scripts/bump-version.mjs +36 -32
- package/templates/nestjs-api/README.md +2 -123
- package/templates/nestjs-api/blocklet.yml +0 -1
- package/templates/nestjs-api/package.json +16 -15
- package/templates/nextjs-dapp/README.md +2 -125
- package/templates/nextjs-dapp/blocklet.yml +0 -1
- package/templates/nextjs-dapp/package.json +7 -7
- package/templates/react-dapp/README.md +2 -124
- package/templates/react-dapp/blocklet.yml +0 -1
- package/templates/react-dapp/package.json +16 -15
- package/templates/react-dapp-ts/README.md +2 -125
- package/templates/react-dapp-ts/blocklet.yml +0 -1
- package/templates/react-dapp-ts/package.json +19 -18
- package/templates/react-gun-dapp/README.md +2 -125
- package/templates/react-gun-dapp/blocklet.yml +0 -1
- package/templates/react-gun-dapp/package.json +15 -14
- package/templates/react-static/README.md +2 -125
- package/templates/react-static/blocklet.yml +0 -1
- package/templates/react-static/package.json +11 -10
- package/templates/solidjs-dapp/README.md +2 -125
- package/templates/solidjs-dapp/blocklet.yml +0 -1
- package/templates/solidjs-dapp/package.json +14 -13
- package/templates/solidjs-static/README.md +2 -123
- package/templates/solidjs-static/blocklet.yml +0 -1
- package/templates/solidjs-static/package.json +9 -8
- package/templates/svelte-dapp/README.md +2 -124
- package/templates/svelte-dapp/blocklet.yml +0 -1
- package/templates/svelte-dapp/package.json +12 -11
- package/templates/svelte-static/README.md +2 -125
- package/templates/svelte-static/blocklet.yml +0 -1
- package/templates/svelte-static/package.json +8 -7
- package/templates/todo-list-example/blocklet.yml +0 -1
- package/templates/todo-list-example/package.json +11 -10
- package/templates/vue-dapp/README.md +2 -125
- package/templates/vue-dapp/blocklet.yml +0 -1
- package/templates/vue-dapp/package.json +16 -15
- package/templates/vue-static/README.md +2 -125
- package/templates/vue-static/blocklet.yml +0 -1
- package/templates/vue-static/package.json +11 -10
- package/templates/vue-ts-static/README.md +2 -124
- package/templates/vue-ts-static/blocklet.yml +1 -2
- package/templates/vue-ts-static/package.json +12 -12
- package/templates/vue2-dapp/README.md +2 -125
- package/templates/vue2-dapp/blocklet.yml +0 -1
- package/templates/vue2-dapp/package.json +13 -12
- package/templates/vue2-static/README.md +2 -125
- package/templates/vue2-static/blocklet.yml +0 -1
- package/templates/vue2-static/package.json +9 -8
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
const { getRandomBytes } = require('@ocap/mcrypto');
|
|
2
|
+
const pick = require('lodash/pick');
|
|
3
|
+
const { toAddress, fromBase58 } = require('@ocap/util');
|
|
4
|
+
const { isFromPublicKey, toTypeInfo } = require('@arcblock/did');
|
|
5
|
+
const { fromPublicKey } = require('@ocap/wallet');
|
|
6
|
+
|
|
7
|
+
const { client } = require('./auth');
|
|
8
|
+
const env = require('./env');
|
|
9
|
+
|
|
10
|
+
const getRandomMessage = (len = 16) => {
|
|
11
|
+
const hex = getRandomBytes(len);
|
|
12
|
+
return hex.replace(/^0x/, '').toUpperCase();
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
const getTokenInfo = async () => {
|
|
16
|
+
const [{ state }] = await Promise.all([client.getTokenState({ address: env.localTokenId })]);
|
|
17
|
+
|
|
18
|
+
const result = {
|
|
19
|
+
symbol: state.symbol,
|
|
20
|
+
decimal: state.decimal,
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
return result;
|
|
24
|
+
};
|
|
25
|
+
const pickGasPayerHeaders = ({ headers }) => ({ headers: pick(headers, ['x-gas-payer-sig', 'x-gas-payer-pk']) });
|
|
26
|
+
|
|
27
|
+
const verifyAssetClaim = async ({ claim, challenge, trustedIssuers = [], trustedParents = [] }) => {
|
|
28
|
+
const fields = ['asset', 'ownerProof', 'ownerPk', 'ownerDid'];
|
|
29
|
+
for (const field of fields) {
|
|
30
|
+
if (!claim[field]) {
|
|
31
|
+
throw new Error(`Invalid asset claim: ${field} is missing`);
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
const address = claim.asset;
|
|
36
|
+
const ownerDid = toAddress(claim.ownerDid);
|
|
37
|
+
const ownerPk = fromBase58(claim.ownerPk);
|
|
38
|
+
const ownerProof = fromBase58(claim.ownerProof);
|
|
39
|
+
if (isFromPublicKey(ownerDid, ownerPk) === false) {
|
|
40
|
+
throw new Error('Invalid asset claim: owner did and pk mismatch');
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
const owner = fromPublicKey(ownerPk, toTypeInfo(ownerDid));
|
|
44
|
+
if (owner.verify(challenge, ownerProof) === false) {
|
|
45
|
+
throw new Error('Invalid asset claim: owner proof invalid');
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
const { state } = await client.getAssetState({ address }, { ignoreFields: ['context'] });
|
|
49
|
+
if (!state) {
|
|
50
|
+
throw new Error('Invalid asset claim: asset not found on chain');
|
|
51
|
+
}
|
|
52
|
+
if (state.owner !== ownerDid) {
|
|
53
|
+
throw new Error('Invalid asset claim: owner does not match with on chain state');
|
|
54
|
+
}
|
|
55
|
+
if (trustedIssuers.length && trustedIssuers.includes(state.issuer) === false) {
|
|
56
|
+
throw new Error('Invalid asset claim: asset issuer not in whitelist');
|
|
57
|
+
}
|
|
58
|
+
if (trustedParents.length && trustedParents.includes(state.parent) === false) {
|
|
59
|
+
throw new Error('Invalid asset claim: asset parent not in whitelist');
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
return state;
|
|
63
|
+
};
|
|
64
|
+
|
|
65
|
+
module.exports = {
|
|
66
|
+
getRandomMessage,
|
|
67
|
+
getTokenInfo,
|
|
68
|
+
pickGasPayerHeaders,
|
|
69
|
+
verifyAssetClaim,
|
|
70
|
+
};
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/* eslint-disable global-require */
|
|
2
|
+
const { handlers } = require('../../libs/auth');
|
|
3
|
+
|
|
4
|
+
module.exports = {
|
|
5
|
+
init(app) {
|
|
6
|
+
handlers.attach({ app, ...require('./request-profile') });
|
|
7
|
+
handlers.attach({ app, ...require('./request-text-signature') });
|
|
8
|
+
handlers.attach({ app, ...require('./request-digest-signature') });
|
|
9
|
+
handlers.attach({ app, ...require('./request-transaction-signature') });
|
|
10
|
+
handlers.attach({ app, ...require('./request-payment') });
|
|
11
|
+
handlers.attach({ app, ...require('./request-nft') });
|
|
12
|
+
handlers.attach({ app, ...require('./request-multiple-claims') });
|
|
13
|
+
handlers.attach({ app, ...require('./request-multiple-steps') });
|
|
14
|
+
},
|
|
15
|
+
};
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
const { toTypeInfo } = require('@arcblock/did');
|
|
2
|
+
const { fromPublicKey } = require('@ocap/wallet');
|
|
3
|
+
const { types, getHasher } = require('@ocap/mcrypto');
|
|
4
|
+
const { toBase58 } = require('@ocap/util');
|
|
5
|
+
|
|
6
|
+
const logger = require('../../libs/logger');
|
|
7
|
+
|
|
8
|
+
const hasher = getHasher(types.HashType.SHA3);
|
|
9
|
+
const data = 'abcdefghijklmnopqrstuvwxyz'.repeat(32);
|
|
10
|
+
|
|
11
|
+
const action = 'request-digest-signature';
|
|
12
|
+
|
|
13
|
+
module.exports = {
|
|
14
|
+
action,
|
|
15
|
+
claims: {
|
|
16
|
+
signature: () => {
|
|
17
|
+
return {
|
|
18
|
+
description: 'Please sign the digest',
|
|
19
|
+
digest: toBase58(hasher(data, 1)),
|
|
20
|
+
};
|
|
21
|
+
},
|
|
22
|
+
},
|
|
23
|
+
|
|
24
|
+
onAuth: ({ userDid, userPk, claims, updateSession }) => {
|
|
25
|
+
const type = toTypeInfo(userDid);
|
|
26
|
+
const user = fromPublicKey(userPk, type);
|
|
27
|
+
const claim = claims.find((x) => x.type === 'signature');
|
|
28
|
+
|
|
29
|
+
logger.info(`${action}.onAuth`, { userPk, userDid, claim });
|
|
30
|
+
|
|
31
|
+
if (claim.origin) {
|
|
32
|
+
if (user.verify(claim.origin, claim.sig, claim.method !== 'none') === false) {
|
|
33
|
+
throw new Error('Invalid origin signature');
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
// We do not need to hash the data when verifying
|
|
38
|
+
if (claim.digest) {
|
|
39
|
+
if (user.verify(claim.digest, claim.sig, false) === false) {
|
|
40
|
+
throw new Error('Invalid digest signature');
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
updateSession({
|
|
44
|
+
result: {
|
|
45
|
+
origin: data,
|
|
46
|
+
digest: claim.digest,
|
|
47
|
+
sig: claim.sig,
|
|
48
|
+
},
|
|
49
|
+
});
|
|
50
|
+
},
|
|
51
|
+
};
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
const { fromBase58, toBase58 } = require('@ocap/util');
|
|
2
|
+
const { getHasher, types } = require('@ocap/mcrypto');
|
|
3
|
+
|
|
4
|
+
const { getRandomMessage } = require('../../libs/utils');
|
|
5
|
+
const logger = require('../../libs/logger');
|
|
6
|
+
|
|
7
|
+
const hasher = getHasher(types.HashType.SHA3);
|
|
8
|
+
const data = 'abcdefghijklmnopqrstuvwxyz'.repeat(32);
|
|
9
|
+
|
|
10
|
+
const action = 'request-multiple-claims';
|
|
11
|
+
|
|
12
|
+
module.exports = {
|
|
13
|
+
action,
|
|
14
|
+
claims: {
|
|
15
|
+
signText: [
|
|
16
|
+
'signature',
|
|
17
|
+
() => {
|
|
18
|
+
return {
|
|
19
|
+
type: 'mime:text/plain',
|
|
20
|
+
data: getRandomMessage(),
|
|
21
|
+
description: 'Please sign the text',
|
|
22
|
+
};
|
|
23
|
+
},
|
|
24
|
+
],
|
|
25
|
+
signDigest: [
|
|
26
|
+
'signature',
|
|
27
|
+
() => {
|
|
28
|
+
return {
|
|
29
|
+
description: 'Please sign the digest',
|
|
30
|
+
digest: toBase58(hasher(data, 1)),
|
|
31
|
+
};
|
|
32
|
+
},
|
|
33
|
+
],
|
|
34
|
+
},
|
|
35
|
+
|
|
36
|
+
onAuth: ({ userDid, userPk, claims, updateSession }) => {
|
|
37
|
+
logger.info(`${action}.onAuth`, { userPk, userDid, claims });
|
|
38
|
+
updateSession({
|
|
39
|
+
result: [
|
|
40
|
+
{
|
|
41
|
+
origin: fromBase58(claims[0].origin).toString(),
|
|
42
|
+
sig: claims[0].sig,
|
|
43
|
+
},
|
|
44
|
+
{
|
|
45
|
+
origin: data,
|
|
46
|
+
digest: claims[1].digest,
|
|
47
|
+
sig: claims[1].sig,
|
|
48
|
+
},
|
|
49
|
+
],
|
|
50
|
+
});
|
|
51
|
+
},
|
|
52
|
+
};
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
const { fromBase58, toBase58 } = require('@ocap/util');
|
|
2
|
+
const { getHasher, types } = require('@ocap/mcrypto');
|
|
3
|
+
|
|
4
|
+
const { getRandomMessage } = require('../../libs/utils');
|
|
5
|
+
const logger = require('../../libs/logger');
|
|
6
|
+
|
|
7
|
+
const hasher = getHasher(types.HashType.SHA3);
|
|
8
|
+
const data = 'abcdefghijklmnopqrstuvwxyz'.repeat(32);
|
|
9
|
+
|
|
10
|
+
const action = 'request-multiple-steps';
|
|
11
|
+
|
|
12
|
+
module.exports = {
|
|
13
|
+
action,
|
|
14
|
+
claims: [
|
|
15
|
+
{
|
|
16
|
+
signature: () => {
|
|
17
|
+
return {
|
|
18
|
+
type: 'mime:text/plain',
|
|
19
|
+
data: getRandomMessage(),
|
|
20
|
+
description: 'Please sign the text',
|
|
21
|
+
};
|
|
22
|
+
},
|
|
23
|
+
},
|
|
24
|
+
{
|
|
25
|
+
signature: () => {
|
|
26
|
+
return {
|
|
27
|
+
description: 'Please sign the digest',
|
|
28
|
+
digest: toBase58(hasher(data, 1)),
|
|
29
|
+
};
|
|
30
|
+
},
|
|
31
|
+
},
|
|
32
|
+
],
|
|
33
|
+
|
|
34
|
+
onAuth: ({ userDid, userPk, claims, step, req, updateSession }) => {
|
|
35
|
+
logger.info(`${action}.onAuth`, { step, userPk, userDid, claims });
|
|
36
|
+
const result = req?.context?.store?.result || [];
|
|
37
|
+
const claim = claims.find((x) => x.type === 'signature');
|
|
38
|
+
if (step === 1) {
|
|
39
|
+
result.push({
|
|
40
|
+
step,
|
|
41
|
+
origin: fromBase58(claim.origin).toString(),
|
|
42
|
+
sig: claim.sig,
|
|
43
|
+
});
|
|
44
|
+
} else if (step === 2) {
|
|
45
|
+
result.push({
|
|
46
|
+
step,
|
|
47
|
+
origin: data,
|
|
48
|
+
digest: claim.digest,
|
|
49
|
+
sig: claim.sig,
|
|
50
|
+
});
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
updateSession({
|
|
54
|
+
result,
|
|
55
|
+
});
|
|
56
|
+
},
|
|
57
|
+
};
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
const { fromPublicKey } = require('@ocap/wallet');
|
|
2
|
+
const { toAddress, fromBase58, toBuffer } = require('@ocap/util');
|
|
3
|
+
const { toTypeInfo } = require('@arcblock/did');
|
|
4
|
+
|
|
5
|
+
const { verifyAssetClaim } = require('../../libs/utils');
|
|
6
|
+
const { wallet } = require('../../libs/auth');
|
|
7
|
+
const logger = require('../../libs/logger');
|
|
8
|
+
|
|
9
|
+
const validateAgentProof = (claim) => {
|
|
10
|
+
const ownerDid = toAddress(claim.ownerDid);
|
|
11
|
+
const ownerPk = fromBase58(claim.ownerPk);
|
|
12
|
+
if (!claim.agentProof) {
|
|
13
|
+
throw new Error('agent proof is empty');
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
if (typeof claim.agentProof === 'string') {
|
|
17
|
+
claim.agentProof = JSON.parse(claim.agentProof);
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
logger.info('claim.agentProof.nonce', claim.agentProof.nonce);
|
|
21
|
+
logger.info('claim.agentProof.signature', claim.agentProof.signature);
|
|
22
|
+
|
|
23
|
+
const { nonce } = claim.agentProof;
|
|
24
|
+
if (nonce < Math.ceil(Date.now() / 1000) - 5 * 60) {
|
|
25
|
+
throw new Error('agent proof is expired: ttl is 5 minutes');
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
const message = Buffer.concat([toBuffer(nonce.toString()), toBuffer(wallet.address)]);
|
|
29
|
+
const signer = fromPublicKey(ownerPk, toTypeInfo(ownerDid));
|
|
30
|
+
const signature = fromBase58(claim.agentProof.signature);
|
|
31
|
+
|
|
32
|
+
if (claim.type === 'asset') {
|
|
33
|
+
if (!signer.verify(message, signature)) {
|
|
34
|
+
throw new Error('agent proof is invalid for asset');
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
if (claim.type === 'verifiableCredential') {
|
|
39
|
+
if (!signer.verify(message, signature)) {
|
|
40
|
+
throw new Error('agent proof is invalid for vc');
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
const action = 'request-nft';
|
|
46
|
+
|
|
47
|
+
module.exports = {
|
|
48
|
+
action,
|
|
49
|
+
claims: {
|
|
50
|
+
assetOrVC: () => {
|
|
51
|
+
return {
|
|
52
|
+
description: 'Please provide NFT or VC to continue',
|
|
53
|
+
filters: [
|
|
54
|
+
{
|
|
55
|
+
type: ['NFTBadge', 'NFTCertificate'],
|
|
56
|
+
trustedIssuers: [
|
|
57
|
+
// wallet.address,
|
|
58
|
+
'zNKXAEjKYXEnf2hf18NjTQsa1JajA9gJ3haY',
|
|
59
|
+
],
|
|
60
|
+
},
|
|
61
|
+
],
|
|
62
|
+
};
|
|
63
|
+
},
|
|
64
|
+
},
|
|
65
|
+
onAuth: async ({ claims, challenge, updateSession }) => {
|
|
66
|
+
const asset = claims.find((x) => x.type === 'asset');
|
|
67
|
+
|
|
68
|
+
if (!asset) {
|
|
69
|
+
throw new Error('Neither NFT nor VC is provided');
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
logger.info(`${action}.onAuth.asset`, asset);
|
|
73
|
+
|
|
74
|
+
validateAgentProof(asset, challenge);
|
|
75
|
+
|
|
76
|
+
const assetState = await verifyAssetClaim({ claim: asset, challenge });
|
|
77
|
+
updateSession({
|
|
78
|
+
result: asset,
|
|
79
|
+
});
|
|
80
|
+
return { successMessage: `You provided asset: ${assetState.address}` };
|
|
81
|
+
},
|
|
82
|
+
};
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
/* eslint-disable no-console */
|
|
2
|
+
const { fromTokenToUnit } = require('@ocap/util');
|
|
3
|
+
const { fromAddress } = require('@ocap/wallet');
|
|
4
|
+
|
|
5
|
+
const { wallet, client } = require('../../libs/auth');
|
|
6
|
+
const { getTokenInfo, pickGasPayerHeaders } = require('../../libs/utils');
|
|
7
|
+
const env = require('../../libs/env');
|
|
8
|
+
const logger = require('../../libs/logger');
|
|
9
|
+
|
|
10
|
+
const action = 'request-payment';
|
|
11
|
+
module.exports = {
|
|
12
|
+
action,
|
|
13
|
+
claims: {
|
|
14
|
+
signature: async ({ userDid, userPk }) => {
|
|
15
|
+
const amount = 1;
|
|
16
|
+
const token = await getTokenInfo();
|
|
17
|
+
|
|
18
|
+
return {
|
|
19
|
+
type: 'TransferV2Tx',
|
|
20
|
+
data: {
|
|
21
|
+
from: userDid,
|
|
22
|
+
pk: userPk,
|
|
23
|
+
itx: {
|
|
24
|
+
to: wallet.address,
|
|
25
|
+
tokens: [
|
|
26
|
+
{
|
|
27
|
+
address: env.localTokenId,
|
|
28
|
+
value: fromTokenToUnit(amount, token.decimal).toString(),
|
|
29
|
+
},
|
|
30
|
+
],
|
|
31
|
+
},
|
|
32
|
+
},
|
|
33
|
+
description: `Please pay ${amount} ${token.symbol} to application`,
|
|
34
|
+
};
|
|
35
|
+
},
|
|
36
|
+
},
|
|
37
|
+
onAuth: async ({ req, claims, userDid, updateSession }) => {
|
|
38
|
+
try {
|
|
39
|
+
const claim = claims.find((x) => x.type === 'signature');
|
|
40
|
+
const tx = client.decodeTx(claim.origin);
|
|
41
|
+
const user = fromAddress(userDid);
|
|
42
|
+
if (claim.from) {
|
|
43
|
+
tx.from = claim.from;
|
|
44
|
+
}
|
|
45
|
+
if (claim.delegator) {
|
|
46
|
+
tx.delegator = claim.delegator;
|
|
47
|
+
}
|
|
48
|
+
const hash = await client.sendTransferV2Tx(
|
|
49
|
+
{
|
|
50
|
+
tx,
|
|
51
|
+
wallet: user,
|
|
52
|
+
signature: claim.sig,
|
|
53
|
+
},
|
|
54
|
+
pickGasPayerHeaders(req),
|
|
55
|
+
);
|
|
56
|
+
|
|
57
|
+
logger.info(`${action}.onAuth`, { claims, userDid, hash });
|
|
58
|
+
updateSession({
|
|
59
|
+
result: {
|
|
60
|
+
hash,
|
|
61
|
+
tx,
|
|
62
|
+
origin: claim.origin,
|
|
63
|
+
sig: claim.sig,
|
|
64
|
+
},
|
|
65
|
+
});
|
|
66
|
+
return { hash, tx: claim.origin };
|
|
67
|
+
} catch (err) {
|
|
68
|
+
logger.info(`${action}.onAuth.error`, err);
|
|
69
|
+
throw new Error('Send token failed!');
|
|
70
|
+
}
|
|
71
|
+
},
|
|
72
|
+
};
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
const omit = require('lodash/omit');
|
|
2
|
+
const logger = require('../../libs/logger');
|
|
3
|
+
|
|
4
|
+
const action = 'request-profile';
|
|
5
|
+
module.exports = {
|
|
6
|
+
action,
|
|
7
|
+
claims: {
|
|
8
|
+
profile: () => ({
|
|
9
|
+
description: 'Please provide your full profile',
|
|
10
|
+
fields: ['fullName', 'email', 'phone', 'signature', 'avatar', 'birthday'],
|
|
11
|
+
}),
|
|
12
|
+
},
|
|
13
|
+
|
|
14
|
+
onAuth: ({ userDid, userPk, claims, updateSession }) => {
|
|
15
|
+
const claim = claims.find((x) => x.type === 'profile');
|
|
16
|
+
logger.log(`${action}.onAuth`, { userDid, userPk, claim });
|
|
17
|
+
updateSession({
|
|
18
|
+
result: {
|
|
19
|
+
...omit(claim, ['type', 'signature']),
|
|
20
|
+
did: userDid,
|
|
21
|
+
pk: userPk,
|
|
22
|
+
},
|
|
23
|
+
});
|
|
24
|
+
},
|
|
25
|
+
};
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
/* eslint-disable no-console */
|
|
2
|
+
const { toTypeInfo } = require('@arcblock/did');
|
|
3
|
+
const { fromPublicKey } = require('@ocap/wallet');
|
|
4
|
+
const { fromBase58 } = require('@ocap/util');
|
|
5
|
+
|
|
6
|
+
const logger = require('../../libs/logger');
|
|
7
|
+
const { getRandomMessage } = require('../../libs/utils');
|
|
8
|
+
|
|
9
|
+
const action = 'request-text-signature';
|
|
10
|
+
module.exports = {
|
|
11
|
+
action,
|
|
12
|
+
claims: {
|
|
13
|
+
signature: () => {
|
|
14
|
+
const data = getRandomMessage();
|
|
15
|
+
|
|
16
|
+
return {
|
|
17
|
+
description: 'Please sign the text',
|
|
18
|
+
type: 'mime:text/plain',
|
|
19
|
+
data,
|
|
20
|
+
};
|
|
21
|
+
},
|
|
22
|
+
},
|
|
23
|
+
|
|
24
|
+
onAuth: ({ userDid, userPk, claims, updateSession }) => {
|
|
25
|
+
const type = toTypeInfo(userDid);
|
|
26
|
+
const user = fromPublicKey(userPk, type);
|
|
27
|
+
const claim = claims.find((x) => x.type === 'signature');
|
|
28
|
+
|
|
29
|
+
logger.info(`${action}.onAuth`, { userPk, userDid, claim });
|
|
30
|
+
|
|
31
|
+
if (claim.origin) {
|
|
32
|
+
if (user.verify(claim.origin, claim.sig, claim.method !== 'none') === false) {
|
|
33
|
+
throw new Error('Invalid origin signature');
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
updateSession({
|
|
38
|
+
result: {
|
|
39
|
+
origin: fromBase58(claim.origin).toString(),
|
|
40
|
+
sig: claim.sig,
|
|
41
|
+
},
|
|
42
|
+
});
|
|
43
|
+
},
|
|
44
|
+
};
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
/* eslint-disable no-console */
|
|
2
|
+
const { toTypeInfo } = require('@arcblock/did');
|
|
3
|
+
const { fromPublicKey } = require('@ocap/wallet');
|
|
4
|
+
const { toTxHash } = require('@ocap/mcrypto');
|
|
5
|
+
const { toBase58 } = require('@ocap/util');
|
|
6
|
+
|
|
7
|
+
const logger = require('../../libs/logger');
|
|
8
|
+
const env = require('../../libs/env');
|
|
9
|
+
const { wallet, client } = require('../../libs/auth');
|
|
10
|
+
|
|
11
|
+
const action = 'request-transaction-signature';
|
|
12
|
+
|
|
13
|
+
module.exports = {
|
|
14
|
+
action,
|
|
15
|
+
claims: {
|
|
16
|
+
signature: async ({ userPk, userDid }) => {
|
|
17
|
+
const value = await client.fromTokenToUnit(1);
|
|
18
|
+
const type = toTypeInfo(userDid);
|
|
19
|
+
|
|
20
|
+
const encoded = await client.encodeTransferV2Tx({
|
|
21
|
+
tx: {
|
|
22
|
+
itx: {
|
|
23
|
+
to: wallet.address,
|
|
24
|
+
tokens: [
|
|
25
|
+
{
|
|
26
|
+
address: env.localTokenId,
|
|
27
|
+
value: value.toString(),
|
|
28
|
+
},
|
|
29
|
+
],
|
|
30
|
+
},
|
|
31
|
+
},
|
|
32
|
+
wallet: fromPublicKey(userPk, type),
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
const origin = toBase58(encoded.buffer);
|
|
36
|
+
|
|
37
|
+
return {
|
|
38
|
+
description: 'Please sign the transaction',
|
|
39
|
+
type: 'fg:t:transaction',
|
|
40
|
+
data: origin,
|
|
41
|
+
};
|
|
42
|
+
},
|
|
43
|
+
},
|
|
44
|
+
|
|
45
|
+
onAuth: ({ userDid, userPk, claims, updateSession }) => {
|
|
46
|
+
const claim = claims.find((x) => x.type === 'signature');
|
|
47
|
+
const tx = client.decodeTx(claim.origin);
|
|
48
|
+
|
|
49
|
+
logger.info(`${action}.onAuth`, { userPk, userDid, claim });
|
|
50
|
+
const buffer = Buffer.from(claim.origin);
|
|
51
|
+
const hash = toTxHash(buffer);
|
|
52
|
+
|
|
53
|
+
updateSession({
|
|
54
|
+
result: {
|
|
55
|
+
hash,
|
|
56
|
+
tx,
|
|
57
|
+
origin: claim.origin,
|
|
58
|
+
sig: claim.sig,
|
|
59
|
+
},
|
|
60
|
+
});
|
|
61
|
+
},
|
|
62
|
+
};
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
title: DID Connect Playground
|
|
2
|
+
description: A Blocklet show DID Connect Playground
|
|
3
|
+
keywords:
|
|
4
|
+
- blocklet
|
|
5
|
+
- react
|
|
6
|
+
- did-connect
|
|
7
|
+
group: dapp
|
|
8
|
+
did: ''
|
|
9
|
+
main: api/index.js
|
|
10
|
+
author:
|
|
11
|
+
name: ArcBlock
|
|
12
|
+
email: blocklet@arcblock.io
|
|
13
|
+
url: https://github.com/blocklet
|
|
14
|
+
repository:
|
|
15
|
+
type: git
|
|
16
|
+
url: git+https://github.com/blocklet/create-blocklet.git
|
|
17
|
+
specVersion: 1.2.8
|
|
18
|
+
version: 0.1.0
|
|
19
|
+
logo: logo.png
|
|
20
|
+
files:
|
|
21
|
+
- dist
|
|
22
|
+
- logo.png
|
|
23
|
+
- screenshots
|
|
24
|
+
- api/hooks/pre-start.js
|
|
25
|
+
interfaces:
|
|
26
|
+
- type: web
|
|
27
|
+
name: publicUrl
|
|
28
|
+
path: /
|
|
29
|
+
prefix: '*'
|
|
30
|
+
port: BLOCKLET_PORT
|
|
31
|
+
protocol: http
|
|
32
|
+
community: ''
|
|
33
|
+
documentation: ''
|
|
34
|
+
homepage: ''
|
|
35
|
+
license: ''
|
|
36
|
+
payment:
|
|
37
|
+
price: []
|
|
38
|
+
share: []
|
|
39
|
+
timeout:
|
|
40
|
+
start: 60
|
|
41
|
+
requirements:
|
|
42
|
+
server: '>=1.16.31'
|
|
43
|
+
os: '*'
|
|
44
|
+
cpu: '*'
|
|
45
|
+
scripts:
|
|
46
|
+
preStart: node api/hooks/pre-start.js
|
|
47
|
+
dev: npm run start
|
|
48
|
+
environments:
|
|
49
|
+
- name: CHAIN_HOST
|
|
50
|
+
description: What's endpoint of the chain?
|
|
51
|
+
required: true
|
|
52
|
+
default: https://beta.abtnetwork.io/api/
|
|
53
|
+
secure: false
|
|
54
|
+
capabilities:
|
|
55
|
+
navigation: true
|
|
56
|
+
screenshots: []
|
|
57
|
+
components: []
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
<!DOCTYPE html>
|
|
2
|
+
<html lang="en">
|
|
3
|
+
|
|
4
|
+
<head>
|
|
5
|
+
<meta charset="UTF-8" />
|
|
6
|
+
<link rel="icon" href="/favicon.ico?imageFilter=convert&f=png&w=32" />
|
|
7
|
+
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0" />
|
|
8
|
+
<meta name="theme-color" content="#4F6AF5" />
|
|
9
|
+
</head>
|
|
10
|
+
|
|
11
|
+
<body>
|
|
12
|
+
<noscript> You need to enable JavaScript to run this app. </noscript>
|
|
13
|
+
<div id="app"></div>
|
|
14
|
+
<script type="module" src="/src/index.jsx"></script>
|
|
15
|
+
</body>
|
|
16
|
+
|
|
17
|
+
</html>
|