Skip to content

Commit 7562c1f

Browse files
committed
feat(support import read-only account): support import account from DApp by SDK without key
1 parent 2f91490 commit 7562c1f

File tree

4 files changed

+65
-8
lines changed

4 files changed

+65
-8
lines changed

example/basic-dapp/index.html

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,23 @@
123123
Result: <span id="deploy_contract_result">N/A</span>
124124
</footer>
125125
</article>
126+
<article>
127+
<header>
128+
导入地址
129+
</header>
130+
<form>
131+
<label>输入地址用英文逗号风格
132+
<input type="text" id=
133+
"import_address_input" placeholder="地址">
134+
</label>
135+
</form>
136+
<button id="import_address_button" disabled=
137+
"true">导入
138+
</button>
139+
<footer>
140+
Result: <span id="import_address_result">N/A</span>
141+
</footer>
142+
</article>
126143
</div>
127144

128145
<script>

example/basic-dapp/index.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -479,11 +479,13 @@ async function walletInitialized() {
479479
const approveButton = getElement('approve')
480480
const transferFromButton = getElement('transfer_from')
481481
const getCFXButton = getElement('get-cfx')
482+
const importAddressButton = getElement('import_address_button')
482483
const nativeReceiverAddressInput = getElement('native-receiver')
483484
const countInput = getElement('native-count')
484485
const approveAccountInput = getElement('approve-account')
485486
const transferFromAccountInput = getElement('from-account')
486487
const transferToAccountInput = getElement('to-account')
488+
const importAddressInput = getElement('import_address_input')
487489

488490
const deployContract = getElement('deploy_contract')
489491

@@ -499,6 +501,7 @@ async function walletInitialized() {
499501
deployContract.disabled = false
500502
getCFXButton.disabled = false
501503
connectButton.disabled = true
504+
importAddressButton.disabled = false
502505
}
503506

504507
function unAuthed() {
@@ -510,6 +513,7 @@ async function walletInitialized() {
510513
getCFXButton.disabled = true
511514
deployContract.disabled = true
512515
connectButton.disabled = false
516+
importAddressButton.disabled = true
513517
}
514518

515519
provider.on('accountsChanged', (accounts) => {
@@ -680,6 +684,26 @@ async function walletInitialized() {
680684
console.log('err', err)
681685
}
682686
}
687+
688+
importAddressButton.onclick = async () => {
689+
try {
690+
const [connectedAddress] = await provider.request({
691+
method: 'cfx_accounts',
692+
})
693+
694+
const tx = {
695+
address: importAddressInput.value.split(','),
696+
}
697+
provider
698+
.request({ method: 'anyweb_importAddress', params: [tx] })
699+
.then((result) => {
700+
getElement('import_address_result').innerHTML = result
701+
console.log('result', result)
702+
})
703+
} catch (err) {
704+
console.log('err', err)
705+
}
706+
}
683707
}
684708

685709
window.addEventListener('load', async () => {

src/interface/provider.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,11 @@ export interface IIframeOptions {
7777
params: string
7878
chainId: string
7979
scope?: number[]
80-
authType?: 'account' | 'createContract' | 'callContract' | 'createTransaction'
80+
authType?:
81+
| 'account'
82+
| 'createContract'
83+
| 'callContract'
84+
| 'createTransaction'
85+
| 'importAddress'
8186
waitResult?: boolean
8287
}

src/provider.ts

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@ import {
88
IBaseProviderOptions,
99
IProvider,
1010
IProviderConnectInfo,
11+
IProviderMessage,
1112
IProviderRpcError,
1213
IRequestArguments,
13-
IProviderMessage,
1414
} from './interface/provider'
1515
import { callIframe, readCache, setCache } from './utils/common'
1616
import config from '../package.json'
@@ -130,6 +130,11 @@ export class Provider implements IProvider {
130130
method: string,
131131
params?: readonly unknown[] | Record<string, unknown>
132132
): Promise<unknown> {
133+
const paramsObj = params
134+
? Array.isArray(params) && params.length > 0
135+
? params[0]
136+
: params
137+
: {}
133138
switch (method) {
134139
case 'cfx_netVersion':
135140
if (this.networkId === -1) {
@@ -162,11 +167,6 @@ export class Provider implements IProvider {
162167
this.events.onNetworkChanged(String(result.networkId))
163168
return this.address
164169
case 'cfx_sendTransaction':
165-
const paramsObj = params
166-
? Array.isArray(params) && params.length > 0
167-
? params[0]
168-
: params
169-
: {}
170170
try {
171171
return await callIframe('pages/dapp/auth', {
172172
appId: this.appId,
@@ -183,7 +183,18 @@ export class Provider implements IProvider {
183183
console.error('Error to sendTransaction', e)
184184
return 'fail'
185185
}
186-
186+
case 'anyweb_importAddress':
187+
try {
188+
return await callIframe('pages/dapp/auth', {
189+
appId: this.appId,
190+
chainId: (await this.request({ method: 'cfx_chainId' })) as string,
191+
params: params ? JSON.stringify(paramsObj) : JSON.stringify([]),
192+
authType: 'importAddress',
193+
})
194+
} catch (e) {
195+
console.error('Error to import Address', e)
196+
return 'fail'
197+
}
187198
case 'anyweb_version':
188199
return config.version
189200
case 'anyweb_home':

0 commit comments

Comments
 (0)