Skip to content

Commit 955cc72

Browse files
authored
fix(bioforest-api): require explicit magic from genesisBlock (#487)
1 parent f4308b9 commit 955cc72

3 files changed

Lines changed: 72 additions & 5 deletions

File tree

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
import { describe, expect, it } from 'vitest'
2+
3+
import { BioForestApiClient, BioForestApiError } from './client'
4+
5+
describe('BioForestApiClient.getBalance', () => {
6+
it('uses the provided magic from genesisBlock after trim', async () => {
7+
const requests: Array<{ url: string; init: RequestInit | undefined }> = []
8+
const fetchFn: typeof fetch = async (input, init) => {
9+
requests.push({ url: String(input), init })
10+
return new Response(
11+
JSON.stringify({
12+
success: true,
13+
result: { amount: '100' },
14+
}),
15+
{
16+
status: 200,
17+
headers: { 'Content-Type': 'application/json' },
18+
},
19+
)
20+
}
21+
22+
const client = new BioForestApiClient({
23+
rpcUrl: 'https://walletapi.bfmeta.info',
24+
chainId: 'bfm',
25+
fetch: fetchFn,
26+
})
27+
28+
await client.getBalance('b-test', 'BFM', ' LLLQL ')
29+
const request = requests[0]
30+
expect(request?.url).toBe('https://walletapi.bfmeta.info/wallet/bfm/address/balance')
31+
expect(request?.init?.method).toBe('POST')
32+
expect(request?.init?.body).toBe(
33+
JSON.stringify({
34+
address: 'b-test',
35+
magic: 'LLLQL',
36+
assetType: 'BFM',
37+
}),
38+
)
39+
})
40+
41+
it('throws when magic is empty', async () => {
42+
let called = false
43+
const fetchFn: typeof fetch = async () => {
44+
called = true
45+
return new Response(JSON.stringify({ success: true, result: { amount: '0' } }), {
46+
status: 200,
47+
headers: { 'Content-Type': 'application/json' },
48+
})
49+
}
50+
51+
const client = new BioForestApiClient({
52+
rpcUrl: 'https://walletapi.bfmeta.info',
53+
chainId: 'bfm',
54+
fetch: fetchFn,
55+
})
56+
57+
await expect(client.getBalance('b-test', 'BFM', ' ')).rejects.toBeInstanceOf(BioForestApiError)
58+
expect(called).toBe(false)
59+
})
60+
})

src/services/bioforest-api/client.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,8 @@ export interface BioForestApiClientConfig {
5757
* const block = await client.getLastBlock()
5858
* console.log(`Height: ${block.height}`)
5959
*
60-
* const balance = await client.getBalance('bXXX...', 'BFM')
60+
* const magic = '...from genesisBlock.magic'
61+
* const balance = await client.getBalance('bXXX...', 'BFM', magic)
6162
* console.log(`Balance: ${balance.amount}`)
6263
* ```
6364
*/
@@ -172,12 +173,17 @@ export class BioForestApiClient {
172173
* Get account balance for a specific asset
173174
* @param address - Account address
174175
* @param assetType - Asset type (e.g., 'BFM')
175-
* @param magic - Chain magic (default: 'nxOGQ' for mainnet)
176+
* @param magic - Chain magic (必须来自 default-config 的 genesisBlock.magic)
176177
*/
177-
async getBalance(address: string, assetType: string, magic = 'nxOGQ'): Promise<BalanceInfo> {
178+
async getBalance(address: string, assetType: string, magic: string): Promise<BalanceInfo> {
179+
const chainMagic = magic.trim()
180+
if (!chainMagic) {
181+
throw new BioForestApiError('Chain magic is required. Use default-config genesisBlock.magic.')
182+
}
183+
178184
return this.post<BalanceInfo>('/address/balance', {
179185
address,
180-
magic,
186+
magic: chainMagic,
181187
assetType,
182188
})
183189
}

src/services/bioforest-api/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@
2020
* const block = await client.getLastBlock()
2121
*
2222
* // Get balance
23-
* const balance = await client.getBalance('bXXX...', 'BFM')
23+
* const magic = '...from genesisBlock.magic'
24+
* const balance = await client.getBalance('bXXX...', 'BFM', magic)
2425
*
2526
* // Check pay password status
2627
* const hasTwoStepSecret = await client.hasTwoStepSecret('bXXX...')

0 commit comments

Comments
 (0)