Skip to content

Commit f11f05d

Browse files
committed
feat(add sign function): add sign function
1 parent 9f2db12 commit f11f05d

File tree

9 files changed

+156
-8
lines changed

9 files changed

+156
-8
lines changed

dist/anyweb-js-sdk.umd.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/anyweb-js-sdk.umd.min.js.map

Lines changed: 4 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "@idealight-labs/anyweb-js-sdk",
33
"description": "AnyWeb JavaScript Software Development Kit",
4-
"version": "1.2.2",
4+
"version": "1.2.3",
55
"license": "LGPL-3.0",
66
"author": "common@idealight.ltd",
77
"repository": {

dist/src/interface/provider.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ export interface IIframeOptions {
6161
params: string;
6262
chainId: number;
6363
scopes?: string[];
64-
authType?: 'account' | 'createContract' | 'callContract' | 'createTransaction' | 'importAccount' | 'exit_accounts' | 'logout' | 'check_auth' | 'check_identify' | 'identify' | 'check_login';
64+
authType?: 'account' | 'createContract' | 'callContract' | 'createTransaction' | 'importAccount' | 'exit_accounts' | 'logout' | 'check_auth' | 'check_identify' | 'identify' | 'check_login' | 'signTypedData';
6565
waitResult?: boolean;
6666
silence?: boolean;
6767
}

dist/src/provider.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,22 @@ class Provider {
205205
else {
206206
return false;
207207
}
208+
case 'cfx_signTypedData':
209+
const from = params[0];
210+
const data = params[1];
211+
const isRsv = !!params[2];
212+
return yield (0, common_1.callIframe)('pages/dapp/auth', {
213+
appId: this.appId,
214+
chainId: this.chainId,
215+
params: params
216+
? JSON.stringify({
217+
from,
218+
data,
219+
isRsv,
220+
})
221+
: '',
222+
authType: 'signTypedData',
223+
}, this);
208224
case 'cfx_sendTransaction':
209225
let authType;
210226
const payload = params[0];

example/basic-dapp/index.html

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,20 @@
109109
</footer>
110110
</article>
111111

112+
<article>
113+
<header>
114+
获取签名
115+
</header>
116+
<form>
117+
<label>test</label>
118+
</form>
119+
<button id="sign_button" disabled="true">Gateway Test
120+
</button>
121+
<footer>
122+
Result: <span id="sign_result">N/A</span>
123+
</footer>
124+
</article>
125+
112126
<article>
113127
<header>
114128
Approve (cUSDT)
@@ -192,8 +206,10 @@
192206
</div>
193207

194208
<script>
195-
const now = new Date()
196-
document.write(`<script type='text/javascript' src='../../dist/anyweb-js-sdk.umd.min.js?v=${now.getFullYear()}${now.getMonth() + 1}${now.getDate()}'><\/script>`);
209+
const now = new Date();
210+
document.write(
211+
`<script type="text/javascript" src="../../dist/anyweb-js-sdk.umd.min.js?v=${now.getFullYear()}${now.getMonth() +
212+
1}${now.getDate()}"><\/script>`);
197213
</script>
198214

199215
<script type="module">

example/basic-dapp/index.js

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -516,6 +516,71 @@ const abi = [
516516
},
517517
]
518518

519+
const msgParams = {
520+
types: {
521+
CIP23Domain: [
522+
{
523+
name: 'name',
524+
type: 'string',
525+
},
526+
{
527+
name: 'version',
528+
type: 'string',
529+
},
530+
{
531+
name: 'chainId',
532+
type: 'uint256',
533+
},
534+
{
535+
name: 'verifyingContract',
536+
type: 'address',
537+
},
538+
],
539+
Person: [
540+
{
541+
name: 'name',
542+
type: 'string',
543+
},
544+
{
545+
name: 'wallet',
546+
type: 'address',
547+
},
548+
],
549+
Mail: [
550+
{
551+
name: 'from',
552+
type: 'Person',
553+
},
554+
{
555+
name: 'to',
556+
type: 'Person',
557+
},
558+
{
559+
name: 'contents',
560+
type: 'string',
561+
},
562+
],
563+
},
564+
primaryType: 'Mail',
565+
domain: {
566+
name: 'Ether Mail',
567+
version: '1',
568+
chainId: 1,
569+
verifyingContract: '0xCcCCccccCCCCcCCCCCCcCcCccCcCCCcCcccccccC',
570+
},
571+
message: {
572+
from: {
573+
name: 'Cow',
574+
wallet: '0xCD2a3d9F938E13CD947Ec05AbC7FE734Df8DD826',
575+
},
576+
to: {
577+
name: 'Bob',
578+
wallet: '0xbBbBBBBbbBBBbbbBbbBbbbbBBbBbbbbBbBbbBBbB',
579+
},
580+
contents: 'Hello, Bob!',
581+
},
582+
}
583+
519584
// 初始化钱包
520585
const provider = new window.AnyWeb.Provider({
521586
appId: '693b6401-135a-4dc3-846b-1c05ad2572f6',
@@ -586,6 +651,7 @@ async function walletInitialized() {
586651
const importAddressNameInput = getElement('import_address_name_input')
587652
const identifyButton = getElement('identify_button')
588653
const checkLoginButton = getElement('checklogin_button')
654+
const signButton = getElement('sign_button')
589655

590656
const deployContract = getElement('deploy_contract')
591657

@@ -609,6 +675,7 @@ async function walletInitialized() {
609675
logoutButton.disabled = false
610676
importAddressButton.disabled = false
611677
identifyButton.disabled = false
678+
signButton.disabled = false
612679
}
613680

614681
function unAuthed() {
@@ -626,6 +693,7 @@ async function walletInitialized() {
626693
logoutButton.disabled = true
627694
importAddressButton.disabled = true
628695
identifyButton.disabled = true
696+
signButton.disabled = true
629697
}
630698

631699
try {
@@ -818,6 +886,32 @@ async function walletInitialized() {
818886
}
819887
}
820888

889+
signButton.onclick = async () => {
890+
try {
891+
const from = address[0]
892+
console.log('connectedAddress', from)
893+
provider
894+
.request({
895+
method: 'cfx_signTypedData',
896+
params: [from, JSON.stringify(msgParams)],
897+
})
898+
.then((result) => {
899+
getElement('sign_result').innerHTML = JSON.stringify(result)
900+
console.log('result', result)
901+
})
902+
.catch((e) => {
903+
getElement('gateway_test_result').innerHTML = `
904+
Code: ${e.code},\n
905+
Message: ${e.message},\n
906+
Data: ${e.data}\n
907+
`
908+
console.error('签名失败', { message: e.message, data: e.data })
909+
})
910+
} catch (err) {
911+
console.log('err', err)
912+
}
913+
}
914+
821915
// approve spender
822916
approveButton.onclick = async () => {
823917
try {

src/interface/provider.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ export interface IIframeOptions {
8383
| 'check_identify'
8484
| 'identify'
8585
| 'check_login'
86+
| 'signTypedData'
8687
waitResult?: boolean
8788
silence?: boolean
8889
}

src/provider.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,26 @@ export class Provider implements IProvider {
248248
} else {
249249
return false
250250
}
251+
case 'cfx_signTypedData':
252+
const from = params[0]
253+
const data = params[1]
254+
const isRsv = !!params[2]
255+
return await callIframe(
256+
'pages/dapp/auth',
257+
{
258+
appId: this.appId,
259+
chainId: this.chainId,
260+
params: params
261+
? JSON.stringify({
262+
from,
263+
data,
264+
isRsv,
265+
})
266+
: '',
267+
authType: 'signTypedData',
268+
},
269+
this
270+
)
251271
case 'cfx_sendTransaction':
252272
let authType: IIframeOptions['authType']
253273
const payload = params[0]

0 commit comments

Comments
 (0)