Skip to content

Commit e72bb63

Browse files
authored
feat: add rchash method (#1035)
1 parent fdc25b0 commit e72bb63

File tree

3 files changed

+37
-0
lines changed

3 files changed

+37
-0
lines changed

src/bee.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import * as grantee from './modules/grantee'
2222
import * as gsoc from './modules/gsoc'
2323
import * as pinning from './modules/pinning'
2424
import * as pss from './modules/pss'
25+
import { rchash } from './modules/rchash'
2526
import * as status from './modules/status'
2627
import * as stewardship from './modules/stewardship'
2728
import * as tag from './modules/tag'
@@ -1248,6 +1249,13 @@ export class Bee {
12481249
return postEnvelope(this.getRequestOptionsForCall(options), postageBatchId, reference)
12491250
}
12501251

1252+
/**
1253+
* Get reserve commitment hash duration seconds
1254+
*/
1255+
async rchash(depth: number, anchor1: string, anchor2: string, options?: BeeRequestOptions): Promise<number> {
1256+
return rchash(this.getRequestOptionsForCall(options), depth, anchor1, anchor2)
1257+
}
1258+
12511259
/**
12521260
* Ping the Bee node to see if there is a live Bee node on the given URL.
12531261
*

src/modules/rchash.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import { Types } from 'cafe-utility'
2+
import type { BeeRequestOptions } from '../types'
3+
import { http } from '../utils/http'
4+
5+
const RCHASH_ENDPOINT = 'rchash'
6+
7+
export async function rchash(
8+
requestOptions: BeeRequestOptions,
9+
depth: number,
10+
anchor1: string,
11+
anchor2: string,
12+
): Promise<number> {
13+
const response = await http<unknown>(requestOptions, {
14+
responseType: 'json',
15+
url: `${RCHASH_ENDPOINT}/${depth}/${anchor1}/${anchor2}`,
16+
})
17+
18+
const body = Types.asObject(response.data, { name: 'response.data' })
19+
20+
return Types.asNumber(body.durationSeconds, { name: 'durationSeconds' })
21+
}

test/integration/rchash.spec.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { makeBee } from '../utils'
2+
3+
test('GET rchash', async () => {
4+
const bee = makeBee()
5+
const durationSeconds = await bee.rchash(15, '0bab', '0bab')
6+
expect(durationSeconds).toBeGreaterThan(1)
7+
expect(durationSeconds).toBeLessThan(60)
8+
})

0 commit comments

Comments
 (0)