Skip to content

Commit 8495d5d

Browse files
authored
Merge pull request #55 from dotbitHQ/feat/subaccount-check
feat: add verifyAddrsByAccount method on DotBit
2 parents 11c8134 + 66b16d6 commit 8495d5d

File tree

4 files changed

+119
-0
lines changed

4 files changed

+119
-0
lines changed

docs/api/dotbit.md

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
- [dweb(account)](#dwebaccount)
2626
- [profiles(account, key)](#profilesaccount-key)
2727
- [avatar(account)](#avataraccount)
28+
- [verifyAddrsByAccount(address, account, subAccount, verifyType)](#verifyaddrsbyaccountaddress-account-subaccount-verifytype)
2829

2930
## constructor(config)
3031
To create a new DotBit instance.
@@ -720,3 +721,36 @@ Get the avatar of a given account.
720721
- account: `string`
721722

722723
For details, please refer to the documentation of [@dotbit/plugin-avatar](https://github.com/mozwell/dotbit.js/blob/main/packages/plugin-avatar/README.md)
724+
725+
## verifyAddrsByAccount(address, account, subAccount, verifyType)
726+
Check if the given address has the specified SubDID under the account.
727+
### Parameters
728+
- address: `string` Address to be verified.
729+
- account: `string` Specified .bit account.
730+
- (Optional)subAccount: `string` Specified SubDID. `subAccount` is an optional field. If subAccount is empty, it will default to checking if the given address has at least one SubDID under any specified account.
731+
- (Optional)verifyType: `number` The `verifyType` is an optional field with a default value of 0, where 0 represents that the `address` is the owner address of the SubDID, and 1 represents that the `address` is the manager address of the SubDID.
732+
### Return Value
733+
Promise\<`boolean`>
734+
### Example
735+
```javascript
736+
// Check if the owner address has the SubDID 'leon.leonx.bit' under the account 'leonx.bit'.
737+
dotbit.verifyAddrsByAccount(
738+
'0xC72B6f66017246d6A7f159F5C2BF358188AD9ECa',
739+
'leonx.bit',
740+
'leon.leonx.bit',
741+
)
742+
743+
// The printed result would be like:
744+
true
745+
746+
// Check if the manager address has any SubDID under the account 'leonx.bit'.
747+
dotbit.verifyAddrsByAccount(
748+
'0xC72B6f66017246d6A7f159F5C2BF358188AD9ECa',
749+
'leonx.bit',
750+
null,
751+
1
752+
)
753+
754+
// The printed result would be like:
755+
true
756+
```

src/DotBit.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,4 +188,15 @@ export class DotBit {
188188

189189
return await bitAccount.avatar()
190190
}
191+
192+
/**
193+
* Check if the address has the specified SubDID under the designated account.
194+
* @param address
195+
* @param account
196+
* @param subAccount
197+
* @param verifyType default 0 === "owner", 1 === "manager"
198+
*/
199+
async verifyAddrsByAccount (address: string, account: string, subAccount?: string, verifyType?: number){
200+
return await this.bitIndexer.subAccountVerify(address, account, subAccount, verifyType)
201+
}
191202
}

src/fetchers/BitIndexer.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,4 +72,21 @@ export class BitIndexer {
7272
}])
7373
.then(result => result.account_list.map(item => item.account))
7474
}
75+
76+
/**
77+
* Check if the address has the specified SubDID under the designated account.
78+
* @param address
79+
* @param account
80+
* @param subAccount
81+
* @param verifyType default 0 === "owner", 1 === "manager"
82+
*/
83+
subAccountVerify (address: string, account: string, subAccount?: string, verifyType?: number): Promise<boolean> {
84+
return this.request('das_subAccountVerify', [{
85+
address,
86+
account,
87+
sub_account: subAccount,
88+
verify_type: verifyType ? 1 : 0,
89+
}])
90+
.then(result => result.is_subdid)
91+
}
7592
}

tests/DotBit.test.ts

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,13 @@ import { CoinType } from '../src/const'
44
import { DotBit } from '../src/DotBit'
55
import { BitPluginBase } from '../src/types'
66
import { dotbitProd } from './common/index'
7+
import { BitIndexer } from '../src/fetchers/BitIndexer'
8+
9+
const dotbitTest = new DotBit({
10+
bitIndexer: new BitIndexer({
11+
uri: 'https://test-indexer.did.id',
12+
})
13+
})
714

815
describe('serverInfo', function () {
916
it('work', async function () {
@@ -193,3 +200,53 @@ describe('addrs', function () {
193200
expect(addrs.length).toBe(1)
194201
}, 10000)
195202
})
203+
204+
describe('verifyAddrsByAccount', function () {
205+
it('should work', async function () {
206+
const isValid = await dotbitTest.verifyAddrsByAccount(
207+
'0xC72B6f66017246d6A7f159F5C2BF358188AD9ECa',
208+
'leonx.bit',
209+
'leon.leonx.bit',
210+
)
211+
expect(isValid).toBe(true)
212+
})
213+
it('should work', async function () {
214+
const isValid = await dotbitTest.verifyAddrsByAccount(
215+
'0xC72B6f66017246d6A7f159F5C2BF358188AD9ECa',
216+
'leonx.bit',
217+
)
218+
expect(isValid).toBe(true)
219+
})
220+
it('should work', async function () {
221+
const isValid = await dotbitTest.verifyAddrsByAccount(
222+
'0xC72B6f66017246d6A7f159F5C2BF358188AD9ECa',
223+
'leonx.bit',
224+
'leon123123.leonx.bit',
225+
)
226+
expect(isValid).toBe(false)
227+
})
228+
it('should work', async function () {
229+
const isValid = await dotbitTest.verifyAddrsByAccount(
230+
'0xC72B6f66017246d6A7f159F5C2BF358188AD9ECa',
231+
'123.bit',
232+
'123.123.bit',
233+
)
234+
expect(isValid).toBe(false)
235+
})
236+
it('should work', async function () {
237+
const isValid = await dotbitTest.verifyAddrsByAccount(
238+
'0xC72B6f66017246d6A7f159F5C2BF358188AD9ECa',
239+
'123.bit',
240+
)
241+
expect(isValid).toBe(false)
242+
})
243+
it('should work', async function () {
244+
const isValid = await dotbitTest.verifyAddrsByAccount(
245+
'0xC72B6f66017246d6A7f159F5C2BF358188AD9ECa',
246+
'leonx.bit',
247+
null,
248+
1
249+
)
250+
expect(isValid).toBe(true)
251+
})
252+
})

0 commit comments

Comments
 (0)