Skip to content

Commit 6f85079

Browse files
committed
feat(support import privateKey): support import account from DApp by SDK with privateKey
1 parent 38e9daf commit 6f85079

File tree

4 files changed

+44
-10
lines changed

4 files changed

+44
-10
lines changed

example/basic-dapp/index.html

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@
128128
导入地址
129129
</header>
130130
<form>
131-
<label>输入地址用英文逗号风格
131+
<label>输入地址用英文逗号分隔
132132
<input type="text" id=
133133
"import_address_input" placeholder="地址">
134134
</label>
@@ -140,6 +140,24 @@
140140
Result: <span id="import_address_result">N/A</span>
141141
</footer>
142142
</article>
143+
144+
<article>
145+
<header>
146+
导入私钥
147+
</header>
148+
<form>
149+
<label>输入私钥用英文逗号风格分隔
150+
<input type="text" id=
151+
"import_private_key_input" placeholder="地址">
152+
</label>
153+
</form>
154+
<button id="import_private_key_button" disabled=
155+
"true">导入
156+
</button>
157+
<footer>
158+
Result: <span id="import_private_key_result">N/A</span>
159+
</footer>
160+
</article>
143161
</div>
144162

145163
<script>

example/basic-dapp/index.js

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -480,12 +480,14 @@ async function walletInitialized() {
480480
const transferFromButton = getElement('transfer_from')
481481
const getCFXButton = getElement('get-cfx')
482482
const importAddressButton = getElement('import_address_button')
483+
const importPrivateKeyButton = getElement('import_private_key_button')
483484
const nativeReceiverAddressInput = getElement('native-receiver')
484485
const countInput = getElement('native-count')
485486
const approveAccountInput = getElement('approve-account')
486487
const transferFromAccountInput = getElement('from-account')
487488
const transferToAccountInput = getElement('to-account')
488489
const importAddressInput = getElement('import_address_input')
490+
const importPrivateKeyInput = getElement('import_private_key_input')
489491

490492
const deployContract = getElement('deploy_contract')
491493

@@ -502,6 +504,7 @@ async function walletInitialized() {
502504
getCFXButton.disabled = false
503505
connectButton.disabled = true
504506
importAddressButton.disabled = false
507+
importPrivateKeyButton.disabled = false
505508
}
506509

507510
function unAuthed() {
@@ -514,6 +517,7 @@ async function walletInitialized() {
514517
deployContract.disabled = true
515518
connectButton.disabled = false
516519
importAddressButton.disabled = true
520+
importPrivateKeyButton.disabled = true
517521
}
518522

519523
provider.on('accountsChanged', (accounts) => {
@@ -687,15 +691,11 @@ async function walletInitialized() {
687691

688692
importAddressButton.onclick = async () => {
689693
try {
690-
const [connectedAddress] = await provider.request({
691-
method: 'cfx_accounts',
692-
})
693-
694694
const tx = {
695-
address: importAddressInput.value.split(','),
695+
address: importAddressInput.value.replace(/\s+/g, '').split(','),
696696
}
697697
provider
698-
.request({ method: 'anyweb_importAddress', params: [tx] })
698+
.request({ method: 'anyweb_importAccount', params: [tx] })
699699
.then((result) => {
700700
getElement('import_address_result').innerHTML = result
701701
console.log('result', result)
@@ -704,6 +704,22 @@ async function walletInitialized() {
704704
console.log('err', err)
705705
}
706706
}
707+
708+
importPrivateKeyButton.onclick = async () => {
709+
try {
710+
const tx = {
711+
privateKey: importPrivateKeyInput.value.replace(/\s+/g, '').split(','),
712+
}
713+
provider
714+
.request({ method: 'anyweb_importAccount', params: [tx] })
715+
.then((result) => {
716+
getElement('import_private_key_result').innerHTML = result
717+
console.log('result', result)
718+
})
719+
} catch (err) {
720+
console.log('err', err)
721+
}
722+
}
707723
}
708724

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

src/interface/provider.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,6 @@ export interface IIframeOptions {
8282
| 'createContract'
8383
| 'callContract'
8484
| 'createTransaction'
85-
| 'importAddress'
85+
| 'importAccount'
8686
waitResult?: boolean
8787
}

src/provider.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -183,13 +183,13 @@ export class Provider implements IProvider {
183183
console.error('Error to sendTransaction', e)
184184
return 'fail'
185185
}
186-
case 'anyweb_importAddress':
186+
case 'anyweb_importAccount':
187187
try {
188188
return await callIframe('pages/dapp/auth', {
189189
appId: this.appId,
190190
chainId: (await this.request({ method: 'cfx_chainId' })) as string,
191191
params: params ? JSON.stringify(paramsObj) : JSON.stringify([]),
192-
authType: 'importAddress',
192+
authType: 'importAccount',
193193
})
194194
} catch (e) {
195195
console.error('Error to import Address', e)

0 commit comments

Comments
 (0)