-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add delegation signing cases (#243)
* feat: add delegation signing cases * v0.36.60
- Loading branch information
1 parent
9f2822d
commit a16b094
Showing
8 changed files
with
409 additions
and
210 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }; | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,7 +10,7 @@ files: | |
- blocklet.md | ||
- README.md | ||
- build | ||
version: 0.36.59 | ||
version: 0.36.60 | ||
author: | ||
name: ArcBlock | ||
email: [email protected] | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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", | ||
|
@@ -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", | ||
|
@@ -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" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
0.36.59 | ||
0.36.60 |
Oops, something went wrong.