Skip to content

Commit

Permalink
feat: add delegation signing cases (#243)
Browse files Browse the repository at this point in the history
* feat: add delegation signing cases

* v0.36.60
  • Loading branch information
wangshijun authored Nov 30, 2023
1 parent 9f2822d commit a16b094
Show file tree
Hide file tree
Showing 8 changed files with 409 additions and 210 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.60 (November 30, 2023)

- feat: add delegation signing cases

## 0.36.59 (November 13, 2023)

- 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 @@ -79,6 +79,7 @@ walletHandlers.attach(Object.assign({ app: router }, require('../routes/auth/iss
walletHandlers.attach(Object.assign({ app: router }, require('../routes/auth/issue-badge-asset')));
walletHandlers.attach(Object.assign({ app: router }, require('../routes/auth/extra-params')));
walletHandlers.attach(Object.assign({ app: router }, require('../routes/auth/delegate')));
walletHandlers.attach(Object.assign({ app: router }, require('../routes/auth/delegation-limit')));
walletHandlers.attach(Object.assign({ app: router }, require('../routes/auth/launch-node')));
walletHandlers.attach(Object.assign({ app: router }, require('../routes/auth/verify-vc')));
walletHandlers.attach(Object.assign({ app: router }, require('../routes/auth/verify-passport')));
Expand Down
108 changes: 108 additions & 0 deletions api/routes/auth/delegation-limit.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
/* eslint-disable no-console */
const { fromPublicKey } = require('@ocap/wallet');
const { toTypeInfo } = require('@arcblock/did');
const { fromTokenToUnit } = require('@ocap/util');

const { toDelegateAddress } = require('@arcblock/did-util');
const env = require('../../libs/env');
const { wallet, client } = require('../../libs/auth');
const { pickGasPayerHeaders } = require('../../libs/util');

module.exports = {
action: 'delegation-limit',
claims: {
signature: async ({ userPk, userDid, extraParams: { type = 1, token = 0, asset = 0 } }) => {
const limit = {
tokens: [],
assets: [],
};

const now = Math.round(Date.now() / 1000);
const typeCount = +type;
const tokenCount = +token;
const assetCount = +asset;

const tokenLimit1 = {
address: env.localTokenId,
txAllowance: fromTokenToUnit(5).toString(),
totalAllowance: fromTokenToUnit(100).toString(),
txCount: 20,
to: [wallet.address],
validUntil: now + 60 * 60 * 24 * 30,
};

const tokenLimit2 = {
address: env.localTokenId,
txAllowance: fromTokenToUnit(5).toString(),
totalAllowance: fromTokenToUnit(100).toString(),
txCount: 20,
to: [wallet.address],
rate: {
interval: 60,
},
};

if (tokenCount === 1) {
limit.tokens.push(tokenLimit1);
}
if (tokenCount === 2) {
limit.tokens.push(tokenLimit1, tokenLimit2);
}
if (assetCount > 0) {
const { assets } = await client.listAssets({
ownerAddress: userDid,
paging: { size: 10 },
});
limit.assets.push({
address: assets.map(x => x.address),
txCount: 5,
to: [wallet.address],
validUntil: now + 60 * 60 * 24 * 30,
});
}

const ops = [];
if (typeCount === 1) {
ops.push({ typeUrl: 'fg:t:transfer_v2', limit });
}
if (typeCount === 2) {
ops.push({ typeUrl: 'fg:t:transfer_v2', limit });
ops.push({ typeUrl: 'fg:t:exchange_v2', limit });
}

return {
type: 'DelegateTx',
description: 'Sign the delegation to allow this app to do tx',
wallet: fromPublicKey(userPk, toTypeInfo(userDid)),
data: {
itx: {
address: toDelegateAddress(userDid, wallet.address),
to: wallet.address,
ops,
data: {
type: 'json',
value: {
salt: Math.random(),
},
},
},
},
};
},
},

onAuth: async ({ request, userDid, userPk, claims }) => {
const claim = claims.find(x => x.type === 'signature');

// execute the delegate tx
const tx = client.decodeTx(claim.origin);
tx.signature = claim.sig;

const hash = await client.sendDelegateTx(
{ tx, wallet: fromPublicKey(userPk, toTypeInfo(userDid)) },
pickGasPayerHeaders(request)
);

return { hash };
},
};
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.59
version: 0.36.60
author:
name: ArcBlock
email: [email protected]
Expand Down
72 changes: 36 additions & 36 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.59",
"version": "0.36.60",
"author": "wangshijun <[email protected]> https://github.com/wangshijun",
"keywords": [
"dapp",
Expand All @@ -28,32 +28,32 @@
"email": "[email protected]"
},
"dependencies": {
"@arcblock/did": "^1.18.95",
"@arcblock/did-auth": "^1.18.95",
"@arcblock/did": "^1.18.101",
"@arcblock/did-auth": "^1.18.101",
"@arcblock/did-auth-storage-nedb": "^1.7.1",
"@arcblock/did-connect": "2.8.12",
"@arcblock/did-playground": "2.8.12",
"@arcblock/did-util": "^1.18.95",
"@arcblock/jwt": "^1.18.95",
"@arcblock/nft": "^1.18.95",
"@arcblock/nft-template": "^1.18.95",
"@arcblock/react-hooks": "2.8.12",
"@arcblock/ux": "^2.8.12",
"@arcblock/vc": "^1.18.95",
"@arcblock/did-connect": "2.8.17",
"@arcblock/did-playground": "2.8.17",
"@arcblock/did-util": "^1.18.101",
"@arcblock/jwt": "^1.18.101",
"@arcblock/nft": "^1.18.101",
"@arcblock/nft-template": "^1.18.101",
"@arcblock/react-hooks": "2.8.17",
"@arcblock/ux": "^2.8.17",
"@arcblock/vc": "^1.18.101",
"@blocklet/sdk": "1.16.18",
"@blocklet/ui-react": "2.8.12",
"@blocklet/ui-react": "2.8.17",
"@emotion/react": "^11.10.5",
"@emotion/styled": "^11.10.5",
"@mui/icons-material": "^5.10.2",
"@mui/material": "^5.10.2",
"@ocap/asset": "^1.18.95",
"@ocap/client": "^1.18.95",
"@ocap/mcrypto": "^1.18.95",
"@ocap/message": "^1.18.95",
"@ocap/proto": "^1.18.95",
"@ocap/tx-util": "^1.18.95",
"@ocap/util": "^1.18.95",
"@ocap/wallet": "^1.18.95",
"@ocap/asset": "^1.18.101",
"@ocap/client": "^1.18.101",
"@ocap/mcrypto": "^1.18.101",
"@ocap/message": "^1.18.101",
"@ocap/proto": "^1.18.101",
"@ocap/tx-util": "^1.18.101",
"@ocap/util": "^1.18.101",
"@ocap/wallet": "^1.18.101",
"ahooks": "^3.7.8",
"axios": "^0.21.4",
"batch-promises": "^0.0.3",
Expand Down Expand Up @@ -161,21 +161,21 @@
},
"resolutions": {
"react-error-overlay": "6.0.9",
"@arcblock/did": "1.18.95",
"@arcblock/did-auth": "1.18.95",
"@arcblock/did-util": "1.18.95",
"@arcblock/jwt": "1.18.95",
"@arcblock/nft": "1.18.95",
"@arcblock/nft-template": "1.18.95",
"@arcblock/vc": "1.18.95",
"@ocap/asset": "1.18.95",
"@ocap/client": "1.18.95",
"@ocap/mcrypto": "1.18.95",
"@ocap/message": "1.18.95",
"@ocap/proto": "1.18.95",
"@ocap/tx-util": "1.18.95",
"@ocap/util": "1.18.95",
"@ocap/wallet": "1.18.95"
"@arcblock/did": "1.18.101",
"@arcblock/did-auth": "1.18.101",
"@arcblock/did-util": "1.18.101",
"@arcblock/jwt": "1.18.101",
"@arcblock/nft": "1.18.101",
"@arcblock/nft-template": "1.18.101",
"@arcblock/vc": "1.18.101",
"@ocap/asset": "1.18.101",
"@ocap/client": "1.18.101",
"@ocap/mcrypto": "1.18.101",
"@ocap/message": "1.18.101",
"@ocap/proto": "1.18.101",
"@ocap/tx-util": "1.18.101",
"@ocap/util": "1.18.101",
"@ocap/wallet": "1.18.101"
},
"license": "Apache-2.0"
}
59 changes: 57 additions & 2 deletions src/pages/full.js
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,61 @@ export default function IndexPage() {
success: 'Exchange successful',
}}
/>
<AuthButton
button="Grant Permission (token x1)"
action="delegation-limit"
extraParams={{ type: 1, token: 1, asset: 0 }}
messages={{
title: 'Grant token permission',
scan: 'Connect your DID Wallet to complete the delegation',
confirm: 'Confirm on your DID Wallet',
success: 'Delegate successful',
}}
/>
<AuthButton
button="Grant Permission (token x2)"
action="delegation-limit"
extraParams={{ type: 1, token: 2, asset: 0 }}
messages={{
title: 'Grant token permission',
scan: 'Connect your DID Wallet to complete the delegation',
confirm: 'Confirm on your DID Wallet',
success: 'Delegate successful',
}}
/>
<AuthButton
button="Grant Permission (assets)"
action="delegation-limit"
extraParams={{ type: 1, token: 0, asset: 1 }}
messages={{
title: 'Grant asset permission',
scan: 'Connect your DID Wallet to complete the delegation',
confirm: 'Confirm on your DID Wallet',
success: 'Delegate successful',
}}
/>
<AuthButton
button="Grant Permission (asset + token)"
action="delegation-limit"
extraParams={{ type: 1, token: 1, asset: 1 }}
messages={{
title: 'Grant asset permission',
scan: 'Connect your DID Wallet to complete the delegation',
confirm: 'Confirm on your DID Wallet',
success: 'Delegate successful',
}}
/>
<AuthButton
button="Grant Permission (type x2)"
action="delegation-limit"
extraParams={{ type: 2, token: 1, asset: 0 }}
messages={{
title: 'Grant both permission',
scan: 'Connect your DID Wallet to complete the delegation',
confirm: 'Confirm on your DID Wallet',
success: 'Delegate successful',
}}
/>
</div>
</section>

Expand All @@ -360,12 +415,12 @@ export default function IndexPage() {
<SignButton {...session} type="html" />
<SignButton {...session} type="skip_hash" />
<AuthButton
button="Sign Delegation"
button="Sign Offline Delegation"
action="sign-delegation"
saveConnect={false}
extraParams={{ sessionDid: session.user.did }}
messages={{
title: 'Sign Delegation',
title: 'Sign Offline Delegation',
scan: 'Connect your DID Wallet to sign the delegation',
confirm: 'Confirm on your DID Wallet',
success: 'Delegation Signed',
Expand Down
2 changes: 1 addition & 1 deletion version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.36.59
0.36.60
Loading

0 comments on commit a16b094

Please sign in to comment.