@hashgraphonline/standards-sdk 0.1.141 → 0.1.143-feat-adapter-registry.canary.fb173dc.54
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +24 -1
- package/dist/cjs/hcs-11/client.d.ts.map +1 -1
- package/dist/cjs/hcs-11/types.d.ts +1 -0
- package/dist/cjs/hcs-11/types.d.ts.map +1 -1
- package/dist/cjs/hcs-12/validation/schemas.d.ts +8 -8
- package/dist/cjs/hcs-16/base-client.d.ts.map +1 -1
- package/dist/cjs/hcs-17/types.d.ts.map +1 -1
- package/dist/cjs/hcs-20/sdk.d.ts.map +1 -1
- package/dist/cjs/hcs-20/types.d.ts +9 -8
- package/dist/cjs/hcs-20/types.d.ts.map +1 -1
- package/dist/cjs/hcs-21/base-client.d.ts +14 -12
- package/dist/cjs/hcs-21/base-client.d.ts.map +1 -1
- package/dist/cjs/hcs-21/browser.d.ts +17 -7
- package/dist/cjs/hcs-21/browser.d.ts.map +1 -1
- package/dist/cjs/hcs-21/index.d.ts +1 -0
- package/dist/cjs/hcs-21/index.d.ts.map +1 -1
- package/dist/cjs/hcs-21/sdk.d.ts +70 -6
- package/dist/cjs/hcs-21/sdk.d.ts.map +1 -1
- package/dist/cjs/hcs-21/tx.d.ts +6 -2
- package/dist/cjs/hcs-21/tx.d.ts.map +1 -1
- package/dist/cjs/hcs-21/types.d.ts +751 -66
- package/dist/cjs/hcs-21/types.d.ts.map +1 -1
- package/dist/cjs/hcs-21/verify.d.ts +6 -0
- package/dist/cjs/hcs-21/verify.d.ts.map +1 -0
- package/dist/cjs/inscribe/inscriber.d.ts +3 -1
- package/dist/cjs/inscribe/inscriber.d.ts.map +1 -1
- package/dist/cjs/inscribe/quote-cache.d.ts.map +1 -1
- package/dist/cjs/inscribe/types.d.ts +7 -0
- package/dist/cjs/inscribe/types.d.ts.map +1 -1
- package/dist/cjs/services/registry-broker/client/adapters.d.ts +20 -0
- package/dist/cjs/services/registry-broker/client/adapters.d.ts.map +1 -0
- package/dist/cjs/services/registry-broker/client/agents.d.ts +16 -0
- package/dist/cjs/services/registry-broker/client/agents.d.ts.map +1 -0
- package/dist/cjs/services/registry-broker/client/base-client.d.ts +57 -0
- package/dist/cjs/services/registry-broker/client/base-client.d.ts.map +1 -0
- package/dist/cjs/services/registry-broker/client/chat-history.d.ts +23 -0
- package/dist/cjs/services/registry-broker/client/chat-history.d.ts.map +1 -0
- package/dist/cjs/services/registry-broker/client/chat.d.ts +33 -0
- package/dist/cjs/services/registry-broker/client/chat.d.ts.map +1 -0
- package/dist/cjs/services/registry-broker/client/credits.d.ts +40 -0
- package/dist/cjs/services/registry-broker/client/credits.d.ts.map +1 -0
- package/dist/cjs/services/registry-broker/client/encrypted-chat-manager.d.ts +35 -0
- package/dist/cjs/services/registry-broker/client/encrypted-chat-manager.d.ts.map +1 -0
- package/dist/cjs/services/registry-broker/client/encryption.d.ts +33 -0
- package/dist/cjs/services/registry-broker/client/encryption.d.ts.map +1 -0
- package/dist/cjs/services/registry-broker/client/errors.d.ts +19 -0
- package/dist/cjs/services/registry-broker/client/errors.d.ts.map +1 -0
- package/dist/cjs/services/registry-broker/client/ledger-auth.d.ts +10 -0
- package/dist/cjs/services/registry-broker/client/ledger-auth.d.ts.map +1 -0
- package/dist/cjs/services/registry-broker/client/search.d.ts +19 -0
- package/dist/cjs/services/registry-broker/client/search.d.ts.map +1 -0
- package/dist/cjs/services/registry-broker/client/utils.d.ts +21 -0
- package/dist/cjs/services/registry-broker/client/utils.d.ts.map +1 -0
- package/dist/cjs/services/registry-broker/client.d.ts +2 -204
- package/dist/cjs/services/registry-broker/client.d.ts.map +1 -1
- package/dist/cjs/services/registry-broker/private-key-signer.d.ts.map +1 -1
- package/dist/cjs/services/registry-broker/schemas.d.ts +1473 -177
- package/dist/cjs/services/registry-broker/schemas.d.ts.map +1 -1
- package/dist/cjs/services/registry-broker/types.d.ts +45 -3
- package/dist/cjs/services/registry-broker/types.d.ts.map +1 -1
- package/dist/cjs/services/types.d.ts +1 -0
- package/dist/cjs/services/types.d.ts.map +1 -1
- package/dist/cjs/standards-sdk.cjs +2 -2
- package/dist/cjs/standards-sdk.cjs.map +1 -1
- package/dist/cjs/utils/dynamic-import.d.ts +1 -0
- package/dist/cjs/utils/dynamic-import.d.ts.map +1 -1
- package/dist/es/hcs-11/client.d.ts.map +1 -1
- package/dist/es/hcs-11/types.d.ts +1 -0
- package/dist/es/hcs-11/types.d.ts.map +1 -1
- package/dist/es/hcs-12/validation/schemas.d.ts +8 -8
- package/dist/es/hcs-16/base-client.d.ts.map +1 -1
- package/dist/es/hcs-17/types.d.ts.map +1 -1
- package/dist/es/hcs-20/sdk.d.ts.map +1 -1
- package/dist/es/hcs-20/types.d.ts +9 -8
- package/dist/es/hcs-20/types.d.ts.map +1 -1
- package/dist/es/hcs-21/base-client.d.ts +14 -12
- package/dist/es/hcs-21/base-client.d.ts.map +1 -1
- package/dist/es/hcs-21/browser.d.ts +17 -7
- package/dist/es/hcs-21/browser.d.ts.map +1 -1
- package/dist/es/hcs-21/index.d.ts +1 -0
- package/dist/es/hcs-21/index.d.ts.map +1 -1
- package/dist/es/hcs-21/sdk.d.ts +70 -6
- package/dist/es/hcs-21/sdk.d.ts.map +1 -1
- package/dist/es/hcs-21/tx.d.ts +6 -2
- package/dist/es/hcs-21/tx.d.ts.map +1 -1
- package/dist/es/hcs-21/types.d.ts +751 -66
- package/dist/es/hcs-21/types.d.ts.map +1 -1
- package/dist/es/hcs-21/verify.d.ts +6 -0
- package/dist/es/hcs-21/verify.d.ts.map +1 -0
- package/dist/es/inscribe/inscriber.d.ts +3 -1
- package/dist/es/inscribe/inscriber.d.ts.map +1 -1
- package/dist/es/inscribe/quote-cache.d.ts.map +1 -1
- package/dist/es/inscribe/types.d.ts +7 -0
- package/dist/es/inscribe/types.d.ts.map +1 -1
- package/dist/es/services/registry-broker/client/adapters.d.ts +20 -0
- package/dist/es/services/registry-broker/client/adapters.d.ts.map +1 -0
- package/dist/es/services/registry-broker/client/agents.d.ts +16 -0
- package/dist/es/services/registry-broker/client/agents.d.ts.map +1 -0
- package/dist/es/services/registry-broker/client/base-client.d.ts +57 -0
- package/dist/es/services/registry-broker/client/base-client.d.ts.map +1 -0
- package/dist/es/services/registry-broker/client/chat-history.d.ts +23 -0
- package/dist/es/services/registry-broker/client/chat-history.d.ts.map +1 -0
- package/dist/es/services/registry-broker/client/chat.d.ts +33 -0
- package/dist/es/services/registry-broker/client/chat.d.ts.map +1 -0
- package/dist/es/services/registry-broker/client/credits.d.ts +40 -0
- package/dist/es/services/registry-broker/client/credits.d.ts.map +1 -0
- package/dist/es/services/registry-broker/client/encrypted-chat-manager.d.ts +35 -0
- package/dist/es/services/registry-broker/client/encrypted-chat-manager.d.ts.map +1 -0
- package/dist/es/services/registry-broker/client/encryption.d.ts +33 -0
- package/dist/es/services/registry-broker/client/encryption.d.ts.map +1 -0
- package/dist/es/services/registry-broker/client/errors.d.ts +19 -0
- package/dist/es/services/registry-broker/client/errors.d.ts.map +1 -0
- package/dist/es/services/registry-broker/client/ledger-auth.d.ts +10 -0
- package/dist/es/services/registry-broker/client/ledger-auth.d.ts.map +1 -0
- package/dist/es/services/registry-broker/client/search.d.ts +19 -0
- package/dist/es/services/registry-broker/client/search.d.ts.map +1 -0
- package/dist/es/services/registry-broker/client/utils.d.ts +21 -0
- package/dist/es/services/registry-broker/client/utils.d.ts.map +1 -0
- package/dist/es/services/registry-broker/client.d.ts +2 -204
- package/dist/es/services/registry-broker/client.d.ts.map +1 -1
- package/dist/es/services/registry-broker/private-key-signer.d.ts.map +1 -1
- package/dist/es/services/registry-broker/schemas.d.ts +1473 -177
- package/dist/es/services/registry-broker/schemas.d.ts.map +1 -1
- package/dist/es/services/registry-broker/types.d.ts +45 -3
- package/dist/es/services/registry-broker/types.d.ts.map +1 -1
- package/dist/es/services/types.d.ts +1 -0
- package/dist/es/services/types.d.ts.map +1 -1
- package/dist/es/standards-sdk.es.js +73 -51
- package/dist/es/standards-sdk.es.js.map +1 -1
- package/dist/es/standards-sdk.es10.js +2 -2
- package/dist/es/standards-sdk.es100.js +26 -229
- package/dist/es/standards-sdk.es100.js.map +1 -1
- package/dist/es/standards-sdk.es101.js +228 -109
- package/dist/es/standards-sdk.es101.js.map +1 -1
- package/dist/es/standards-sdk.es102.js +94 -15
- package/dist/es/standards-sdk.es102.js.map +1 -1
- package/dist/es/standards-sdk.es103.js +30 -80
- package/dist/es/standards-sdk.es103.js.map +1 -1
- package/dist/es/standards-sdk.es104.js +80 -27
- package/dist/es/standards-sdk.es104.js.map +1 -1
- package/dist/es/standards-sdk.es105.js +25 -136
- package/dist/es/standards-sdk.es105.js.map +1 -1
- package/dist/es/standards-sdk.es106.js +140 -27
- package/dist/es/standards-sdk.es106.js.map +1 -1
- package/dist/es/standards-sdk.es107.js +27 -20
- package/dist/es/standards-sdk.es107.js.map +1 -1
- package/dist/es/standards-sdk.es108.js +18 -156
- package/dist/es/standards-sdk.es108.js.map +1 -1
- package/dist/es/standards-sdk.es109.js +148 -196
- package/dist/es/standards-sdk.es109.js.map +1 -1
- package/dist/es/standards-sdk.es110.js +167 -747
- package/dist/es/standards-sdk.es110.js.map +1 -1
- package/dist/es/standards-sdk.es111.js +786 -9
- package/dist/es/standards-sdk.es111.js.map +1 -1
- package/dist/es/standards-sdk.es112.js +13 -567
- package/dist/es/standards-sdk.es112.js.map +1 -1
- package/dist/es/standards-sdk.es113.js +541 -576
- package/dist/es/standards-sdk.es113.js.map +1 -1
- package/dist/es/standards-sdk.es114.js +601 -12
- package/dist/es/standards-sdk.es114.js.map +1 -1
- package/dist/es/standards-sdk.es115.js +13 -2
- package/dist/es/standards-sdk.es115.js.map +1 -1
- package/dist/es/standards-sdk.es116.js +2 -83
- package/dist/es/standards-sdk.es116.js.map +1 -1
- package/dist/es/standards-sdk.es117.js +80 -36
- package/dist/es/standards-sdk.es117.js.map +1 -1
- package/dist/es/standards-sdk.es118.js +39 -2
- package/dist/es/standards-sdk.es118.js.map +1 -1
- package/dist/es/standards-sdk.es119.js +2 -223
- package/dist/es/standards-sdk.es119.js.map +1 -1
- package/dist/es/standards-sdk.es12.js +1 -1
- package/dist/es/standards-sdk.es120.js +193 -1110
- package/dist/es/standards-sdk.es120.js.map +1 -1
- package/dist/es/standards-sdk.es121.js +1059 -225
- package/dist/es/standards-sdk.es121.js.map +1 -1
- package/dist/es/standards-sdk.es122.js +303 -419
- package/dist/es/standards-sdk.es122.js.map +1 -1
- package/dist/es/standards-sdk.es123.js +418 -351
- package/dist/es/standards-sdk.es123.js.map +1 -1
- package/dist/es/standards-sdk.es124.js +348 -754
- package/dist/es/standards-sdk.es124.js.map +1 -1
- package/dist/es/standards-sdk.es125.js +854 -178
- package/dist/es/standards-sdk.es125.js.map +1 -1
- package/dist/es/standards-sdk.es126.js +153 -1512
- package/dist/es/standards-sdk.es126.js.map +1 -1
- package/dist/es/standards-sdk.es127.js +1373 -1977
- package/dist/es/standards-sdk.es127.js.map +1 -1
- package/dist/es/standards-sdk.es128.js +176 -51
- package/dist/es/standards-sdk.es128.js.map +1 -1
- package/dist/es/standards-sdk.es129.js +196 -76
- package/dist/es/standards-sdk.es129.js.map +1 -1
- package/dist/es/standards-sdk.es13.js +1 -1
- package/dist/es/standards-sdk.es130.js +98 -153
- package/dist/es/standards-sdk.es130.js.map +1 -1
- package/dist/es/standards-sdk.es131.js +122 -8
- package/dist/es/standards-sdk.es131.js.map +1 -1
- package/dist/es/standards-sdk.es132.js +234 -73
- package/dist/es/standards-sdk.es132.js.map +1 -1
- package/dist/es/standards-sdk.es133.js +171 -56
- package/dist/es/standards-sdk.es133.js.map +1 -1
- package/dist/es/standards-sdk.es134.js +123 -29
- package/dist/es/standards-sdk.es134.js.map +1 -1
- package/dist/es/standards-sdk.es135.js +313 -125
- package/dist/es/standards-sdk.es135.js.map +1 -1
- package/dist/es/standards-sdk.es136.js +358 -34
- package/dist/es/standards-sdk.es136.js.map +1 -1
- package/dist/es/standards-sdk.es137.js +17 -732
- package/dist/es/standards-sdk.es137.js.map +1 -1
- package/dist/es/standards-sdk.es138.js +65 -12264
- package/dist/es/standards-sdk.es138.js.map +1 -1
- package/dist/es/standards-sdk.es139.js +86 -16
- package/dist/es/standards-sdk.es139.js.map +1 -1
- package/dist/es/standards-sdk.es14.js +1 -1
- package/dist/es/standards-sdk.es140.js +152 -49
- package/dist/es/standards-sdk.es140.js.map +1 -1
- package/dist/es/standards-sdk.es141.js +7 -64
- package/dist/es/standards-sdk.es141.js.map +1 -1
- package/dist/es/standards-sdk.es142.js +71 -157
- package/dist/es/standards-sdk.es142.js.map +1 -1
- package/dist/es/standards-sdk.es143.js +49 -306
- package/dist/es/standards-sdk.es143.js.map +1 -1
- package/dist/es/standards-sdk.es144.js +28 -346
- package/dist/es/standards-sdk.es144.js.map +1 -1
- package/dist/es/standards-sdk.es145.js +112 -427
- package/dist/es/standards-sdk.es145.js.map +1 -1
- package/dist/es/standards-sdk.es146.js +40 -329
- package/dist/es/standards-sdk.es146.js.map +1 -1
- package/dist/es/standards-sdk.es147.js +12277 -66
- package/dist/es/standards-sdk.es147.js.map +1 -1
- package/dist/es/standards-sdk.es148.js +20 -0
- package/dist/es/standards-sdk.es148.js.map +1 -0
- package/dist/es/standards-sdk.es149.js +59 -0
- package/dist/es/standards-sdk.es149.js.map +1 -0
- package/dist/es/standards-sdk.es15.js +1 -1
- package/dist/es/standards-sdk.es150.js +85 -0
- package/dist/es/standards-sdk.es150.js.map +1 -0
- package/dist/es/standards-sdk.es151.js +175 -0
- package/dist/es/standards-sdk.es151.js.map +1 -0
- package/dist/es/standards-sdk.es152.js +325 -0
- package/dist/es/standards-sdk.es152.js.map +1 -0
- package/dist/es/standards-sdk.es153.js +349 -0
- package/dist/es/standards-sdk.es153.js.map +1 -0
- package/dist/es/standards-sdk.es154.js +456 -0
- package/dist/es/standards-sdk.es154.js.map +1 -0
- package/dist/es/standards-sdk.es155.js +334 -0
- package/dist/es/standards-sdk.es155.js.map +1 -0
- package/dist/es/standards-sdk.es156.js +79 -0
- package/dist/es/standards-sdk.es156.js.map +1 -0
- package/dist/es/standards-sdk.es157.js +874 -0
- package/dist/es/standards-sdk.es157.js.map +1 -0
- package/dist/es/standards-sdk.es158.js +242 -0
- package/dist/es/standards-sdk.es158.js.map +1 -0
- package/dist/es/standards-sdk.es159.js +247 -0
- package/dist/es/standards-sdk.es159.js.map +1 -0
- package/dist/es/standards-sdk.es16.js +1 -1
- package/dist/es/standards-sdk.es17.js +13 -11
- package/dist/es/standards-sdk.es17.js.map +1 -1
- package/dist/es/standards-sdk.es19.js +20 -17
- package/dist/es/standards-sdk.es19.js.map +1 -1
- package/dist/es/standards-sdk.es20.js +17 -14
- package/dist/es/standards-sdk.es20.js.map +1 -1
- package/dist/es/standards-sdk.es21.js +1 -1
- package/dist/es/standards-sdk.es22.js +1 -1
- package/dist/es/standards-sdk.es23.js +1 -1
- package/dist/es/standards-sdk.es24.js +1 -1
- package/dist/es/standards-sdk.es25.js +1 -1
- package/dist/es/standards-sdk.es26.js +1 -1
- package/dist/es/standards-sdk.es27.js +1 -1
- package/dist/es/standards-sdk.es28.js +24 -19
- package/dist/es/standards-sdk.es28.js.map +1 -1
- package/dist/es/standards-sdk.es29.js.map +1 -1
- package/dist/es/standards-sdk.es3.js +2 -2
- package/dist/es/standards-sdk.es31.js +2 -2
- package/dist/es/standards-sdk.es32.js +4 -4
- package/dist/es/standards-sdk.es33.js +1 -1
- package/dist/es/standards-sdk.es36.js +16 -13
- package/dist/es/standards-sdk.es36.js.map +1 -1
- package/dist/es/standards-sdk.es37.js +4 -4
- package/dist/es/standards-sdk.es38.js +2 -2
- package/dist/es/standards-sdk.es39.js +2 -2
- package/dist/es/standards-sdk.es40.js +1 -1
- package/dist/es/standards-sdk.es41.js +1 -1
- package/dist/es/standards-sdk.es42.js +2 -2
- package/dist/es/standards-sdk.es47.js +1 -1
- package/dist/es/standards-sdk.es5.js +2 -2
- package/dist/es/standards-sdk.es52.js +1 -1
- package/dist/es/standards-sdk.es54.js +1 -1
- package/dist/es/standards-sdk.es57.js +2 -2
- package/dist/es/standards-sdk.es59.js +1 -1
- package/dist/es/standards-sdk.es6.js +2 -2
- package/dist/es/standards-sdk.es60.js +1 -1
- package/dist/es/standards-sdk.es61.js +15 -12
- package/dist/es/standards-sdk.es61.js.map +1 -1
- package/dist/es/standards-sdk.es63.js +1 -1
- package/dist/es/standards-sdk.es65.js +2 -2
- package/dist/es/standards-sdk.es66.js +3 -3
- package/dist/es/standards-sdk.es69.js +2 -2
- package/dist/es/standards-sdk.es7.js +2 -2
- package/dist/es/standards-sdk.es70.js +3 -3
- package/dist/es/standards-sdk.es71.js +2 -2
- package/dist/es/standards-sdk.es72.js +1 -1
- package/dist/es/standards-sdk.es73.js.map +1 -1
- package/dist/es/standards-sdk.es75.js +2 -2
- package/dist/es/standards-sdk.es77.js +5 -3
- package/dist/es/standards-sdk.es77.js.map +1 -1
- package/dist/es/standards-sdk.es78.js +12 -11
- package/dist/es/standards-sdk.es78.js.map +1 -1
- package/dist/es/standards-sdk.es79.js +1 -1
- package/dist/es/standards-sdk.es8.js +1 -1
- package/dist/es/standards-sdk.es80.js +113 -29
- package/dist/es/standards-sdk.es80.js.map +1 -1
- package/dist/es/standards-sdk.es82.js +23 -4
- package/dist/es/standards-sdk.es82.js.map +1 -1
- package/dist/es/standards-sdk.es83.js +39 -14
- package/dist/es/standards-sdk.es83.js.map +1 -1
- package/dist/es/standards-sdk.es84.js +198 -17
- package/dist/es/standards-sdk.es84.js.map +1 -1
- package/dist/es/standards-sdk.es85.js +256 -9
- package/dist/es/standards-sdk.es85.js.map +1 -1
- package/dist/es/standards-sdk.es86.js +55 -21
- package/dist/es/standards-sdk.es86.js.map +1 -1
- package/dist/es/standards-sdk.es87.js +22 -75
- package/dist/es/standards-sdk.es87.js.map +1 -1
- package/dist/es/standards-sdk.es88.js +45 -30
- package/dist/es/standards-sdk.es88.js.map +1 -1
- package/dist/es/standards-sdk.es89.js +57 -22
- package/dist/es/standards-sdk.es89.js.map +1 -1
- package/dist/es/standards-sdk.es90.js +28 -23
- package/dist/es/standards-sdk.es90.js.map +1 -1
- package/dist/es/standards-sdk.es91.js +23 -167
- package/dist/es/standards-sdk.es91.js.map +1 -1
- package/dist/es/standards-sdk.es92.js +158 -119
- package/dist/es/standards-sdk.es92.js.map +1 -1
- package/dist/es/standards-sdk.es93.js +68 -95
- package/dist/es/standards-sdk.es93.js.map +1 -1
- package/dist/es/standards-sdk.es94.js +136 -119
- package/dist/es/standards-sdk.es94.js.map +1 -1
- package/dist/es/standards-sdk.es95.js +139 -39
- package/dist/es/standards-sdk.es95.js.map +1 -1
- package/dist/es/standards-sdk.es96.js +42 -257
- package/dist/es/standards-sdk.es96.js.map +1 -1
- package/dist/es/standards-sdk.es97.js +243 -80
- package/dist/es/standards-sdk.es97.js.map +1 -1
- package/dist/es/standards-sdk.es98.js +47 -48
- package/dist/es/standards-sdk.es98.js.map +1 -1
- package/dist/es/standards-sdk.es99.js +100 -28
- package/dist/es/standards-sdk.es99.js.map +1 -1
- package/dist/es/utils/dynamic-import.d.ts +1 -0
- package/dist/es/utils/dynamic-import.d.ts.map +1 -1
- package/package.json +6 -10
|
@@ -1,2174 +1,1570 @@
|
|
|
1
|
-
import
|
|
2
|
-
import { Buffer } from "node:buffer";
|
|
3
|
-
import { randomBytes, createHash, createCipheriv, createDecipheriv } from "node:crypto";
|
|
4
|
-
import { secp256k1 } from "@noble/curves/secp256k1.js";
|
|
5
|
-
import { canonicalizeLedgerNetwork } from "./standards-sdk.es129.js";
|
|
1
|
+
import { PublicKey, Timestamp, AccountId } from "@hashgraph/sdk";
|
|
6
2
|
import axios from "axios";
|
|
7
|
-
import {
|
|
8
|
-
import "
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
return value.toISOString();
|
|
43
|
-
}
|
|
44
|
-
if (typeof value === "string" || typeof value === "number" || typeof value === "boolean") {
|
|
45
|
-
return value;
|
|
46
|
-
}
|
|
47
|
-
if (Array.isArray(value)) {
|
|
48
|
-
return value.map((item) => item === void 0 ? null : toJsonValue(item));
|
|
49
|
-
}
|
|
50
|
-
if (typeof value === "object") {
|
|
51
|
-
const result = {};
|
|
52
|
-
Object.entries(value).forEach(
|
|
53
|
-
([key, entryValue]) => {
|
|
54
|
-
if (entryValue !== void 0) {
|
|
55
|
-
result[key] = toJsonValue(entryValue);
|
|
56
|
-
}
|
|
57
|
-
}
|
|
3
|
+
import { Logger } from "./standards-sdk.es106.js";
|
|
4
|
+
import { proto } from "@hashgraph/proto";
|
|
5
|
+
class HederaMirrorNode {
|
|
6
|
+
constructor(network, logger, config) {
|
|
7
|
+
this.maxRetries = 5;
|
|
8
|
+
this.initialDelayMs = 2e3;
|
|
9
|
+
this.maxDelayMs = 3e4;
|
|
10
|
+
this.backoffFactor = 2;
|
|
11
|
+
this.network = network;
|
|
12
|
+
this.apiKey = config?.apiKey;
|
|
13
|
+
this.customHeaders = config?.headers || {};
|
|
14
|
+
this.baseUrl = config?.customUrl || this.getMirrorNodeUrl();
|
|
15
|
+
this.logger = logger || new Logger({
|
|
16
|
+
level: "debug",
|
|
17
|
+
module: "MirrorNode"
|
|
18
|
+
});
|
|
19
|
+
this.isServerEnvironment = typeof window === "undefined";
|
|
20
|
+
if (config?.customUrl) {
|
|
21
|
+
this.logger.info(`Using custom mirror node URL: ${config.customUrl}`);
|
|
22
|
+
}
|
|
23
|
+
if (config?.apiKey) {
|
|
24
|
+
this.logger.info("Using API key for mirror node requests");
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* Configures the retry mechanism for API requests.
|
|
29
|
+
* @param config The retry configuration.
|
|
30
|
+
*/
|
|
31
|
+
configureRetry(config) {
|
|
32
|
+
this.maxRetries = config.maxRetries ?? this.maxRetries;
|
|
33
|
+
this.initialDelayMs = config.initialDelayMs ?? this.initialDelayMs;
|
|
34
|
+
this.maxDelayMs = config.maxDelayMs ?? this.maxDelayMs;
|
|
35
|
+
this.backoffFactor = config.backoffFactor ?? this.backoffFactor;
|
|
36
|
+
this.logger.info(
|
|
37
|
+
`Retry configuration updated: maxRetries=${this.maxRetries}, initialDelayMs=${this.initialDelayMs}, maxDelayMs=${this.maxDelayMs}, backoffFactor=${this.backoffFactor}`
|
|
58
38
|
);
|
|
59
|
-
return result;
|
|
60
|
-
}
|
|
61
|
-
throw new TypeError("Only JSON-compatible values are supported");
|
|
62
|
-
};
|
|
63
|
-
const isJsonObject = (value) => typeof value === "object" && value !== null && !Array.isArray(value);
|
|
64
|
-
const toJsonObject = (value) => {
|
|
65
|
-
const normalised = toJsonValue(value);
|
|
66
|
-
if (isJsonObject(normalised)) {
|
|
67
|
-
return normalised;
|
|
68
|
-
}
|
|
69
|
-
throw new TypeError("Expected JSON object value");
|
|
70
|
-
};
|
|
71
|
-
const serialiseAuthConfig = (auth) => {
|
|
72
|
-
const authPayload = {};
|
|
73
|
-
if (auth.type) {
|
|
74
|
-
authPayload.type = auth.type;
|
|
75
|
-
}
|
|
76
|
-
if (auth.token) {
|
|
77
|
-
authPayload.token = auth.token;
|
|
78
|
-
}
|
|
79
|
-
if (auth.username) {
|
|
80
|
-
authPayload.username = auth.username;
|
|
81
|
-
}
|
|
82
|
-
if (auth.password) {
|
|
83
|
-
authPayload.password = auth.password;
|
|
84
|
-
}
|
|
85
|
-
if (auth.headerName) {
|
|
86
|
-
authPayload.headerName = auth.headerName;
|
|
87
|
-
}
|
|
88
|
-
if (auth.headerValue) {
|
|
89
|
-
authPayload.headerValue = auth.headerValue;
|
|
90
|
-
}
|
|
91
|
-
if (auth.headers) {
|
|
92
|
-
authPayload.headers = { ...auth.headers };
|
|
93
|
-
}
|
|
94
|
-
return authPayload;
|
|
95
|
-
};
|
|
96
|
-
const serialiseAgentRegistrationRequest = (payload) => {
|
|
97
|
-
const body = {
|
|
98
|
-
profile: toJsonObject(payload.profile)
|
|
99
|
-
};
|
|
100
|
-
if (payload.endpoint !== void 0) {
|
|
101
|
-
body.endpoint = payload.endpoint;
|
|
102
|
-
}
|
|
103
|
-
if (payload.protocol !== void 0) {
|
|
104
|
-
body.protocol = payload.protocol;
|
|
105
|
-
}
|
|
106
|
-
if (payload.communicationProtocol !== void 0) {
|
|
107
|
-
body.communicationProtocol = payload.communicationProtocol;
|
|
108
|
-
}
|
|
109
|
-
if (payload.registry !== void 0) {
|
|
110
|
-
body.registry = payload.registry;
|
|
111
|
-
}
|
|
112
|
-
if (payload.additionalRegistries !== void 0) {
|
|
113
|
-
body.additionalRegistries = payload.additionalRegistries;
|
|
114
|
-
}
|
|
115
|
-
if (payload.metadata !== void 0) {
|
|
116
|
-
body.metadata = toJsonObject(payload.metadata);
|
|
117
|
-
}
|
|
118
|
-
return body;
|
|
119
|
-
};
|
|
120
|
-
const normalizeHexPrivateKey = (value) => {
|
|
121
|
-
const trimmed = value.trim();
|
|
122
|
-
if (!trimmed) {
|
|
123
|
-
throw new Error("evmPrivateKey is required");
|
|
124
|
-
}
|
|
125
|
-
return trimmed.startsWith("0x") ? trimmed : `0x${trimmed}`;
|
|
126
|
-
};
|
|
127
|
-
class RegistryBrokerError extends Error {
|
|
128
|
-
constructor(message, details) {
|
|
129
|
-
super(message);
|
|
130
|
-
this.status = details.status;
|
|
131
|
-
this.statusText = details.statusText;
|
|
132
|
-
this.body = details.body;
|
|
133
|
-
}
|
|
134
|
-
}
|
|
135
|
-
class RegistryBrokerParseError extends Error {
|
|
136
|
-
constructor(message, cause, rawValue) {
|
|
137
|
-
super(message);
|
|
138
|
-
this.cause = cause;
|
|
139
|
-
this.rawValue = rawValue;
|
|
140
39
|
}
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
40
|
+
/**
|
|
41
|
+
* Updates the mirror node configuration.
|
|
42
|
+
* @param config The new mirror node configuration.
|
|
43
|
+
*/
|
|
44
|
+
configureMirrorNode(config) {
|
|
45
|
+
if (config.customUrl) {
|
|
46
|
+
this.baseUrl = config.customUrl;
|
|
47
|
+
this.logger.info(`Updated mirror node URL: ${config.customUrl}`);
|
|
48
|
+
}
|
|
49
|
+
if (config.apiKey) {
|
|
50
|
+
this.apiKey = config.apiKey;
|
|
51
|
+
this.logger.info("Updated API key for mirror node requests");
|
|
52
|
+
}
|
|
53
|
+
if (config.headers) {
|
|
54
|
+
this.customHeaders = { ...this.customHeaders, ...config.headers };
|
|
55
|
+
this.logger.info("Updated custom headers for mirror node requests");
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* Constructs a full URL for API requests, handling custom providers with API keys in the path.
|
|
60
|
+
* @param endpoint The API endpoint (e.g., '/api/v1/accounts/0.0.123')
|
|
61
|
+
* @returns The full URL for the request
|
|
62
|
+
*/
|
|
63
|
+
constructUrl(endpoint) {
|
|
64
|
+
if (this.baseUrl.includes("<API-KEY>") && this.apiKey) {
|
|
65
|
+
const baseUrlWithKey = this.baseUrl.replace("<API-KEY>", this.apiKey);
|
|
66
|
+
return endpoint.startsWith("/") ? `${baseUrlWithKey}${endpoint}` : `${baseUrlWithKey}/${endpoint}`;
|
|
67
|
+
}
|
|
68
|
+
return endpoint.startsWith("/") ? `${this.baseUrl}${endpoint}` : `${this.baseUrl}/${endpoint}`;
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* Returns the base URL for the Hedera mirror node based on the network type
|
|
72
|
+
* @returns The mirror node base URL
|
|
73
|
+
* @private
|
|
74
|
+
*/
|
|
75
|
+
getMirrorNodeUrl() {
|
|
76
|
+
return this.network === "mainnet" ? "https://mainnet-public.mirrornode.hedera.com" : "https://testnet.mirrornode.hedera.com";
|
|
77
|
+
}
|
|
78
|
+
getBaseUrl() {
|
|
79
|
+
return this.baseUrl;
|
|
80
|
+
}
|
|
81
|
+
/**
|
|
82
|
+
* Retrieves the public key for a given account ID from the mirror node.
|
|
83
|
+
* @param accountId The ID of the account to retrieve the public key for.
|
|
84
|
+
* @returns A promise that resolves to the public key for the given account.
|
|
85
|
+
* @throws An error if the account ID is invalid or the public key cannot be retrieved.
|
|
86
|
+
*/
|
|
87
|
+
async getPublicKey(accountId) {
|
|
88
|
+
this.logger.debug(`Getting public key for account ${accountId}`);
|
|
89
|
+
const accountInfo = await this.requestAccount(accountId);
|
|
90
|
+
try {
|
|
91
|
+
if (!accountInfo || !accountInfo.key) {
|
|
92
|
+
throw new Error(
|
|
93
|
+
`Failed to retrieve public key for account ID: ${accountId}`
|
|
94
|
+
);
|
|
151
95
|
}
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
const appendList = (key, values) => {
|
|
175
|
-
if (!values) {
|
|
176
|
-
return;
|
|
177
|
-
}
|
|
178
|
-
values.forEach((value) => {
|
|
179
|
-
if (typeof value === "string") {
|
|
180
|
-
const trimmed = value.trim();
|
|
181
|
-
if (trimmed.length > 0) {
|
|
182
|
-
query.append(key, trimmed);
|
|
183
|
-
}
|
|
96
|
+
return PublicKey.fromString(accountInfo.key.key);
|
|
97
|
+
} catch (e) {
|
|
98
|
+
const error = e;
|
|
99
|
+
const logMessage = `Error fetching public key from Mirror Node: ${error.message}`;
|
|
100
|
+
this.logger.error(logMessage);
|
|
101
|
+
throw new Error(logMessage);
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
/**
|
|
105
|
+
* Retrieves the memo for a given account ID from the mirror node.
|
|
106
|
+
* @param accountId The ID of the account to retrieve the memo for.
|
|
107
|
+
* @returns A promise that resolves to the memo for the given account.
|
|
108
|
+
* @throws An error if the account ID is invalid or the memo cannot be retrieved.
|
|
109
|
+
*/
|
|
110
|
+
async getAccountMemo(accountId) {
|
|
111
|
+
this.logger.debug(`Getting account memo for account ID: ${accountId}`);
|
|
112
|
+
try {
|
|
113
|
+
const accountInfo = await this._requestWithRetry(
|
|
114
|
+
`/api/v1/accounts/${accountId}`
|
|
115
|
+
);
|
|
116
|
+
if (accountInfo?.memo) {
|
|
117
|
+
return accountInfo.memo;
|
|
184
118
|
}
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
if (typeof params.page === "number") {
|
|
194
|
-
query.set("page", params.page.toString());
|
|
195
|
-
}
|
|
196
|
-
if (typeof params.limit === "number") {
|
|
197
|
-
query.set("limit", params.limit.toString());
|
|
198
|
-
}
|
|
199
|
-
if (params.registry) {
|
|
200
|
-
const trimmed = params.registry.trim();
|
|
201
|
-
if (trimmed.length > 0) {
|
|
202
|
-
query.set("registry", trimmed);
|
|
119
|
+
this.logger.warn(`No memo found for account ${accountId}`);
|
|
120
|
+
return null;
|
|
121
|
+
} catch (e) {
|
|
122
|
+
const error = e;
|
|
123
|
+
this.logger.error(
|
|
124
|
+
`Failed to get account memo for ${accountId} after retries: ${error.message}`
|
|
125
|
+
);
|
|
126
|
+
return null;
|
|
203
127
|
}
|
|
204
128
|
}
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
}
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
129
|
+
/**
|
|
130
|
+
* Retrieves topic information for a given topic ID from the mirror node.
|
|
131
|
+
* @param topicId The ID of the topic to retrieve information for.
|
|
132
|
+
* @returns A promise that resolves to the topic information.
|
|
133
|
+
* @throws An error if the topic ID is invalid or the information cannot be retrieved.
|
|
134
|
+
*/
|
|
135
|
+
async getTopicInfo(topicId) {
|
|
136
|
+
try {
|
|
137
|
+
this.logger.debug(`Fetching topic info for ${topicId}`);
|
|
138
|
+
const data = await this._requestWithRetry(
|
|
139
|
+
`/api/v1/topics/${topicId}`
|
|
140
|
+
);
|
|
141
|
+
return data;
|
|
142
|
+
} catch (e) {
|
|
143
|
+
const error = e;
|
|
144
|
+
const logMessage = `Error retrieving topic information for ${topicId} after retries: ${error.message}`;
|
|
145
|
+
this.logger.error(logMessage);
|
|
146
|
+
throw new Error(logMessage);
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
/**
|
|
150
|
+
* Retrieves custom fees for a given topic ID from the mirror node.
|
|
151
|
+
* @param topicId The ID of the topic to retrieve custom fees for.
|
|
152
|
+
* @returns A promise that resolves to the custom fees for the given topic.
|
|
153
|
+
* @throws An error if the topic ID is invalid or the custom fees cannot be retrieved.
|
|
154
|
+
*/
|
|
155
|
+
async getTopicFees(topicId) {
|
|
156
|
+
try {
|
|
157
|
+
const topicInfo = await this.getTopicInfo(topicId);
|
|
158
|
+
return topicInfo.custom_fees;
|
|
159
|
+
} catch (e) {
|
|
160
|
+
const error = e;
|
|
161
|
+
const logMessage = `Error retrieving topic fees: ${error.message}`;
|
|
162
|
+
this.logger.error(logMessage);
|
|
163
|
+
return null;
|
|
233
164
|
}
|
|
234
165
|
}
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
166
|
+
/**
|
|
167
|
+
* Retrieves the current HBAR price from the mirror node.
|
|
168
|
+
* @param date The date to retrieve the HBAR price for.
|
|
169
|
+
* @returns A promise that resolves to the HBAR price for the given date.
|
|
170
|
+
* @throws An error if the date is invalid or the price cannot be retrieved.
|
|
171
|
+
*/
|
|
172
|
+
async getHBARPrice(date) {
|
|
173
|
+
try {
|
|
174
|
+
const timestamp = Timestamp.fromDate(date).toString();
|
|
175
|
+
this.logger.debug(`Fetching HBAR price for timestamp ${timestamp}`);
|
|
176
|
+
const response = await this._requestWithRetry(
|
|
177
|
+
`/api/v1/network/exchangerate?timestamp=${timestamp}`
|
|
178
|
+
);
|
|
179
|
+
const usdPrice = Number(response?.current_rate?.cent_equivalent) / Number(response?.current_rate?.hbar_equivalent) / 100;
|
|
180
|
+
return usdPrice;
|
|
181
|
+
} catch (e) {
|
|
182
|
+
const error = e;
|
|
183
|
+
const logMessage = `Error retrieving HBAR price: ${error.message}`;
|
|
184
|
+
this.logger.error(logMessage);
|
|
185
|
+
return null;
|
|
245
186
|
}
|
|
246
187
|
}
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
188
|
+
/**
|
|
189
|
+
* Retrieves token information for a given token ID from the mirror node.
|
|
190
|
+
* @param tokenId The ID of the token to retrieve information for.
|
|
191
|
+
* @returns A promise that resolves to the token information.
|
|
192
|
+
* @throws An error if the token ID is invalid or the information cannot be retrieved.
|
|
193
|
+
*/
|
|
194
|
+
async getTokenInfo(tokenId) {
|
|
195
|
+
this.logger.debug(`Fetching token info for ${tokenId}`);
|
|
196
|
+
try {
|
|
197
|
+
const data = await this._requestWithRetry(
|
|
198
|
+
`/api/v1/tokens/${tokenId}`
|
|
199
|
+
);
|
|
200
|
+
if (data) {
|
|
201
|
+
this.logger.trace(`Token info found for ${tokenId}:`, data);
|
|
202
|
+
return data;
|
|
203
|
+
}
|
|
204
|
+
this.logger.warn(`No token info found for ${tokenId}`);
|
|
205
|
+
return null;
|
|
206
|
+
} catch (e) {
|
|
207
|
+
const error = e;
|
|
208
|
+
const logMessage = `Error fetching token info for ${tokenId}: ${error.message}`;
|
|
209
|
+
this.logger.error(logMessage);
|
|
210
|
+
return null;
|
|
251
211
|
}
|
|
252
212
|
}
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
headers.set("user-agent", DEFAULT_USER_AGENT);
|
|
274
|
-
}
|
|
275
|
-
const init = {
|
|
276
|
-
method: config.method ?? "GET",
|
|
277
|
-
headers
|
|
278
|
-
};
|
|
279
|
-
if (config.body !== void 0) {
|
|
280
|
-
init.body = JSON.stringify(config.body);
|
|
281
|
-
if (!headers.has("content-type")) {
|
|
282
|
-
headers.set("content-type", "application/json");
|
|
213
|
+
/**
|
|
214
|
+
* Retrieves messages for a given topic ID from the mirror node. Supports filtering by sequence number
|
|
215
|
+
* based on the OpenAPI specification.
|
|
216
|
+
* @param topicId The ID of the topic to retrieve messages for.
|
|
217
|
+
* @param options Optional filtering parameters.
|
|
218
|
+
* @returns A promise that resolves to the messages for the given topic.
|
|
219
|
+
*/
|
|
220
|
+
async getTopicMessages(topicId, options) {
|
|
221
|
+
this.logger.trace(
|
|
222
|
+
`Querying messages for topic ${topicId}${options ? " with filters" : ""}`
|
|
223
|
+
);
|
|
224
|
+
let endpoint = `/api/v1/topics/${topicId}/messages`;
|
|
225
|
+
const params = new URLSearchParams();
|
|
226
|
+
if (options) {
|
|
227
|
+
if (options.sequenceNumber !== void 0) {
|
|
228
|
+
const seqNum = typeof options.sequenceNumber === "number" ? options.sequenceNumber.toString() : options.sequenceNumber;
|
|
229
|
+
if (!seqNum.match(/^(gt|gte|lt|lte|eq|ne):/)) {
|
|
230
|
+
params.append("sequencenumber", `gt:${seqNum}`);
|
|
231
|
+
} else {
|
|
232
|
+
params.append("sequencenumber", seqNum);
|
|
283
233
|
}
|
|
284
234
|
}
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
235
|
+
if (options.limit) {
|
|
236
|
+
params.append("limit", options.limit.toString());
|
|
237
|
+
}
|
|
238
|
+
if (options.order) {
|
|
239
|
+
params.append("order", options.order);
|
|
288
240
|
}
|
|
289
|
-
const errorBody = await this.extractErrorBody(response);
|
|
290
|
-
throw new RegistryBrokerError("Registry broker request failed", {
|
|
291
|
-
status: response.status,
|
|
292
|
-
statusText: response.statusText,
|
|
293
|
-
body: errorBody
|
|
294
|
-
});
|
|
295
|
-
};
|
|
296
|
-
this.baseUrl = normaliseBaseUrl(options.baseUrl);
|
|
297
|
-
const fetchCandidate = options.fetchImplementation ?? globalThis.fetch;
|
|
298
|
-
if (!fetchCandidate) {
|
|
299
|
-
throw new Error(
|
|
300
|
-
"A fetch implementation is required for RegistryBrokerClient"
|
|
301
|
-
);
|
|
302
|
-
}
|
|
303
|
-
this.fetchImpl = fetchCandidate;
|
|
304
|
-
this.defaultHeaders = {};
|
|
305
|
-
if (options.defaultHeaders) {
|
|
306
|
-
Object.entries(options.defaultHeaders).forEach(([name, value]) => {
|
|
307
|
-
if (typeof value === "string") {
|
|
308
|
-
this.setDefaultHeader(name, value);
|
|
309
|
-
}
|
|
310
|
-
});
|
|
311
241
|
}
|
|
312
|
-
|
|
313
|
-
|
|
242
|
+
const queryString = params.toString();
|
|
243
|
+
if (queryString) {
|
|
244
|
+
endpoint += `?${queryString}`;
|
|
314
245
|
}
|
|
315
|
-
|
|
316
|
-
|
|
246
|
+
const messages = [];
|
|
247
|
+
let nextEndpoint = endpoint;
|
|
248
|
+
while (nextEndpoint) {
|
|
249
|
+
try {
|
|
250
|
+
const data = await this._requestWithRetry(nextEndpoint);
|
|
251
|
+
if (data.messages && data.messages.length > 0) {
|
|
252
|
+
for (const message of data.messages) {
|
|
253
|
+
try {
|
|
254
|
+
if (!message.message) {
|
|
255
|
+
continue;
|
|
256
|
+
}
|
|
257
|
+
let messageContent;
|
|
258
|
+
try {
|
|
259
|
+
if (this.isServerEnvironment) {
|
|
260
|
+
messageContent = Buffer.from(
|
|
261
|
+
message.message,
|
|
262
|
+
"base64"
|
|
263
|
+
).toString("utf-8");
|
|
264
|
+
} else {
|
|
265
|
+
messageContent = new TextDecoder().decode(
|
|
266
|
+
Uint8Array.from(
|
|
267
|
+
atob(message.message),
|
|
268
|
+
(c) => c.charCodeAt(0)
|
|
269
|
+
)
|
|
270
|
+
);
|
|
271
|
+
}
|
|
272
|
+
} catch (error) {
|
|
273
|
+
const logMessage = `Error decoding message: ${error}`;
|
|
274
|
+
this.logger.error(logMessage);
|
|
275
|
+
continue;
|
|
276
|
+
}
|
|
277
|
+
let messageJson;
|
|
278
|
+
try {
|
|
279
|
+
messageJson = JSON.parse(messageContent);
|
|
280
|
+
} catch (error) {
|
|
281
|
+
const logMessage = `Invalid JSON message content: ${messageContent}`;
|
|
282
|
+
this.logger.error(logMessage);
|
|
283
|
+
continue;
|
|
284
|
+
}
|
|
285
|
+
messageJson.sequence_number = message.sequence_number;
|
|
286
|
+
messages.push({
|
|
287
|
+
...messageJson,
|
|
288
|
+
consensus_timestamp: message.consensus_timestamp,
|
|
289
|
+
sequence_number: message.sequence_number,
|
|
290
|
+
running_hash: message.running_hash,
|
|
291
|
+
running_hash_version: message.running_hash_version,
|
|
292
|
+
topic_id: message.topic_id,
|
|
293
|
+
payer: message.payer_account_id,
|
|
294
|
+
created: new Date(Number(message.consensus_timestamp) * 1e3)
|
|
295
|
+
});
|
|
296
|
+
} catch (error) {
|
|
297
|
+
const logMessage = `Error processing message: ${error.message}`;
|
|
298
|
+
this.logger.error(logMessage);
|
|
299
|
+
}
|
|
300
|
+
}
|
|
301
|
+
}
|
|
302
|
+
nextEndpoint = data.links?.next || "";
|
|
303
|
+
} catch (e) {
|
|
304
|
+
const error = e;
|
|
305
|
+
const logMessage = `Error querying topic messages for topic ${topicId} (endpoint: ${nextEndpoint}) after retries: ${error.message}`;
|
|
306
|
+
this.logger.error(logMessage);
|
|
307
|
+
throw new Error(logMessage);
|
|
308
|
+
}
|
|
317
309
|
}
|
|
318
|
-
|
|
319
|
-
this.historyAutoTopUp = options.historyAutoTopUp;
|
|
320
|
-
this.encryptedChatManager = new EncryptedChatManager(this);
|
|
321
|
-
this.encryptionOptions = options.encryption;
|
|
322
|
-
this.encryptionBootstrapPromise = this.bootstrapEncryptionOptions(
|
|
323
|
-
options.encryption
|
|
324
|
-
);
|
|
325
|
-
this.chat = {
|
|
326
|
-
start: (options2) => this.startChat(options2),
|
|
327
|
-
createSession: (payload) => this.createSession(payload),
|
|
328
|
-
sendMessage: (payload) => this.sendMessage(payload),
|
|
329
|
-
endSession: (sessionId) => this.endSession(sessionId),
|
|
330
|
-
getHistory: (sessionId, options2) => this.fetchHistorySnapshot(sessionId, options2),
|
|
331
|
-
compactHistory: (payload) => this.compactHistory(payload),
|
|
332
|
-
getEncryptionStatus: (sessionId) => this.fetchEncryptionStatus(sessionId),
|
|
333
|
-
submitEncryptionHandshake: (sessionId, payload) => this.postEncryptionHandshake(sessionId, payload),
|
|
334
|
-
startConversation: (opts) => this.startConversation(opts),
|
|
335
|
-
acceptConversation: (opts) => this.acceptConversation(opts)
|
|
336
|
-
};
|
|
337
|
-
this.encryption = {
|
|
338
|
-
registerKey: (payload) => this.registerEncryptionKey(payload),
|
|
339
|
-
generateEphemeralKeyPair: () => this.createEphemeralKeyPair(),
|
|
340
|
-
deriveSharedSecret: (options2) => this.deriveSharedSecret(options2),
|
|
341
|
-
encryptCipherEnvelope: (options2) => this.buildCipherEnvelope(options2),
|
|
342
|
-
decryptCipherEnvelope: (options2) => this.openCipherEnvelope(options2),
|
|
343
|
-
ensureAgentKey: (options2) => this.ensureAgentEncryptionKey(options2)
|
|
344
|
-
};
|
|
345
|
-
this.chat.createEncryptedSession = (options2) => this.encryptedChatManager.startSession(options2);
|
|
346
|
-
this.chat.acceptEncryptedSession = (options2) => this.encryptedChatManager.acceptSession(options2);
|
|
310
|
+
return messages;
|
|
347
311
|
}
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
312
|
+
/**
|
|
313
|
+
* Requests account information for a given account ID from the mirror node.
|
|
314
|
+
* @param accountId The ID of the account to retrieve information for.
|
|
315
|
+
* @returns A promise that resolves to the account information.
|
|
316
|
+
* @throws An error if the account ID is invalid or the information cannot be retrieved.
|
|
317
|
+
*/
|
|
318
|
+
async requestAccount(accountId) {
|
|
319
|
+
try {
|
|
320
|
+
this.logger.debug(`Requesting account info for ${accountId}`);
|
|
321
|
+
const data = await this._requestWithRetry(
|
|
322
|
+
`/api/v1/accounts/${accountId}`
|
|
323
|
+
);
|
|
324
|
+
if (!data) {
|
|
325
|
+
throw new Error(
|
|
326
|
+
`No data received from mirror node for account: ${accountId}`
|
|
327
|
+
);
|
|
328
|
+
}
|
|
329
|
+
return data;
|
|
330
|
+
} catch (e) {
|
|
331
|
+
const error = e;
|
|
332
|
+
const logMessage = `Failed to fetch account ${accountId} after retries: ${error.message}`;
|
|
333
|
+
this.logger.error(logMessage);
|
|
334
|
+
throw new Error(logMessage);
|
|
335
|
+
}
|
|
336
|
+
}
|
|
337
|
+
/**
|
|
338
|
+
* Checks if a user has access to a given key list.
|
|
339
|
+
* @param keyBytes The key list to check access for.
|
|
340
|
+
* @param userPublicKey The public key of the user to check access for.
|
|
341
|
+
* @returns A promise that resolves to true if the user has access, false otherwise.
|
|
342
|
+
*/
|
|
343
|
+
async checkKeyListAccess(keyBytes, userPublicKey) {
|
|
344
|
+
try {
|
|
345
|
+
const key = proto.Key.decode(keyBytes);
|
|
346
|
+
return this.evaluateKeyAccess(key, userPublicKey);
|
|
347
|
+
} catch (e) {
|
|
348
|
+
const error = e;
|
|
349
|
+
const logMessage = `Error decoding protobuf key: ${error.message}`;
|
|
350
|
+
this.logger.error(logMessage);
|
|
351
|
+
throw new Error(logMessage);
|
|
352
|
+
}
|
|
353
|
+
}
|
|
354
|
+
/**
|
|
355
|
+
* Evaluates the access of a given key to a user's public key.
|
|
356
|
+
* @param key The key to evaluate access for.
|
|
357
|
+
* @param userPublicKey The public key of the user to evaluate access for.
|
|
358
|
+
* @returns A promise that resolves to true if the key has access, false otherwise.
|
|
359
|
+
*/
|
|
360
|
+
async evaluateKeyAccess(key, userPublicKey) {
|
|
361
|
+
if (key.ed25519) {
|
|
362
|
+
return this.compareEd25519Key(key.ed25519, userPublicKey);
|
|
363
|
+
}
|
|
364
|
+
if (key.keyList) {
|
|
365
|
+
return this.evaluateKeyList(key.keyList, userPublicKey);
|
|
366
|
+
}
|
|
367
|
+
if (key.thresholdKey && key.thresholdKey.keys) {
|
|
368
|
+
return this.evaluateKeyList(key.thresholdKey.keys, userPublicKey);
|
|
375
369
|
}
|
|
376
|
-
|
|
377
|
-
}
|
|
378
|
-
getDefaultHeaders() {
|
|
379
|
-
return { ...this.defaultHeaders };
|
|
370
|
+
return false;
|
|
380
371
|
}
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
372
|
+
/**
|
|
373
|
+
* Evaluates the access of a given key list to a user's public key.
|
|
374
|
+
* @param keyList The key list to evaluate access for.
|
|
375
|
+
* @param userPublicKey The public key of the user to evaluate access for.
|
|
376
|
+
* @returns A promise that resolves to true if the key list has access, false otherwise.
|
|
377
|
+
*/
|
|
378
|
+
async evaluateKeyList(keyList, userPublicKey) {
|
|
379
|
+
const keys = keyList.keys || [];
|
|
380
|
+
for (const listKey of keys) {
|
|
381
|
+
if (!listKey) continue;
|
|
382
|
+
if (listKey.ed25519) {
|
|
383
|
+
if (this.compareEd25519Key(listKey.ed25519, userPublicKey)) {
|
|
384
|
+
return true;
|
|
385
|
+
}
|
|
386
|
+
} else if (listKey.keyList || listKey.thresholdKey) {
|
|
387
|
+
try {
|
|
388
|
+
const nestedKeyBytes = proto.Key.encode({
|
|
389
|
+
...listKey.keyList ? { keyList: listKey.keyList } : {},
|
|
390
|
+
...listKey.thresholdKey ? { thresholdKey: listKey.thresholdKey } : {}
|
|
391
|
+
}).finish();
|
|
392
|
+
const hasNestedAccess = await this.checkKeyListAccess(
|
|
393
|
+
Buffer.from(nestedKeyBytes),
|
|
394
|
+
userPublicKey
|
|
395
|
+
);
|
|
396
|
+
if (hasNestedAccess) {
|
|
397
|
+
return true;
|
|
398
|
+
}
|
|
399
|
+
} catch (e) {
|
|
400
|
+
const error = e;
|
|
401
|
+
const logMessage = `Error in nested key: ${error.message}`;
|
|
402
|
+
this.logger.debug(logMessage);
|
|
403
|
+
}
|
|
404
|
+
}
|
|
384
405
|
}
|
|
385
|
-
|
|
386
|
-
}
|
|
387
|
-
async search(params = {}) {
|
|
388
|
-
const query = buildSearchQuery(params);
|
|
389
|
-
const raw = await this.requestJson(`/search${query}`, {
|
|
390
|
-
method: "GET"
|
|
391
|
-
});
|
|
392
|
-
return this.parseWithSchema(raw, searchResponseSchema, "search response");
|
|
393
|
-
}
|
|
394
|
-
async stats() {
|
|
395
|
-
const raw = await this.requestJson("/stats", { method: "GET" });
|
|
396
|
-
return this.parseWithSchema(raw, statsResponseSchema, "stats response");
|
|
406
|
+
return false;
|
|
397
407
|
}
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
408
|
+
/**
|
|
409
|
+
* Compares an Ed25519 key with a user's public key.
|
|
410
|
+
* @param keyData The Ed25519 key data to compare.
|
|
411
|
+
* @param userPublicKey The public key of the user to compare with.
|
|
412
|
+
* @returns A boolean indicating whether the key matches the user's public key.
|
|
413
|
+
*/
|
|
414
|
+
compareEd25519Key(keyData, userPublicKey) {
|
|
415
|
+
try {
|
|
416
|
+
const decodedKey = PublicKey.fromBytes(Buffer.from(keyData));
|
|
417
|
+
return decodedKey.toString() === userPublicKey.toString();
|
|
418
|
+
} catch (e) {
|
|
419
|
+
const error = e;
|
|
420
|
+
const logMessage = `Error comparing Ed25519 key: ${error.message}`;
|
|
421
|
+
this.logger.debug(logMessage);
|
|
422
|
+
return false;
|
|
423
|
+
}
|
|
407
424
|
}
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
425
|
+
/**
|
|
426
|
+
* Retrieves information about a scheduled transaction
|
|
427
|
+
* @param scheduleId The ID of the scheduled transaction
|
|
428
|
+
* @returns A promise that resolves to the scheduled transaction information
|
|
429
|
+
*/
|
|
430
|
+
async getScheduleInfo(scheduleId) {
|
|
431
|
+
try {
|
|
432
|
+
this.logger.info(
|
|
433
|
+
`Getting information for scheduled transaction ${scheduleId}`
|
|
434
|
+
);
|
|
435
|
+
const data = await this._requestWithRetry(
|
|
436
|
+
`/api/v1/schedules/${scheduleId}`
|
|
437
|
+
);
|
|
438
|
+
if (data) {
|
|
439
|
+
return data;
|
|
413
440
|
}
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
if (!options?.autoRegister || options.autoRegister.enabled === false) {
|
|
441
|
+
this.logger.warn(
|
|
442
|
+
`No schedule info found for ${scheduleId} after retries.`
|
|
443
|
+
);
|
|
444
|
+
return null;
|
|
445
|
+
} catch (error) {
|
|
446
|
+
this.logger.error(
|
|
447
|
+
`Error fetching schedule info for ${scheduleId} after retries: ${error.message}`
|
|
448
|
+
);
|
|
423
449
|
return null;
|
|
424
450
|
}
|
|
425
|
-
return this.autoRegisterEncryptionKey(options.autoRegister).then(
|
|
426
|
-
() => void 0
|
|
427
|
-
);
|
|
428
451
|
}
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
452
|
+
/**
|
|
453
|
+
* Checks the status of a scheduled transaction
|
|
454
|
+
* @param scheduleId The schedule ID to check
|
|
455
|
+
* @returns Status of the scheduled transaction
|
|
456
|
+
*/
|
|
457
|
+
async getScheduledTransactionStatus(scheduleId) {
|
|
458
|
+
try {
|
|
459
|
+
this.logger.info(
|
|
460
|
+
`Checking status of scheduled transaction ${scheduleId}`
|
|
434
461
|
);
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
462
|
+
const scheduleInfo = await this.getScheduleInfo(scheduleId);
|
|
463
|
+
if (!scheduleInfo) {
|
|
464
|
+
throw new Error(`Schedule ${scheduleId} not found`);
|
|
465
|
+
}
|
|
466
|
+
return {
|
|
467
|
+
executed: Boolean(scheduleInfo.executed_timestamp),
|
|
468
|
+
executedDate: scheduleInfo.executed_timestamp ? new Date(Number(scheduleInfo.executed_timestamp) * 1e3) : void 0,
|
|
469
|
+
deleted: scheduleInfo.deleted || false
|
|
470
|
+
};
|
|
471
|
+
} catch (error) {
|
|
472
|
+
this.logger.error(
|
|
473
|
+
`Error checking scheduled transaction status: ${error}`
|
|
440
474
|
);
|
|
475
|
+
throw error;
|
|
441
476
|
}
|
|
442
|
-
await this.registerEncryptionKey({
|
|
443
|
-
keyType: config.keyType ?? "secp256k1",
|
|
444
|
-
publicKey: material.publicKey,
|
|
445
|
-
...identity
|
|
446
|
-
});
|
|
447
|
-
return material;
|
|
448
477
|
}
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
478
|
+
/**
|
|
479
|
+
* Retrieves details for a given transaction ID or hash from the mirror node.
|
|
480
|
+
* @param transactionIdOrHash The ID or hash of the transaction.
|
|
481
|
+
* @returns A promise that resolves to the transaction details.
|
|
482
|
+
* @throws An error if the transaction ID/hash is invalid or details cannot be retrieved.
|
|
483
|
+
*/
|
|
484
|
+
async getTransaction(transactionIdOrHash) {
|
|
485
|
+
this.logger.info(
|
|
486
|
+
`Getting transaction details for ID/hash: ${transactionIdOrHash}`
|
|
487
|
+
);
|
|
488
|
+
try {
|
|
489
|
+
const response = await this._requestWithRetry(`/api/v1/transactions/${transactionIdOrHash}`);
|
|
490
|
+
if (response?.transactions?.length > 0) {
|
|
491
|
+
this.logger.trace(
|
|
492
|
+
`Transaction details found for ${transactionIdOrHash}:`,
|
|
493
|
+
response.transactions[0]
|
|
494
|
+
);
|
|
495
|
+
return response.transactions[0];
|
|
458
496
|
}
|
|
497
|
+
this.logger.warn(
|
|
498
|
+
`No transaction details found for ${transactionIdOrHash} or unexpected response structure.`
|
|
499
|
+
);
|
|
500
|
+
return null;
|
|
501
|
+
} catch (e) {
|
|
502
|
+
const error = e;
|
|
503
|
+
this.logger.error(
|
|
504
|
+
`Failed to get transaction details for ${transactionIdOrHash} after retries: ${error.message}`
|
|
505
|
+
);
|
|
506
|
+
return null;
|
|
459
507
|
}
|
|
460
|
-
if (config.email) {
|
|
461
|
-
identity.email = config.email;
|
|
462
|
-
}
|
|
463
|
-
if (identity.uaid || identity.ledgerAccountId || identity.email) {
|
|
464
|
-
return identity;
|
|
465
|
-
}
|
|
466
|
-
return null;
|
|
467
508
|
}
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
let
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
envVar,
|
|
481
|
-
envPath: config.envPath,
|
|
482
|
-
overwrite: config.overwriteEnv
|
|
483
|
-
});
|
|
484
|
-
if (envVar) {
|
|
485
|
-
process.env[envVar] = pair.privateKey;
|
|
509
|
+
/**
|
|
510
|
+
* Private helper to make GET requests with retry logic using Axios.
|
|
511
|
+
*/
|
|
512
|
+
async _requestWithRetry(endpoint, axiosConfig) {
|
|
513
|
+
let attempt = 0;
|
|
514
|
+
let delay = this.initialDelayMs;
|
|
515
|
+
const url = this.constructUrl(endpoint);
|
|
516
|
+
const config = {
|
|
517
|
+
...axiosConfig,
|
|
518
|
+
headers: {
|
|
519
|
+
...this.customHeaders,
|
|
520
|
+
...axiosConfig?.headers
|
|
486
521
|
}
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
522
|
+
};
|
|
523
|
+
if (this.apiKey) {
|
|
524
|
+
config.headers = {
|
|
525
|
+
...config.headers,
|
|
526
|
+
Authorization: `Bearer ${this.apiKey}`,
|
|
527
|
+
"X-API-Key": this.apiKey
|
|
528
|
+
};
|
|
492
529
|
}
|
|
493
|
-
|
|
494
|
-
}
|
|
495
|
-
derivePublicKeyFromPrivateKey(privateKey) {
|
|
496
|
-
const normalized = this.hexToBuffer(privateKey);
|
|
497
|
-
const publicKey = secp256k1.getPublicKey(normalized, true);
|
|
498
|
-
return Buffer.from(publicKey).toString("hex");
|
|
499
|
-
}
|
|
500
|
-
ensureAgentEncryptionKey(options) {
|
|
501
|
-
return this.autoRegisterEncryptionKey({
|
|
502
|
-
...options,
|
|
503
|
-
uaid: options.uaid,
|
|
504
|
-
enabled: true
|
|
505
|
-
});
|
|
506
|
-
}
|
|
507
|
-
async popularSearches() {
|
|
508
|
-
const raw = await this.requestJson("/popular", {
|
|
509
|
-
method: "GET"
|
|
510
|
-
});
|
|
511
|
-
return this.parseWithSchema(
|
|
512
|
-
raw,
|
|
513
|
-
popularResponseSchema,
|
|
514
|
-
"popular searches response"
|
|
515
|
-
);
|
|
516
|
-
}
|
|
517
|
-
async resolveUaid(uaid) {
|
|
518
|
-
const raw = await this.requestJson(
|
|
519
|
-
`/resolve/${encodeURIComponent(uaid)}`,
|
|
520
|
-
{
|
|
521
|
-
method: "GET"
|
|
522
|
-
}
|
|
523
|
-
);
|
|
524
|
-
return this.parseWithSchema(
|
|
525
|
-
raw,
|
|
526
|
-
resolveResponseSchema,
|
|
527
|
-
"resolve UAID response"
|
|
528
|
-
);
|
|
529
|
-
}
|
|
530
|
-
async performRegisterAgent(payload) {
|
|
531
|
-
const raw = await this.requestJson("/register", {
|
|
532
|
-
method: "POST",
|
|
533
|
-
body: serialiseAgentRegistrationRequest(payload),
|
|
534
|
-
headers: { "content-type": "application/json" }
|
|
535
|
-
});
|
|
536
|
-
return this.parseWithSchema(
|
|
537
|
-
raw,
|
|
538
|
-
registerAgentResponseSchema,
|
|
539
|
-
"register agent response"
|
|
540
|
-
);
|
|
541
|
-
}
|
|
542
|
-
async registerAgent(payload, options) {
|
|
543
|
-
const autoTopUp = options?.autoTopUp ?? this.registrationAutoTopUp;
|
|
544
|
-
if (!autoTopUp) {
|
|
545
|
-
return this.performRegisterAgent(payload);
|
|
546
|
-
}
|
|
547
|
-
await this.ensureCreditsForRegistration(payload, autoTopUp);
|
|
548
|
-
let retried = false;
|
|
549
|
-
while (true) {
|
|
530
|
+
while (attempt < this.maxRetries) {
|
|
550
531
|
try {
|
|
551
|
-
|
|
532
|
+
const response = await axios.get(url, config);
|
|
533
|
+
return response.data;
|
|
552
534
|
} catch (error) {
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
535
|
+
attempt++;
|
|
536
|
+
const isLastAttempt = attempt >= this.maxRetries;
|
|
537
|
+
const statusCode = error.response?.status;
|
|
538
|
+
if (statusCode && statusCode > 404 && statusCode < 500 && statusCode !== 429) {
|
|
539
|
+
this.logger.error(
|
|
540
|
+
`Client error for ${url} (status ${statusCode}): ${error.message}. Not retrying.`
|
|
541
|
+
);
|
|
542
|
+
throw error;
|
|
543
|
+
}
|
|
544
|
+
if (isLastAttempt) {
|
|
545
|
+
this.logger.error(
|
|
546
|
+
`Max retries (${this.maxRetries}) reached for ${url}. Last error: ${error.message}`
|
|
547
|
+
);
|
|
548
|
+
throw error;
|
|
558
549
|
}
|
|
559
|
-
|
|
550
|
+
this.logger.warn(
|
|
551
|
+
`Attempt ${attempt}/${this.maxRetries} failed for ${url}: ${error.message}. Retrying in ${delay}ms...`
|
|
552
|
+
);
|
|
553
|
+
await new Promise((resolve) => setTimeout(resolve, delay));
|
|
554
|
+
delay = Math.min(delay * this.backoffFactor, this.maxDelayMs);
|
|
560
555
|
}
|
|
561
556
|
}
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
const raw = await this.requestJson("/register/quote", {
|
|
565
|
-
method: "POST",
|
|
566
|
-
body: serialiseAgentRegistrationRequest(payload),
|
|
567
|
-
headers: { "content-type": "application/json" }
|
|
568
|
-
});
|
|
569
|
-
return this.parseWithSchema(
|
|
570
|
-
raw,
|
|
571
|
-
registrationQuoteResponseSchema,
|
|
572
|
-
"registration quote response"
|
|
557
|
+
throw new Error(
|
|
558
|
+
`Failed to fetch data from ${url} after ${this.maxRetries} attempts.`
|
|
573
559
|
);
|
|
574
560
|
}
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
561
|
+
/**
|
|
562
|
+
* Private helper to make fetch requests with retry logic.
|
|
563
|
+
*/
|
|
564
|
+
async _fetchWithRetry(url, fetchOptions) {
|
|
565
|
+
let attempt = 0;
|
|
566
|
+
let delay = this.initialDelayMs;
|
|
567
|
+
const headers = {
|
|
568
|
+
...this.customHeaders
|
|
569
|
+
};
|
|
570
|
+
if (fetchOptions?.headers) {
|
|
571
|
+
if (fetchOptions.headers instanceof Headers) {
|
|
572
|
+
fetchOptions.headers.forEach((value, key) => {
|
|
573
|
+
headers[key] = value;
|
|
574
|
+
});
|
|
575
|
+
} else if (Array.isArray(fetchOptions.headers)) {
|
|
576
|
+
fetchOptions.headers.forEach(([key, value]) => {
|
|
577
|
+
headers[key] = value;
|
|
578
|
+
});
|
|
579
|
+
} else {
|
|
580
|
+
Object.assign(headers, fetchOptions.headers);
|
|
582
581
|
}
|
|
583
|
-
);
|
|
584
|
-
return this.parseWithSchema(
|
|
585
|
-
raw,
|
|
586
|
-
registerAgentResponseSchema,
|
|
587
|
-
"update agent response"
|
|
588
|
-
);
|
|
589
|
-
}
|
|
590
|
-
async getRegistrationProgress(attemptId) {
|
|
591
|
-
const normalisedAttemptId = attemptId.trim();
|
|
592
|
-
if (!normalisedAttemptId) {
|
|
593
|
-
throw new Error("attemptId is required");
|
|
594
582
|
}
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
{ method: "GET" }
|
|
599
|
-
);
|
|
600
|
-
const parsed = this.parseWithSchema(
|
|
601
|
-
raw,
|
|
602
|
-
registrationProgressResponseSchema,
|
|
603
|
-
"registration progress response"
|
|
604
|
-
);
|
|
605
|
-
return parsed.progress;
|
|
606
|
-
} catch (error) {
|
|
607
|
-
if (error instanceof RegistryBrokerError && error.status === 404) {
|
|
608
|
-
return null;
|
|
609
|
-
}
|
|
610
|
-
throw error;
|
|
583
|
+
if (this.apiKey) {
|
|
584
|
+
headers["Authorization"] = `Bearer ${this.apiKey}`;
|
|
585
|
+
headers["X-API-Key"] = this.apiKey;
|
|
611
586
|
}
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
while (true) {
|
|
627
|
-
if (signal?.aborted) {
|
|
628
|
-
throw createAbortError();
|
|
629
|
-
}
|
|
630
|
-
const progress = await this.getRegistrationProgress(normalisedAttemptId);
|
|
631
|
-
if (progress) {
|
|
632
|
-
options.onProgress?.(progress);
|
|
633
|
-
if (progress.status === "completed") {
|
|
634
|
-
return progress;
|
|
635
|
-
}
|
|
636
|
-
if (progress.status === "partial" || progress.status === "failed") {
|
|
637
|
-
if (throwOnFailure) {
|
|
638
|
-
throw new RegistryBrokerError(
|
|
639
|
-
"Registration did not complete successfully",
|
|
640
|
-
{
|
|
641
|
-
status: 409,
|
|
642
|
-
statusText: progress.status,
|
|
643
|
-
body: progress
|
|
644
|
-
}
|
|
587
|
+
const options = {
|
|
588
|
+
...fetchOptions,
|
|
589
|
+
headers
|
|
590
|
+
};
|
|
591
|
+
while (attempt < this.maxRetries) {
|
|
592
|
+
try {
|
|
593
|
+
const request = await fetch(url, options);
|
|
594
|
+
if (!request.ok) {
|
|
595
|
+
if (request.status >= 400 && request.status < 500 && request.status !== 429) {
|
|
596
|
+
this.logger.error(
|
|
597
|
+
`Client error for ${url} (status ${request.status}): ${request.statusText}. Not retrying.`
|
|
598
|
+
);
|
|
599
|
+
throw new Error(
|
|
600
|
+
`Fetch failed with status ${request.status}: ${request.statusText} for URL: ${url}`
|
|
645
601
|
);
|
|
646
602
|
}
|
|
647
|
-
|
|
603
|
+
throw new Error(
|
|
604
|
+
`Fetch failed with status ${request.status}: ${request.statusText} for URL: ${url}`
|
|
605
|
+
);
|
|
648
606
|
}
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
|
|
607
|
+
const response = await request.json();
|
|
608
|
+
return response;
|
|
609
|
+
} catch (error) {
|
|
610
|
+
attempt++;
|
|
611
|
+
if (attempt >= this.maxRetries) {
|
|
612
|
+
this.logger.error(
|
|
613
|
+
`Max retries (${this.maxRetries}) reached for ${url}. Last error: ${error.message}`
|
|
614
|
+
);
|
|
615
|
+
throw error;
|
|
616
|
+
}
|
|
617
|
+
this.logger.warn(
|
|
618
|
+
`Attempt ${attempt}/${this.maxRetries} failed for ${url}: ${error.message}. Retrying in ${delay}ms...`
|
|
653
619
|
);
|
|
620
|
+
await new Promise((resolve) => setTimeout(resolve, delay));
|
|
621
|
+
delay = Math.min(delay * this.backoffFactor, this.maxDelayMs);
|
|
654
622
|
}
|
|
655
|
-
await this.delay(interval, signal);
|
|
656
623
|
}
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
const body = {
|
|
660
|
-
accountId: params.accountId,
|
|
661
|
-
payerKey: params.privateKey,
|
|
662
|
-
hbarAmount: this.calculateHbarAmountParam(params.hbarAmount)
|
|
663
|
-
};
|
|
664
|
-
if (params.memo) {
|
|
665
|
-
body.memo = params.memo;
|
|
666
|
-
}
|
|
667
|
-
if (params.metadata) {
|
|
668
|
-
body.metadata = params.metadata;
|
|
669
|
-
}
|
|
670
|
-
const raw = await this.requestJson("/credits/purchase", {
|
|
671
|
-
method: "POST",
|
|
672
|
-
headers: { "content-type": "application/json" },
|
|
673
|
-
body
|
|
674
|
-
});
|
|
675
|
-
return this.parseWithSchema(
|
|
676
|
-
raw,
|
|
677
|
-
creditPurchaseResponseSchema,
|
|
678
|
-
"credit purchase response"
|
|
624
|
+
throw new Error(
|
|
625
|
+
`Failed to fetch data from ${url} after ${this.maxRetries} attempts.`
|
|
679
626
|
);
|
|
680
627
|
}
|
|
681
|
-
|
|
682
|
-
|
|
683
|
-
|
|
684
|
-
|
|
685
|
-
|
|
686
|
-
|
|
687
|
-
|
|
688
|
-
|
|
689
|
-
|
|
690
|
-
|
|
691
|
-
|
|
692
|
-
|
|
693
|
-
if (!Number.isFinite(params.credits) || params.credits <= 0) {
|
|
694
|
-
throw new Error("credits must be a positive number");
|
|
695
|
-
}
|
|
696
|
-
if (params.usdAmount !== void 0 && (!Number.isFinite(params.usdAmount) || params.usdAmount <= 0)) {
|
|
697
|
-
throw new Error("usdAmount must be a positive number when provided");
|
|
698
|
-
}
|
|
699
|
-
const body = {
|
|
700
|
-
accountId: params.accountId,
|
|
701
|
-
credits: params.credits
|
|
702
|
-
};
|
|
703
|
-
if (params.usdAmount !== void 0) {
|
|
704
|
-
body.usdAmount = params.usdAmount;
|
|
705
|
-
}
|
|
706
|
-
if (params.description) {
|
|
707
|
-
body.description = params.description;
|
|
708
|
-
}
|
|
709
|
-
if (params.metadata) {
|
|
710
|
-
body.metadata = params.metadata;
|
|
711
|
-
}
|
|
712
|
-
const axiosClient = axios.create({
|
|
713
|
-
baseURL: this.baseUrl,
|
|
714
|
-
headers: {
|
|
715
|
-
...this.getDefaultHeaders(),
|
|
716
|
-
"content-type": "application/json"
|
|
628
|
+
/**
|
|
629
|
+
* Retrieves the numerical balance (in HBAR) for a given account ID.
|
|
630
|
+
* @param accountId The ID of the account.
|
|
631
|
+
* @returns A promise that resolves to the HBAR balance or null if an error occurs.
|
|
632
|
+
*/
|
|
633
|
+
async getAccountBalance(accountId) {
|
|
634
|
+
this.logger.info(`Getting balance for account ${accountId}`);
|
|
635
|
+
try {
|
|
636
|
+
const accountInfo = await this.requestAccount(accountId);
|
|
637
|
+
if (accountInfo && accountInfo.balance) {
|
|
638
|
+
const hbarBalance = accountInfo.balance.balance / 1e8;
|
|
639
|
+
return hbarBalance;
|
|
717
640
|
}
|
|
718
|
-
|
|
719
|
-
|
|
720
|
-
|
|
721
|
-
|
|
722
|
-
)
|
|
723
|
-
|
|
724
|
-
|
|
725
|
-
|
|
726
|
-
|
|
727
|
-
"x402 credit purchase response"
|
|
728
|
-
);
|
|
729
|
-
const paymentHeader = typeof response.headers["x-payment-response"] === "string" ? response.headers["x-payment-response"] : void 0;
|
|
730
|
-
const decodedPayment = paymentHeader !== void 0 ? decodeXPaymentResponse(paymentHeader) : void 0;
|
|
731
|
-
return {
|
|
732
|
-
...parsed,
|
|
733
|
-
paymentResponseHeader: paymentHeader,
|
|
734
|
-
paymentResponse: decodedPayment
|
|
735
|
-
};
|
|
736
|
-
}
|
|
737
|
-
async buyCreditsWithX402(params) {
|
|
738
|
-
const network = params.network ?? "base";
|
|
739
|
-
const normalizedKey = normalizeHexPrivateKey(params.evmPrivateKey);
|
|
740
|
-
const walletClient = await createSigner(network, normalizedKey);
|
|
741
|
-
return this.purchaseCreditsWithX402({
|
|
742
|
-
accountId: params.accountId,
|
|
743
|
-
credits: params.credits,
|
|
744
|
-
usdAmount: params.usdAmount,
|
|
745
|
-
description: params.description,
|
|
746
|
-
metadata: params.metadata,
|
|
747
|
-
walletClient
|
|
748
|
-
});
|
|
749
|
-
}
|
|
750
|
-
calculateHbarAmount(creditsToPurchase, creditsPerHbar) {
|
|
751
|
-
if (creditsPerHbar <= 0) {
|
|
752
|
-
throw new Error("creditsPerHbar must be positive");
|
|
753
|
-
}
|
|
754
|
-
if (creditsToPurchase <= 0) {
|
|
755
|
-
throw new Error("creditsToPurchase must be positive");
|
|
641
|
+
this.logger.warn(
|
|
642
|
+
`Could not retrieve balance for account ${accountId} from account info.`
|
|
643
|
+
);
|
|
644
|
+
return null;
|
|
645
|
+
} catch (error) {
|
|
646
|
+
this.logger.error(
|
|
647
|
+
`Error fetching numerical balance for account ${accountId}: ${error.message}`
|
|
648
|
+
);
|
|
649
|
+
return null;
|
|
756
650
|
}
|
|
757
|
-
const rawHbar = creditsToPurchase / creditsPerHbar;
|
|
758
|
-
const tinybars = Math.ceil(rawHbar * 1e8);
|
|
759
|
-
return tinybars / 1e8;
|
|
760
651
|
}
|
|
761
|
-
|
|
762
|
-
|
|
763
|
-
|
|
764
|
-
|
|
765
|
-
|
|
766
|
-
|
|
767
|
-
|
|
652
|
+
/**
|
|
653
|
+
* Retrieves messages for a given topic ID with optional filters.
|
|
654
|
+
* @param topicId The ID of the topic.
|
|
655
|
+
* @param sequenceNumber Filter by sequence number (e.g., "gt:10", "lte:20").
|
|
656
|
+
* @param startTime Filter by consensus timestamp (e.g., "gt:1629400000.000000000").
|
|
657
|
+
* @param endTime Filter by consensus timestamp (e.g., "lt:1629500000.000000000").
|
|
658
|
+
* @param limit The maximum number of messages to return.
|
|
659
|
+
* @returns A promise that resolves to an array of HCSMessages or null.
|
|
660
|
+
*/
|
|
661
|
+
async getTopicMessagesByFilter(topicId, options) {
|
|
662
|
+
this.logger.trace(
|
|
663
|
+
`Querying messages for topic ${topicId} with filters: ${JSON.stringify(
|
|
664
|
+
options
|
|
665
|
+
)}`
|
|
768
666
|
);
|
|
769
|
-
|
|
770
|
-
|
|
771
|
-
|
|
772
|
-
|
|
773
|
-
throw new Error("Calculated purchase amount must be positive");
|
|
667
|
+
let nextUrl = `/api/v1/topics/${topicId}/messages`;
|
|
668
|
+
const params = new URLSearchParams();
|
|
669
|
+
if (options?.limit) {
|
|
670
|
+
params.append("limit", options.limit.toString());
|
|
774
671
|
}
|
|
775
|
-
|
|
776
|
-
|
|
777
|
-
shouldAutoTopUpHistory(payload, error) {
|
|
778
|
-
if (!this.historyAutoTopUp || payload.historyTtlSeconds === void 0) {
|
|
779
|
-
return false;
|
|
672
|
+
if (options?.sequenceNumber) {
|
|
673
|
+
params.append("sequencenumber", options.sequenceNumber);
|
|
780
674
|
}
|
|
781
|
-
if (
|
|
782
|
-
|
|
675
|
+
if (options?.startTime) {
|
|
676
|
+
params.append("timestamp", `gte:${options.startTime}`);
|
|
783
677
|
}
|
|
784
|
-
if (
|
|
785
|
-
|
|
678
|
+
if (options?.endTime) {
|
|
679
|
+
params.append("timestamp", `lt:${options.endTime}`);
|
|
786
680
|
}
|
|
787
|
-
|
|
788
|
-
|
|
789
|
-
return true;
|
|
681
|
+
if (options?.order) {
|
|
682
|
+
params.append("order", options.order);
|
|
790
683
|
}
|
|
791
|
-
const
|
|
792
|
-
|
|
793
|
-
|
|
794
|
-
extractErrorMessage(body) {
|
|
795
|
-
if (typeof body === "string") {
|
|
796
|
-
return body;
|
|
684
|
+
const queryString = params.toString();
|
|
685
|
+
if (queryString) {
|
|
686
|
+
nextUrl += `?${queryString}`;
|
|
797
687
|
}
|
|
798
|
-
|
|
799
|
-
|
|
800
|
-
|
|
801
|
-
|
|
802
|
-
|
|
803
|
-
|
|
804
|
-
|
|
805
|
-
|
|
806
|
-
|
|
807
|
-
|
|
808
|
-
|
|
809
|
-
|
|
810
|
-
|
|
811
|
-
|
|
812
|
-
|
|
813
|
-
|
|
814
|
-
|
|
815
|
-
|
|
816
|
-
|
|
817
|
-
|
|
818
|
-
|
|
819
|
-
|
|
820
|
-
|
|
821
|
-
|
|
822
|
-
|
|
823
|
-
|
|
824
|
-
|
|
825
|
-
|
|
826
|
-
|
|
827
|
-
|
|
828
|
-
|
|
829
|
-
|
|
830
|
-
|
|
831
|
-
|
|
832
|
-
|
|
833
|
-
|
|
834
|
-
|
|
835
|
-
|
|
836
|
-
|
|
837
|
-
|
|
838
|
-
|
|
839
|
-
|
|
840
|
-
|
|
841
|
-
|
|
842
|
-
|
|
843
|
-
|
|
844
|
-
|
|
845
|
-
|
|
688
|
+
const messages = [];
|
|
689
|
+
let pagesFetched = 0;
|
|
690
|
+
const maxPages = 10;
|
|
691
|
+
try {
|
|
692
|
+
while (nextUrl && pagesFetched < maxPages) {
|
|
693
|
+
pagesFetched++;
|
|
694
|
+
const data = await this._requestWithRetry(nextUrl);
|
|
695
|
+
if (data.messages && data.messages.length > 0) {
|
|
696
|
+
for (const message of data.messages) {
|
|
697
|
+
try {
|
|
698
|
+
if (!message.message) {
|
|
699
|
+
continue;
|
|
700
|
+
}
|
|
701
|
+
let messageContent;
|
|
702
|
+
if (this.isServerEnvironment) {
|
|
703
|
+
messageContent = Buffer.from(
|
|
704
|
+
message.message,
|
|
705
|
+
"base64"
|
|
706
|
+
).toString("utf-8");
|
|
707
|
+
} else {
|
|
708
|
+
messageContent = new TextDecoder().decode(
|
|
709
|
+
Uint8Array.from(atob(message.message), (c) => c.charCodeAt(0))
|
|
710
|
+
);
|
|
711
|
+
}
|
|
712
|
+
let messageJson = {};
|
|
713
|
+
try {
|
|
714
|
+
messageJson = JSON.parse(messageContent);
|
|
715
|
+
} catch (parseError) {
|
|
716
|
+
this.logger.debug(
|
|
717
|
+
`Message content is not valid JSON, using raw: ${messageContent}`
|
|
718
|
+
);
|
|
719
|
+
messageJson = { raw_content: messageContent };
|
|
720
|
+
}
|
|
721
|
+
const parsedContent = messageJson;
|
|
722
|
+
const hcsMsg = {
|
|
723
|
+
...parsedContent,
|
|
724
|
+
consensus_timestamp: message.consensus_timestamp,
|
|
725
|
+
sequence_number: message.sequence_number,
|
|
726
|
+
payer_account_id: message.payer_account_id,
|
|
727
|
+
topic_id: message.topic_id,
|
|
728
|
+
running_hash: message.running_hash,
|
|
729
|
+
running_hash_version: message.running_hash_version,
|
|
730
|
+
chunk_info: message.chunk_info,
|
|
731
|
+
created: new Date(
|
|
732
|
+
Number(message.consensus_timestamp.split(".")[0]) * 1e3 + Number(message.consensus_timestamp.split(".")[1] || 0) / 1e6
|
|
733
|
+
),
|
|
734
|
+
payer: message.payer_account_id
|
|
735
|
+
};
|
|
736
|
+
messages.push(hcsMsg);
|
|
737
|
+
} catch (error) {
|
|
738
|
+
this.logger.error(
|
|
739
|
+
`Error processing individual message: ${error.message}`
|
|
740
|
+
);
|
|
741
|
+
}
|
|
742
|
+
}
|
|
743
|
+
}
|
|
744
|
+
if (options?.limit && messages.length >= options.limit) break;
|
|
745
|
+
nextUrl = data.links?.next ? `${data.links.next}` : "";
|
|
846
746
|
}
|
|
847
|
-
|
|
848
|
-
|
|
849
|
-
|
|
747
|
+
return messages;
|
|
748
|
+
} catch (e) {
|
|
749
|
+
const error = e;
|
|
750
|
+
this.logger.error(
|
|
751
|
+
`Error querying filtered topic messages for ${topicId}: ${error.message}`
|
|
850
752
|
);
|
|
851
|
-
await this.purchaseCreditsWithHbar({
|
|
852
|
-
accountId: details.accountId.trim(),
|
|
853
|
-
privateKey: details.privateKey.trim(),
|
|
854
|
-
hbarAmount,
|
|
855
|
-
memo: details.memo ?? "Registry Broker auto top-up",
|
|
856
|
-
metadata: {
|
|
857
|
-
shortfallCredits: shortfall,
|
|
858
|
-
requiredCredits: quote.requiredCredits,
|
|
859
|
-
purchasedCredits: creditsToPurchase
|
|
860
|
-
}
|
|
861
|
-
});
|
|
862
|
-
}
|
|
863
|
-
const finalQuote = await this.getRegistrationQuote(payload);
|
|
864
|
-
if ((finalQuote.shortfallCredits ?? 0) > 0) {
|
|
865
|
-
throw new Error("Unable to purchase sufficient credits for registration");
|
|
866
|
-
}
|
|
867
|
-
}
|
|
868
|
-
extractInsufficientCreditsDetails(error) {
|
|
869
|
-
if (!(error instanceof RegistryBrokerError) || error.status !== 402) {
|
|
870
|
-
return null;
|
|
871
|
-
}
|
|
872
|
-
const body = error.body;
|
|
873
|
-
if (!body || typeof body !== "object" || Array.isArray(body)) {
|
|
874
|
-
return null;
|
|
875
|
-
}
|
|
876
|
-
const maybeShortfall = body["shortfallCredits"];
|
|
877
|
-
if (typeof maybeShortfall !== "number" || maybeShortfall <= 0) {
|
|
878
753
|
return null;
|
|
879
754
|
}
|
|
880
|
-
return { shortfallCredits: maybeShortfall };
|
|
881
|
-
}
|
|
882
|
-
async createLedgerChallenge(payload) {
|
|
883
|
-
const resolvedNetwork = canonicalizeLedgerNetwork(payload.network);
|
|
884
|
-
const network = resolvedNetwork.kind === "hedera" ? resolvedNetwork.hederaNetwork ?? resolvedNetwork.canonical : resolvedNetwork.canonical;
|
|
885
|
-
const raw = await this.requestJson("/auth/ledger/challenge", {
|
|
886
|
-
method: "POST",
|
|
887
|
-
headers: { "content-type": "application/json" },
|
|
888
|
-
body: {
|
|
889
|
-
accountId: payload.accountId,
|
|
890
|
-
network
|
|
891
|
-
}
|
|
892
|
-
});
|
|
893
|
-
return this.parseWithSchema(
|
|
894
|
-
raw,
|
|
895
|
-
ledgerChallengeResponseSchema,
|
|
896
|
-
"ledger challenge response"
|
|
897
|
-
);
|
|
898
|
-
}
|
|
899
|
-
async verifyLedgerChallenge(payload) {
|
|
900
|
-
const resolvedNetwork = canonicalizeLedgerNetwork(payload.network);
|
|
901
|
-
const network = resolvedNetwork.kind === "hedera" ? resolvedNetwork.hederaNetwork ?? resolvedNetwork.canonical : resolvedNetwork.canonical;
|
|
902
|
-
const body = {
|
|
903
|
-
challengeId: payload.challengeId,
|
|
904
|
-
accountId: payload.accountId,
|
|
905
|
-
network,
|
|
906
|
-
signature: payload.signature
|
|
907
|
-
};
|
|
908
|
-
if (payload.signatureKind) {
|
|
909
|
-
body.signatureKind = payload.signatureKind;
|
|
910
|
-
}
|
|
911
|
-
if (payload.publicKey) {
|
|
912
|
-
body.publicKey = payload.publicKey;
|
|
913
|
-
}
|
|
914
|
-
if (typeof payload.expiresInMinutes === "number") {
|
|
915
|
-
body.expiresInMinutes = payload.expiresInMinutes;
|
|
916
|
-
}
|
|
917
|
-
const raw = await this.requestJson("/auth/ledger/verify", {
|
|
918
|
-
method: "POST",
|
|
919
|
-
headers: { "content-type": "application/json" },
|
|
920
|
-
body
|
|
921
|
-
});
|
|
922
|
-
const result = this.parseWithSchema(
|
|
923
|
-
raw,
|
|
924
|
-
ledgerVerifyResponseSchema,
|
|
925
|
-
"ledger verification response"
|
|
926
|
-
);
|
|
927
|
-
this.setLedgerApiKey(result.key);
|
|
928
|
-
return result;
|
|
929
|
-
}
|
|
930
|
-
async authenticateWithLedger(options) {
|
|
931
|
-
const challenge = await this.createLedgerChallenge({
|
|
932
|
-
accountId: options.accountId,
|
|
933
|
-
network: options.network
|
|
934
|
-
});
|
|
935
|
-
const signed = await this.resolveLedgerAuthSignature(
|
|
936
|
-
challenge.message,
|
|
937
|
-
options
|
|
938
|
-
);
|
|
939
|
-
const verification = await this.verifyLedgerChallenge({
|
|
940
|
-
challengeId: challenge.challengeId,
|
|
941
|
-
accountId: options.accountId,
|
|
942
|
-
network: options.network,
|
|
943
|
-
signature: signed.signature,
|
|
944
|
-
signatureKind: signed.signatureKind,
|
|
945
|
-
publicKey: signed.publicKey,
|
|
946
|
-
expiresInMinutes: options.expiresInMinutes
|
|
947
|
-
});
|
|
948
|
-
return verification;
|
|
949
755
|
}
|
|
950
|
-
|
|
951
|
-
|
|
952
|
-
|
|
953
|
-
|
|
954
|
-
|
|
756
|
+
/**
|
|
757
|
+
* Retrieves token balances for a given account ID.
|
|
758
|
+
* @param accountId The ID of the account.
|
|
759
|
+
* @param limit The maximum number of tokens to return.
|
|
760
|
+
* @returns A promise that resolves to an array of AccountTokenBalance or null.
|
|
761
|
+
*/
|
|
762
|
+
async getAccountTokens(accountId, limit = 100) {
|
|
763
|
+
this.logger.info(`Getting tokens for account ${accountId}`);
|
|
764
|
+
let allTokens = [];
|
|
765
|
+
let endpoint = `/api/v1/accounts/${accountId}/tokens?limit=${limit}`;
|
|
766
|
+
try {
|
|
767
|
+
for (let i = 0; i < 10 && endpoint; i++) {
|
|
768
|
+
const response = await this._requestWithRetry(endpoint);
|
|
769
|
+
if (response && response.tokens) {
|
|
770
|
+
allTokens = allTokens.concat(response.tokens);
|
|
771
|
+
}
|
|
772
|
+
endpoint = response.links?.next || "";
|
|
773
|
+
if (!endpoint || limit && allTokens.length >= limit) {
|
|
774
|
+
if (limit && allTokens.length > limit) {
|
|
775
|
+
allTokens = allTokens.slice(0, limit);
|
|
776
|
+
}
|
|
777
|
+
break;
|
|
778
|
+
}
|
|
955
779
|
}
|
|
956
|
-
return
|
|
957
|
-
}
|
|
958
|
-
|
|
959
|
-
|
|
960
|
-
"Ledger authentication requires a Hedera Signer or custom sign function."
|
|
780
|
+
return allTokens;
|
|
781
|
+
} catch (error) {
|
|
782
|
+
this.logger.error(
|
|
783
|
+
`Error fetching tokens for account ${accountId}: ${error.message}`
|
|
961
784
|
);
|
|
785
|
+
return null;
|
|
962
786
|
}
|
|
963
|
-
const payload = Buffer.from(message, "utf8");
|
|
964
|
-
const signatures = await options.signer.sign([payload]);
|
|
965
|
-
const signatureEntry = signatures?.[0];
|
|
966
|
-
if (!signatureEntry) {
|
|
967
|
-
throw new Error("Signer did not return any signatures.");
|
|
968
|
-
}
|
|
969
|
-
let derivedPublicKey;
|
|
970
|
-
if (signatureEntry.publicKey) {
|
|
971
|
-
derivedPublicKey = signatureEntry.publicKey.toString();
|
|
972
|
-
} else if (typeof options.signer.getAccountKey === "function") {
|
|
973
|
-
const accountKey = await options.signer.getAccountKey();
|
|
974
|
-
if (accountKey && typeof accountKey.toString === "function") {
|
|
975
|
-
derivedPublicKey = accountKey.toString();
|
|
976
|
-
}
|
|
977
|
-
}
|
|
978
|
-
return {
|
|
979
|
-
signature: Buffer.from(signatureEntry.signature).toString("base64"),
|
|
980
|
-
signatureKind: "raw",
|
|
981
|
-
publicKey: derivedPublicKey
|
|
982
|
-
};
|
|
983
787
|
}
|
|
984
|
-
|
|
985
|
-
|
|
986
|
-
|
|
987
|
-
|
|
988
|
-
|
|
989
|
-
|
|
990
|
-
|
|
991
|
-
|
|
992
|
-
|
|
993
|
-
|
|
994
|
-
|
|
995
|
-
logger
|
|
996
|
-
|
|
997
|
-
const resolvedNetwork = canonicalizeLedgerNetwork(network);
|
|
998
|
-
const labelSuffix = label ? ` for ${label}` : "";
|
|
999
|
-
const networkPayload = resolvedNetwork.canonical;
|
|
1000
|
-
const authOptions = {
|
|
1001
|
-
accountId,
|
|
1002
|
-
network: networkPayload,
|
|
1003
|
-
expiresInMinutes
|
|
1004
|
-
};
|
|
1005
|
-
if (sign) {
|
|
1006
|
-
authOptions.sign = sign;
|
|
1007
|
-
} else if (signer) {
|
|
1008
|
-
authOptions.signer = signer;
|
|
1009
|
-
} else if (hederaPrivateKey) {
|
|
1010
|
-
if (resolvedNetwork.kind !== "hedera" || !resolvedNetwork.hederaNetwork) {
|
|
1011
|
-
throw new Error(
|
|
1012
|
-
"hederaPrivateKey can only be used with hedera:mainnet or hedera:testnet networks."
|
|
1013
|
-
);
|
|
1014
|
-
}
|
|
1015
|
-
authOptions.signer = createPrivateKeySigner({
|
|
1016
|
-
accountId,
|
|
1017
|
-
privateKey: hederaPrivateKey,
|
|
1018
|
-
network: resolvedNetwork.hederaNetwork
|
|
1019
|
-
});
|
|
1020
|
-
} else if (evmPrivateKey) {
|
|
1021
|
-
if (resolvedNetwork.kind !== "evm") {
|
|
1022
|
-
throw new Error(
|
|
1023
|
-
"evmPrivateKey can only be used with CAIP-2 EVM networks (eip155:<chainId>)."
|
|
1024
|
-
);
|
|
1025
|
-
}
|
|
1026
|
-
const formattedKey = evmPrivateKey.startsWith("0x") ? evmPrivateKey : `0x${evmPrivateKey}`;
|
|
1027
|
-
const account = privateKeyToAccount(formattedKey);
|
|
1028
|
-
authOptions.sign = async (message) => ({
|
|
1029
|
-
signature: await account.signMessage({ message }),
|
|
1030
|
-
signatureKind: "evm",
|
|
1031
|
-
publicKey: account.publicKey
|
|
1032
|
-
});
|
|
1033
|
-
} else {
|
|
1034
|
-
throw new Error(
|
|
1035
|
-
"Provide a signer, sign function, hederaPrivateKey, or evmPrivateKey to authenticate with the ledger."
|
|
788
|
+
/**
|
|
789
|
+
* Retrieves transaction details by consensus timestamp.
|
|
790
|
+
* @param timestamp The consensus timestamp of the transaction (e.g., "1629400000.000000000").
|
|
791
|
+
* @returns A promise that resolves to the transaction details or null.
|
|
792
|
+
*/
|
|
793
|
+
async getTransactionByTimestamp(timestamp) {
|
|
794
|
+
this.logger.info(`Getting transaction by timestamp: ${timestamp}`);
|
|
795
|
+
try {
|
|
796
|
+
const response = await this._requestWithRetry(`/api/v1/transactions?timestamp=${timestamp}&limit=1`);
|
|
797
|
+
return response.transactions;
|
|
798
|
+
} catch (error) {
|
|
799
|
+
this.logger.error(
|
|
800
|
+
`Error fetching transaction by timestamp ${timestamp}: ${error}`
|
|
1036
801
|
);
|
|
1037
|
-
|
|
1038
|
-
|
|
1039
|
-
|
|
802
|
+
return [];
|
|
803
|
+
}
|
|
804
|
+
}
|
|
805
|
+
/**
|
|
806
|
+
* Retrieves NFTs for a given account ID, optionally filtered by token ID.
|
|
807
|
+
* @param accountId The ID of the account.
|
|
808
|
+
* @param tokenId Optional ID of the token to filter NFTs by.
|
|
809
|
+
* @param limit The maximum number of NFTs to return per page (API has its own max).
|
|
810
|
+
* @returns A promise that resolves to an array of NftDetail or null.
|
|
811
|
+
*/
|
|
812
|
+
async getAccountNfts(accountId, tokenId, limit = 100) {
|
|
813
|
+
this.logger.info(
|
|
814
|
+
`Getting NFTs for account ${accountId}${tokenId ? ` for token ${tokenId}` : ""}`
|
|
1040
815
|
);
|
|
1041
|
-
|
|
1042
|
-
|
|
1043
|
-
|
|
816
|
+
let allNfts = [];
|
|
817
|
+
let endpoint = `/api/v1/accounts/${accountId}/nfts?limit=${limit}`;
|
|
818
|
+
if (tokenId) {
|
|
819
|
+
endpoint += `&token.id=${tokenId}`;
|
|
1044
820
|
}
|
|
1045
|
-
logger?.info?.(
|
|
1046
|
-
`Ledger authentication complete${labelSuffix}. Issued key prefix: ${verification.apiKey.prefix}…${verification.apiKey.lastFour}`
|
|
1047
|
-
);
|
|
1048
|
-
return verification;
|
|
1049
|
-
}
|
|
1050
|
-
async listProtocols() {
|
|
1051
|
-
const raw = await this.requestJson("/protocols", {
|
|
1052
|
-
method: "GET"
|
|
1053
|
-
});
|
|
1054
|
-
return this.parseWithSchema(
|
|
1055
|
-
raw,
|
|
1056
|
-
protocolsResponseSchema,
|
|
1057
|
-
"protocols response"
|
|
1058
|
-
);
|
|
1059
|
-
}
|
|
1060
|
-
async detectProtocol(message) {
|
|
1061
|
-
const raw = await this.requestJson("/detect-protocol", {
|
|
1062
|
-
method: "POST",
|
|
1063
|
-
body: { message },
|
|
1064
|
-
headers: { "content-type": "application/json" }
|
|
1065
|
-
});
|
|
1066
|
-
return this.parseWithSchema(
|
|
1067
|
-
raw,
|
|
1068
|
-
detectProtocolResponseSchema,
|
|
1069
|
-
"detect protocol response"
|
|
1070
|
-
);
|
|
1071
|
-
}
|
|
1072
|
-
async registrySearchByNamespace(registry, query) {
|
|
1073
|
-
const params = new URLSearchParams();
|
|
1074
|
-
if (query) {
|
|
1075
|
-
params.set("q", query);
|
|
1076
|
-
}
|
|
1077
|
-
const suffix = params.size > 0 ? `?${params.toString()}` : "";
|
|
1078
|
-
const raw = await this.requestJson(
|
|
1079
|
-
`/registries/${encodeURIComponent(registry)}/search${suffix}`,
|
|
1080
|
-
{
|
|
1081
|
-
method: "GET"
|
|
1082
|
-
}
|
|
1083
|
-
);
|
|
1084
|
-
return this.parseWithSchema(
|
|
1085
|
-
raw,
|
|
1086
|
-
registrySearchByNamespaceSchema,
|
|
1087
|
-
"registry search response"
|
|
1088
|
-
);
|
|
1089
|
-
}
|
|
1090
|
-
async vectorSearch(request) {
|
|
1091
821
|
try {
|
|
1092
|
-
|
|
1093
|
-
|
|
1094
|
-
|
|
1095
|
-
|
|
1096
|
-
|
|
1097
|
-
|
|
1098
|
-
|
|
1099
|
-
|
|
1100
|
-
|
|
1101
|
-
|
|
1102
|
-
|
|
1103
|
-
|
|
1104
|
-
|
|
1105
|
-
|
|
1106
|
-
|
|
1107
|
-
|
|
822
|
+
for (let i = 0; i < 10 && endpoint; i++) {
|
|
823
|
+
const response = await this._requestWithRetry(endpoint);
|
|
824
|
+
if (response && response.nfts) {
|
|
825
|
+
const nftsWithUri = response.nfts.map((nft) => {
|
|
826
|
+
let tokenUri = void 0;
|
|
827
|
+
if (nft.metadata) {
|
|
828
|
+
try {
|
|
829
|
+
if (this.isServerEnvironment) {
|
|
830
|
+
tokenUri = Buffer.from(nft.metadata, "base64").toString(
|
|
831
|
+
"utf-8"
|
|
832
|
+
);
|
|
833
|
+
} else {
|
|
834
|
+
tokenUri = new TextDecoder().decode(
|
|
835
|
+
Uint8Array.from(atob(nft.metadata), (c) => c.charCodeAt(0))
|
|
836
|
+
);
|
|
837
|
+
}
|
|
838
|
+
} catch (e) {
|
|
839
|
+
this.logger.warn(
|
|
840
|
+
`Failed to decode metadata for NFT ${nft.token_id} SN ${nft.serial_number}: ${e.message}`
|
|
841
|
+
);
|
|
842
|
+
}
|
|
843
|
+
}
|
|
844
|
+
return { ...nft, token_uri: tokenUri };
|
|
845
|
+
});
|
|
846
|
+
allNfts = allNfts.concat(nftsWithUri);
|
|
847
|
+
}
|
|
848
|
+
endpoint = response.links?.next || "";
|
|
849
|
+
if (!endpoint) break;
|
|
1108
850
|
}
|
|
1109
|
-
|
|
851
|
+
return allNfts;
|
|
852
|
+
} catch (error) {
|
|
853
|
+
this.logger.error(
|
|
854
|
+
`Error fetching NFTs for account ${accountId}: ${error.message}`
|
|
855
|
+
);
|
|
856
|
+
return null;
|
|
1110
857
|
}
|
|
1111
858
|
}
|
|
1112
|
-
|
|
1113
|
-
|
|
1114
|
-
|
|
1115
|
-
|
|
1116
|
-
|
|
1117
|
-
|
|
1118
|
-
|
|
1119
|
-
|
|
1120
|
-
|
|
1121
|
-
|
|
1122
|
-
async adaptersDetailed() {
|
|
1123
|
-
const raw = await this.requestJson("/adapters/details", {
|
|
1124
|
-
method: "GET"
|
|
1125
|
-
});
|
|
1126
|
-
return this.parseWithSchema(
|
|
1127
|
-
raw,
|
|
1128
|
-
adapterDetailsResponseSchema,
|
|
1129
|
-
"adapter details response"
|
|
1130
|
-
);
|
|
1131
|
-
}
|
|
1132
|
-
async websocketStats() {
|
|
1133
|
-
const raw = await this.requestJson("/websocket/stats", {
|
|
1134
|
-
method: "GET"
|
|
1135
|
-
});
|
|
1136
|
-
return this.parseWithSchema(
|
|
1137
|
-
raw,
|
|
1138
|
-
websocketStatsResponseSchema,
|
|
1139
|
-
"websocket stats response"
|
|
1140
|
-
);
|
|
1141
|
-
}
|
|
1142
|
-
async metricsSummary() {
|
|
1143
|
-
const raw = await this.requestJson("/metrics", {
|
|
1144
|
-
method: "GET"
|
|
1145
|
-
});
|
|
1146
|
-
return this.parseWithSchema(
|
|
1147
|
-
raw,
|
|
1148
|
-
metricsSummaryResponseSchema,
|
|
1149
|
-
"metrics summary response"
|
|
1150
|
-
);
|
|
1151
|
-
}
|
|
1152
|
-
async validateUaid(uaid) {
|
|
1153
|
-
const raw = await this.requestJson(
|
|
1154
|
-
`/uaids/validate/${encodeURIComponent(uaid)}`,
|
|
1155
|
-
{
|
|
1156
|
-
method: "GET"
|
|
1157
|
-
}
|
|
859
|
+
/**
|
|
860
|
+
* Validates NFT ownership by checking if a specific serial number of a token ID exists for an account.
|
|
861
|
+
* @param accountId The ID of the account.
|
|
862
|
+
* @param tokenId The ID of the NFT's token.
|
|
863
|
+
* @param serialNumber The serial number of the NFT.
|
|
864
|
+
* @returns A promise that resolves to the NftDetail if owned, or null otherwise.
|
|
865
|
+
*/
|
|
866
|
+
async validateNFTOwnership(accountId, tokenId, serialNumber) {
|
|
867
|
+
this.logger.info(
|
|
868
|
+
`Validating ownership of NFT ${tokenId} SN ${serialNumber} for account ${accountId}`
|
|
1158
869
|
);
|
|
1159
|
-
|
|
1160
|
-
|
|
1161
|
-
|
|
1162
|
-
|
|
1163
|
-
|
|
1164
|
-
|
|
1165
|
-
|
|
1166
|
-
const raw = await this.requestJson(
|
|
1167
|
-
`/uaids/connections/${encodeURIComponent(uaid)}/status`,
|
|
1168
|
-
{
|
|
1169
|
-
method: "GET"
|
|
870
|
+
try {
|
|
871
|
+
const nfts = await this.getAccountNfts(accountId, tokenId);
|
|
872
|
+
if (nfts) {
|
|
873
|
+
const foundNft = nfts.find(
|
|
874
|
+
(nft) => nft.token_id === tokenId && nft.serial_number === serialNumber
|
|
875
|
+
);
|
|
876
|
+
return foundNft || null;
|
|
1170
877
|
}
|
|
1171
|
-
|
|
1172
|
-
|
|
1173
|
-
|
|
1174
|
-
|
|
1175
|
-
|
|
1176
|
-
);
|
|
1177
|
-
}
|
|
1178
|
-
async closeUaidConnection(uaid) {
|
|
1179
|
-
await this.request(`/uaids/connections/${encodeURIComponent(uaid)}`, {
|
|
1180
|
-
method: "DELETE"
|
|
1181
|
-
});
|
|
878
|
+
return null;
|
|
879
|
+
} catch (error) {
|
|
880
|
+
this.logger.error(`Error validating NFT ownership: ${error.message}`);
|
|
881
|
+
return null;
|
|
882
|
+
}
|
|
1182
883
|
}
|
|
1183
|
-
|
|
1184
|
-
|
|
1185
|
-
|
|
1186
|
-
|
|
1187
|
-
|
|
1188
|
-
|
|
1189
|
-
|
|
1190
|
-
|
|
884
|
+
/**
|
|
885
|
+
* Performs a read-only query against a smart contract (eth_call like).
|
|
886
|
+
* @param contractIdOrAddress The contract ID (e.g., "0.0.123") or EVM address (e.g., "0x...").
|
|
887
|
+
* @param functionSelector The function selector and encoded parameters (e.g., "0xabcdef12...").
|
|
888
|
+
* @param payerAccountId The account ID of the payer (not strictly payer for read-only, but often required as 'from').
|
|
889
|
+
* @param estimate Whether this is an estimate call. Mirror node might not support this directly in /contracts/call for true estimation.
|
|
890
|
+
* @param block Block parameter, e.g., "latest", "pending", or block number.
|
|
891
|
+
* @param value The value in tinybars to send with the call (for payable view/pure functions, usually 0).
|
|
892
|
+
* @returns A promise that resolves to the contract call query response or null.
|
|
893
|
+
*/
|
|
894
|
+
async readSmartContractQuery(contractIdOrAddress, functionSelector, payerAccountId, options) {
|
|
895
|
+
this.logger.info(
|
|
896
|
+
`Reading smart contract ${contractIdOrAddress} with selector ${functionSelector}`
|
|
1191
897
|
);
|
|
1192
|
-
|
|
1193
|
-
|
|
1194
|
-
const
|
|
1195
|
-
|
|
898
|
+
const toAddress = contractIdOrAddress.startsWith("0x") ? contractIdOrAddress : `0x${AccountId.fromString(contractIdOrAddress).toSolidityAddress()}`;
|
|
899
|
+
const fromAddress = payerAccountId.startsWith("0x") ? payerAccountId : `0x${AccountId.fromString(payerAccountId).toSolidityAddress()}`;
|
|
900
|
+
const body = {
|
|
901
|
+
block: options?.block || "latest",
|
|
902
|
+
data: functionSelector,
|
|
903
|
+
estimate: options?.estimate || false,
|
|
904
|
+
from: fromAddress,
|
|
905
|
+
to: toAddress,
|
|
906
|
+
gas: options?.gas,
|
|
907
|
+
gasPrice: options?.gasPrice,
|
|
908
|
+
value: options?.value || 0
|
|
909
|
+
};
|
|
910
|
+
Object.keys(body).forEach((key) => {
|
|
911
|
+
const K = key;
|
|
912
|
+
if (body[K] === void 0) {
|
|
913
|
+
delete body[K];
|
|
914
|
+
}
|
|
1196
915
|
});
|
|
1197
|
-
|
|
1198
|
-
|
|
1199
|
-
|
|
1200
|
-
|
|
1201
|
-
|
|
1202
|
-
|
|
1203
|
-
|
|
1204
|
-
|
|
1205
|
-
|
|
1206
|
-
|
|
916
|
+
try {
|
|
917
|
+
const url = this.constructUrl("/api/v1/contracts/call");
|
|
918
|
+
const response = await this._fetchWithRetry(
|
|
919
|
+
url,
|
|
920
|
+
{
|
|
921
|
+
method: "POST",
|
|
922
|
+
body: JSON.stringify(body),
|
|
923
|
+
headers: {
|
|
924
|
+
"Content-Type": "application/json"
|
|
925
|
+
}
|
|
926
|
+
}
|
|
927
|
+
);
|
|
928
|
+
return response;
|
|
929
|
+
} catch (error) {
|
|
930
|
+
this.logger.error(
|
|
931
|
+
`Error reading smart contract ${contractIdOrAddress}: ${error.message}`
|
|
932
|
+
);
|
|
933
|
+
return null;
|
|
1207
934
|
}
|
|
1208
|
-
const suffix = params.size > 0 ? `?${params.toString()}` : "";
|
|
1209
|
-
const raw = await this.requestJson(`/search/facets${suffix}`, {
|
|
1210
|
-
method: "GET"
|
|
1211
|
-
});
|
|
1212
|
-
return this.parseWithSchema(
|
|
1213
|
-
raw,
|
|
1214
|
-
searchFacetsResponseSchema,
|
|
1215
|
-
"search facets response"
|
|
1216
|
-
);
|
|
1217
935
|
}
|
|
1218
|
-
|
|
1219
|
-
|
|
1220
|
-
|
|
1221
|
-
|
|
936
|
+
/**
|
|
937
|
+
* Retrieves outstanding token airdrops sent by an account.
|
|
938
|
+
* @param accountId The ID of the account that sent the airdrops.
|
|
939
|
+
* @param options Optional parameters for filtering airdrops.
|
|
940
|
+
* @returns A promise that resolves to an array of TokenAirdrop or null.
|
|
941
|
+
*/
|
|
942
|
+
async getOutstandingTokenAirdrops(accountId, options) {
|
|
943
|
+
this.logger.info(
|
|
944
|
+
`Getting outstanding token airdrops sent by account ${accountId}`
|
|
945
|
+
);
|
|
946
|
+
let endpoint = `/api/v1/accounts/${accountId}/airdrops/outstanding`;
|
|
947
|
+
const params = new URLSearchParams();
|
|
948
|
+
if (options?.limit) {
|
|
949
|
+
params.append("limit", options.limit.toString());
|
|
1222
950
|
}
|
|
1223
|
-
if (
|
|
1224
|
-
|
|
951
|
+
if (options?.order) {
|
|
952
|
+
params.append("order", options.order);
|
|
1225
953
|
}
|
|
1226
|
-
if (
|
|
1227
|
-
|
|
954
|
+
if (options?.receiverId) {
|
|
955
|
+
params.append("receiver.id", options.receiverId);
|
|
1228
956
|
}
|
|
1229
|
-
if (
|
|
1230
|
-
|
|
957
|
+
if (options?.serialNumber) {
|
|
958
|
+
params.append("serialnumber", options.serialNumber);
|
|
1231
959
|
}
|
|
1232
|
-
if (
|
|
1233
|
-
|
|
960
|
+
if (options?.tokenId) {
|
|
961
|
+
params.append("token.id", options.tokenId);
|
|
1234
962
|
}
|
|
1235
|
-
|
|
1236
|
-
|
|
963
|
+
const queryString = params.toString();
|
|
964
|
+
if (queryString) {
|
|
965
|
+
endpoint += `?${queryString}`;
|
|
1237
966
|
}
|
|
1238
967
|
try {
|
|
1239
|
-
const
|
|
1240
|
-
|
|
1241
|
-
body,
|
|
1242
|
-
headers: { "content-type": "application/json" }
|
|
1243
|
-
});
|
|
1244
|
-
return this.parseWithSchema(
|
|
1245
|
-
raw,
|
|
1246
|
-
createSessionResponseSchema,
|
|
1247
|
-
"chat session response"
|
|
1248
|
-
);
|
|
968
|
+
const response = await this._requestWithRetry(endpoint);
|
|
969
|
+
return response.airdrops || [];
|
|
1249
970
|
} catch (error) {
|
|
1250
|
-
|
|
1251
|
-
|
|
1252
|
-
return this.createSession(payload, false);
|
|
1253
|
-
}
|
|
1254
|
-
throw error;
|
|
1255
|
-
}
|
|
1256
|
-
}
|
|
1257
|
-
async startChat(options) {
|
|
1258
|
-
if ("uaid" in options && options.uaid) {
|
|
1259
|
-
return this.startConversation({
|
|
1260
|
-
uaid: options.uaid,
|
|
1261
|
-
senderUaid: options.senderUaid,
|
|
1262
|
-
historyTtlSeconds: options.historyTtlSeconds,
|
|
1263
|
-
auth: options.auth,
|
|
1264
|
-
encryption: options.encryption,
|
|
1265
|
-
onSessionCreated: options.onSessionCreated
|
|
1266
|
-
});
|
|
1267
|
-
}
|
|
1268
|
-
if ("agentUrl" in options && options.agentUrl) {
|
|
1269
|
-
const session = await this.createSession({
|
|
1270
|
-
agentUrl: options.agentUrl,
|
|
1271
|
-
auth: options.auth,
|
|
1272
|
-
historyTtlSeconds: options.historyTtlSeconds,
|
|
1273
|
-
senderUaid: options.senderUaid
|
|
1274
|
-
});
|
|
1275
|
-
options.onSessionCreated?.(session.sessionId);
|
|
1276
|
-
return this.createPlaintextConversationHandle(
|
|
1277
|
-
session.sessionId,
|
|
1278
|
-
session.encryption ?? null,
|
|
1279
|
-
options.auth,
|
|
1280
|
-
{ agentUrl: options.agentUrl, uaid: options.uaid }
|
|
971
|
+
this.logger.error(
|
|
972
|
+
`Error fetching outstanding token airdrops for account ${accountId}: ${error.message}`
|
|
1281
973
|
);
|
|
974
|
+
return null;
|
|
1282
975
|
}
|
|
1283
|
-
throw new Error("startChat requires either uaid or agentUrl");
|
|
1284
976
|
}
|
|
1285
|
-
|
|
1286
|
-
|
|
1287
|
-
|
|
1288
|
-
|
|
1289
|
-
|
|
1290
|
-
|
|
1291
|
-
|
|
1292
|
-
|
|
1293
|
-
|
|
1294
|
-
|
|
1295
|
-
|
|
1296
|
-
|
|
1297
|
-
|
|
1298
|
-
|
|
1299
|
-
|
|
1300
|
-
|
|
1301
|
-
|
|
1302
|
-
|
|
977
|
+
/**
|
|
978
|
+
* Retrieves pending token airdrops received by an account.
|
|
979
|
+
* @param accountId The ID of the account that received the airdrops.
|
|
980
|
+
* @param options Optional parameters for filtering airdrops.
|
|
981
|
+
* @returns A promise that resolves to an array of TokenAirdrop or null.
|
|
982
|
+
*/
|
|
983
|
+
async getPendingTokenAirdrops(accountId, options) {
|
|
984
|
+
this.logger.info(
|
|
985
|
+
`Getting pending token airdrops received by account ${accountId}`
|
|
986
|
+
);
|
|
987
|
+
let endpoint = `/api/v1/accounts/${accountId}/airdrops/pending`;
|
|
988
|
+
const params = new URLSearchParams();
|
|
989
|
+
if (options?.limit) {
|
|
990
|
+
params.append("limit", options.limit.toString());
|
|
991
|
+
}
|
|
992
|
+
if (options?.order) {
|
|
993
|
+
params.append("order", options.order);
|
|
994
|
+
}
|
|
995
|
+
if (options?.senderId) {
|
|
996
|
+
params.append("sender.id", options.senderId);
|
|
997
|
+
}
|
|
998
|
+
if (options?.serialNumber) {
|
|
999
|
+
params.append("serialnumber", options.serialNumber);
|
|
1000
|
+
}
|
|
1001
|
+
if (options?.tokenId) {
|
|
1002
|
+
params.append("token.id", options.tokenId);
|
|
1003
|
+
}
|
|
1004
|
+
const queryString = params.toString();
|
|
1005
|
+
if (queryString) {
|
|
1006
|
+
endpoint += `?${queryString}`;
|
|
1303
1007
|
}
|
|
1304
1008
|
try {
|
|
1305
|
-
const
|
|
1306
|
-
|
|
1307
|
-
senderUaid: options.senderUaid,
|
|
1308
|
-
historyTtlSeconds: options.historyTtlSeconds,
|
|
1309
|
-
handshakeTimeoutMs: options.encryption?.handshakeTimeoutMs,
|
|
1310
|
-
pollIntervalMs: options.encryption?.pollIntervalMs,
|
|
1311
|
-
onSessionCreated: (sessionId) => {
|
|
1312
|
-
options.onSessionCreated?.(sessionId);
|
|
1313
|
-
},
|
|
1314
|
-
auth: options.auth
|
|
1315
|
-
});
|
|
1316
|
-
return handle;
|
|
1009
|
+
const response = await this._requestWithRetry(endpoint);
|
|
1010
|
+
return response.airdrops || [];
|
|
1317
1011
|
} catch (error) {
|
|
1318
|
-
|
|
1319
|
-
|
|
1320
|
-
|
|
1321
|
-
|
|
1322
|
-
return this.createPlaintextConversationHandle(
|
|
1323
|
-
error.sessionId,
|
|
1324
|
-
error.summary ?? null,
|
|
1325
|
-
options.auth,
|
|
1326
|
-
{ uaid: options.uaid }
|
|
1327
|
-
);
|
|
1328
|
-
}
|
|
1329
|
-
throw error;
|
|
1012
|
+
this.logger.error(
|
|
1013
|
+
`Error fetching pending token airdrops for account ${accountId}: ${error.message}`
|
|
1014
|
+
);
|
|
1015
|
+
return null;
|
|
1330
1016
|
}
|
|
1331
1017
|
}
|
|
1332
|
-
|
|
1333
|
-
|
|
1334
|
-
|
|
1335
|
-
|
|
1018
|
+
/**
|
|
1019
|
+
* Retrieves blocks from the network.
|
|
1020
|
+
* @param options Optional parameters for filtering blocks.
|
|
1021
|
+
* @returns A promise that resolves to an array of Block or null.
|
|
1022
|
+
*/
|
|
1023
|
+
async getBlocks(options) {
|
|
1024
|
+
this.logger.info("Getting blocks from the network");
|
|
1025
|
+
let endpoint = `/api/v1/blocks`;
|
|
1026
|
+
const params = new URLSearchParams();
|
|
1027
|
+
if (options?.limit) {
|
|
1028
|
+
params.append("limit", options.limit.toString());
|
|
1029
|
+
}
|
|
1030
|
+
if (options?.order) {
|
|
1031
|
+
params.append("order", options.order);
|
|
1032
|
+
}
|
|
1033
|
+
if (options?.timestamp) {
|
|
1034
|
+
params.append("timestamp", options.timestamp);
|
|
1035
|
+
}
|
|
1036
|
+
if (options?.blockNumber) {
|
|
1037
|
+
params.append("block.number", options.blockNumber);
|
|
1038
|
+
}
|
|
1039
|
+
const queryString = params.toString();
|
|
1040
|
+
if (queryString) {
|
|
1041
|
+
endpoint += `?${queryString}`;
|
|
1336
1042
|
}
|
|
1337
1043
|
try {
|
|
1338
|
-
const
|
|
1339
|
-
|
|
1340
|
-
responderUaid: options.responderUaid,
|
|
1341
|
-
handshakeTimeoutMs: options.encryption?.handshakeTimeoutMs,
|
|
1342
|
-
pollIntervalMs: options.encryption?.pollIntervalMs
|
|
1343
|
-
});
|
|
1344
|
-
return handle;
|
|
1044
|
+
const response = await this._requestWithRetry(endpoint);
|
|
1045
|
+
return response.blocks || [];
|
|
1345
1046
|
} catch (error) {
|
|
1346
|
-
|
|
1347
|
-
|
|
1348
|
-
options.sessionId,
|
|
1349
|
-
null,
|
|
1350
|
-
void 0,
|
|
1351
|
-
{ uaid: options.responderUaid }
|
|
1352
|
-
);
|
|
1353
|
-
}
|
|
1354
|
-
throw error;
|
|
1047
|
+
this.logger.error(`Error fetching blocks: ${error.message}`);
|
|
1048
|
+
return null;
|
|
1355
1049
|
}
|
|
1356
1050
|
}
|
|
1357
|
-
|
|
1358
|
-
|
|
1359
|
-
|
|
1360
|
-
|
|
1361
|
-
|
|
1362
|
-
|
|
1363
|
-
|
|
1364
|
-
|
|
1365
|
-
|
|
1366
|
-
|
|
1367
|
-
|
|
1368
|
-
|
|
1369
|
-
|
|
1370
|
-
|
|
1371
|
-
|
|
1372
|
-
return this.attachDecryptedHistory(sessionId, snapshot, options);
|
|
1373
|
-
}
|
|
1374
|
-
attachDecryptedHistory(sessionId, snapshot, options) {
|
|
1375
|
-
const shouldDecrypt = options?.decrypt !== void 0 ? options.decrypt : this.encryptionOptions?.autoDecryptHistory === true;
|
|
1376
|
-
if (!shouldDecrypt) {
|
|
1377
|
-
return snapshot;
|
|
1378
|
-
}
|
|
1379
|
-
const context = this.resolveDecryptionContext(sessionId, options);
|
|
1380
|
-
if (!context) {
|
|
1381
|
-
throw new Error(
|
|
1382
|
-
"Unable to decrypt chat history: encryption context unavailable"
|
|
1051
|
+
/**
|
|
1052
|
+
* Retrieves a specific block by number or hash.
|
|
1053
|
+
* @param blockNumberOrHash The block number or hash.
|
|
1054
|
+
* @returns A promise that resolves to a Block or null.
|
|
1055
|
+
*/
|
|
1056
|
+
async getBlock(blockNumberOrHash) {
|
|
1057
|
+
this.logger.info(`Getting block ${blockNumberOrHash}`);
|
|
1058
|
+
try {
|
|
1059
|
+
const response = await this._requestWithRetry(
|
|
1060
|
+
`/api/v1/blocks/${blockNumberOrHash}`
|
|
1061
|
+
);
|
|
1062
|
+
return response;
|
|
1063
|
+
} catch (error) {
|
|
1064
|
+
this.logger.error(
|
|
1065
|
+
`Error fetching block ${blockNumberOrHash}: ${error.message}`
|
|
1383
1066
|
);
|
|
1067
|
+
return null;
|
|
1384
1068
|
}
|
|
1385
|
-
const decryptedHistory = snapshot.history.map((entry) => ({
|
|
1386
|
-
entry,
|
|
1387
|
-
plaintext: this.decryptHistoryEntryFromContext(sessionId, entry, context)
|
|
1388
|
-
}));
|
|
1389
|
-
return { ...snapshot, decryptedHistory };
|
|
1390
1069
|
}
|
|
1391
|
-
|
|
1392
|
-
|
|
1393
|
-
|
|
1394
|
-
|
|
1395
|
-
|
|
1396
|
-
|
|
1397
|
-
|
|
1398
|
-
|
|
1399
|
-
|
|
1400
|
-
)
|
|
1401
|
-
|
|
1402
|
-
entries[existingIndex] = normalized;
|
|
1403
|
-
} else {
|
|
1404
|
-
entries.push(normalized);
|
|
1070
|
+
/**
|
|
1071
|
+
* Retrieves contract entities from the network.
|
|
1072
|
+
* @param options Optional parameters for filtering contracts.
|
|
1073
|
+
* @returns A promise that resolves to an array of ContractEntity or null.
|
|
1074
|
+
*/
|
|
1075
|
+
async getContracts(options) {
|
|
1076
|
+
this.logger.info("Getting contracts from the network");
|
|
1077
|
+
let url = `/api/v1/contracts`;
|
|
1078
|
+
const params = new URLSearchParams();
|
|
1079
|
+
if (options?.contractId) {
|
|
1080
|
+
params.append("contract.id", options.contractId);
|
|
1405
1081
|
}
|
|
1406
|
-
|
|
1407
|
-
|
|
1408
|
-
// Exposed for EncryptedChatManager to persist decryption context
|
|
1409
|
-
registerConversationContextForEncryption(context) {
|
|
1410
|
-
this.registerConversationContext(context);
|
|
1411
|
-
}
|
|
1412
|
-
resolveDecryptionContext(sessionId, options) {
|
|
1413
|
-
if (options?.sharedSecret) {
|
|
1414
|
-
return {
|
|
1415
|
-
sessionId,
|
|
1416
|
-
sharedSecret: this.normalizeSharedSecret(options.sharedSecret),
|
|
1417
|
-
identity: options.identity
|
|
1418
|
-
};
|
|
1082
|
+
if (options?.limit) {
|
|
1083
|
+
params.append("limit", options.limit.toString());
|
|
1419
1084
|
}
|
|
1420
|
-
|
|
1421
|
-
|
|
1422
|
-
return null;
|
|
1085
|
+
if (options?.order) {
|
|
1086
|
+
params.append("order", options.order);
|
|
1423
1087
|
}
|
|
1424
|
-
|
|
1425
|
-
|
|
1426
|
-
|
|
1427
|
-
|
|
1428
|
-
|
|
1429
|
-
|
|
1430
|
-
|
|
1088
|
+
const queryString = params.toString();
|
|
1089
|
+
if (queryString) {
|
|
1090
|
+
url += `?${queryString}`;
|
|
1091
|
+
}
|
|
1092
|
+
try {
|
|
1093
|
+
const response = await this._requestWithRetry(url);
|
|
1094
|
+
return response.contracts || [];
|
|
1095
|
+
} catch (error) {
|
|
1096
|
+
this.logger.error(`Error fetching contracts: ${error.message}`);
|
|
1097
|
+
return null;
|
|
1431
1098
|
}
|
|
1432
|
-
return contexts[0];
|
|
1433
1099
|
}
|
|
1434
|
-
|
|
1435
|
-
|
|
1436
|
-
|
|
1437
|
-
|
|
1100
|
+
/**
|
|
1101
|
+
* Retrieves a specific contract by ID or address.
|
|
1102
|
+
* @param contractIdOrAddress The contract ID or EVM address.
|
|
1103
|
+
* @param timestamp Optional timestamp for historical data.
|
|
1104
|
+
* @returns A promise that resolves to a ContractEntity or null.
|
|
1105
|
+
*/
|
|
1106
|
+
async getContract(contractIdOrAddress, timestamp) {
|
|
1107
|
+
this.logger.info(`Getting contract ${contractIdOrAddress}`);
|
|
1108
|
+
let url = `/api/v1/contracts/${contractIdOrAddress}`;
|
|
1109
|
+
if (timestamp) {
|
|
1110
|
+
url += `?timestamp=${timestamp}`;
|
|
1438
1111
|
}
|
|
1439
|
-
const secret = Buffer.from(context.sharedSecret);
|
|
1440
1112
|
try {
|
|
1441
|
-
|
|
1442
|
-
|
|
1443
|
-
|
|
1444
|
-
|
|
1445
|
-
|
|
1113
|
+
const response = await this._requestWithRetry(url);
|
|
1114
|
+
return response;
|
|
1115
|
+
} catch (error) {
|
|
1116
|
+
this.logger.error(
|
|
1117
|
+
`Error fetching contract ${contractIdOrAddress}: ${error.message}`
|
|
1118
|
+
);
|
|
1446
1119
|
return null;
|
|
1447
1120
|
}
|
|
1448
1121
|
}
|
|
1449
|
-
|
|
1450
|
-
|
|
1451
|
-
|
|
1122
|
+
/**
|
|
1123
|
+
* Retrieves contract results from the network.
|
|
1124
|
+
* @param options Optional parameters for filtering contract results.
|
|
1125
|
+
* @returns A promise that resolves to an array of ContractResult or null.
|
|
1126
|
+
*/
|
|
1127
|
+
async getContractResults(options) {
|
|
1128
|
+
this.logger.info("Getting contract results from the network");
|
|
1129
|
+
let url = `/api/v1/contracts/results`;
|
|
1130
|
+
const params = new URLSearchParams();
|
|
1131
|
+
if (options?.from) {
|
|
1132
|
+
params.append("from", options.from);
|
|
1452
1133
|
}
|
|
1453
|
-
if (
|
|
1454
|
-
|
|
1134
|
+
if (options?.blockHash) {
|
|
1135
|
+
params.append("block.hash", options.blockHash);
|
|
1455
1136
|
}
|
|
1456
|
-
if (
|
|
1457
|
-
|
|
1137
|
+
if (options?.blockNumber) {
|
|
1138
|
+
params.append("block.number", options.blockNumber);
|
|
1458
1139
|
}
|
|
1459
|
-
if (
|
|
1460
|
-
|
|
1140
|
+
if (options?.internal !== void 0) {
|
|
1141
|
+
params.append("internal", options.internal.toString());
|
|
1461
1142
|
}
|
|
1462
|
-
if (
|
|
1463
|
-
|
|
1143
|
+
if (options?.limit) {
|
|
1144
|
+
params.append("limit", options.limit.toString());
|
|
1464
1145
|
}
|
|
1465
|
-
if (
|
|
1466
|
-
|
|
1146
|
+
if (options?.order) {
|
|
1147
|
+
params.append("order", options.order);
|
|
1467
1148
|
}
|
|
1468
|
-
|
|
1469
|
-
|
|
1470
|
-
identityMatchesRecipient(recipient, identity) {
|
|
1471
|
-
if (identity.uaid && recipient.uaid?.toLowerCase() === identity.uaid.toLowerCase()) {
|
|
1472
|
-
return true;
|
|
1149
|
+
if (options?.timestamp) {
|
|
1150
|
+
params.append("timestamp", options.timestamp);
|
|
1473
1151
|
}
|
|
1474
|
-
if (
|
|
1475
|
-
|
|
1152
|
+
if (options?.transactionIndex) {
|
|
1153
|
+
params.append("transaction.index", options.transactionIndex.toString());
|
|
1476
1154
|
}
|
|
1477
|
-
|
|
1478
|
-
|
|
1155
|
+
const queryString = params.toString();
|
|
1156
|
+
if (queryString) {
|
|
1157
|
+
url += `?${queryString}`;
|
|
1479
1158
|
}
|
|
1480
|
-
|
|
1481
|
-
|
|
1159
|
+
try {
|
|
1160
|
+
const response = await this._requestWithRetry(url);
|
|
1161
|
+
return response.results || [];
|
|
1162
|
+
} catch (error) {
|
|
1163
|
+
this.logger.error(`Error fetching contract results: ${error.message}`);
|
|
1164
|
+
return null;
|
|
1482
1165
|
}
|
|
1483
|
-
return false;
|
|
1484
|
-
}
|
|
1485
|
-
createPlaintextConversationHandle(sessionId, summary, defaultAuth, context) {
|
|
1486
|
-
const uaid = context?.uaid?.trim();
|
|
1487
|
-
const agentUrl = context?.agentUrl?.trim();
|
|
1488
|
-
return {
|
|
1489
|
-
sessionId,
|
|
1490
|
-
mode: "plaintext",
|
|
1491
|
-
summary: summary ?? null,
|
|
1492
|
-
send: async (options) => {
|
|
1493
|
-
const plaintext = options.plaintext;
|
|
1494
|
-
if (!plaintext || plaintext.trim().length === 0) {
|
|
1495
|
-
throw new Error("plaintext is required for chat messages");
|
|
1496
|
-
}
|
|
1497
|
-
const message = options.message ?? plaintext;
|
|
1498
|
-
return this.sendMessage({
|
|
1499
|
-
sessionId,
|
|
1500
|
-
message,
|
|
1501
|
-
streaming: options.streaming,
|
|
1502
|
-
auth: options.auth ?? defaultAuth,
|
|
1503
|
-
uaid,
|
|
1504
|
-
agentUrl
|
|
1505
|
-
});
|
|
1506
|
-
},
|
|
1507
|
-
decryptHistoryEntry: (entry) => entry.content
|
|
1508
|
-
};
|
|
1509
|
-
}
|
|
1510
|
-
async compactHistory(payload) {
|
|
1511
|
-
if (!payload.sessionId || payload.sessionId.trim().length === 0) {
|
|
1512
|
-
throw new Error("sessionId is required to compact chat history");
|
|
1513
|
-
}
|
|
1514
|
-
const body = {};
|
|
1515
|
-
if (typeof payload.preserveEntries === "number" && Number.isFinite(payload.preserveEntries) && payload.preserveEntries >= 0) {
|
|
1516
|
-
body.preserveEntries = Math.floor(payload.preserveEntries);
|
|
1517
|
-
}
|
|
1518
|
-
const raw = await this.requestJson(
|
|
1519
|
-
`/chat/session/${encodeURIComponent(payload.sessionId)}/compact`,
|
|
1520
|
-
{
|
|
1521
|
-
method: "POST",
|
|
1522
|
-
headers: { "content-type": "application/json" },
|
|
1523
|
-
body
|
|
1524
|
-
}
|
|
1525
|
-
);
|
|
1526
|
-
return this.parseWithSchema(
|
|
1527
|
-
raw,
|
|
1528
|
-
chatHistoryCompactionResponseSchema,
|
|
1529
|
-
"chat history compaction response"
|
|
1530
|
-
);
|
|
1531
|
-
}
|
|
1532
|
-
async fetchEncryptionStatus(sessionId) {
|
|
1533
|
-
if (!sessionId || sessionId.trim().length === 0) {
|
|
1534
|
-
throw new Error("sessionId is required for encryption status");
|
|
1535
|
-
}
|
|
1536
|
-
const raw = await this.requestJson(
|
|
1537
|
-
`/chat/session/${encodeURIComponent(sessionId)}/encryption`,
|
|
1538
|
-
{
|
|
1539
|
-
method: "GET"
|
|
1540
|
-
}
|
|
1541
|
-
);
|
|
1542
|
-
return this.parseWithSchema(
|
|
1543
|
-
raw,
|
|
1544
|
-
sessionEncryptionStatusResponseSchema,
|
|
1545
|
-
"session encryption status response"
|
|
1546
|
-
);
|
|
1547
1166
|
}
|
|
1548
|
-
|
|
1549
|
-
|
|
1550
|
-
|
|
1551
|
-
|
|
1552
|
-
|
|
1553
|
-
|
|
1554
|
-
|
|
1555
|
-
|
|
1556
|
-
|
|
1557
|
-
|
|
1558
|
-
|
|
1559
|
-
|
|
1560
|
-
|
|
1561
|
-
|
|
1562
|
-
|
|
1563
|
-
|
|
1564
|
-
|
|
1565
|
-
|
|
1566
|
-
|
|
1567
|
-
|
|
1568
|
-
|
|
1569
|
-
);
|
|
1570
|
-
const response = this.parseWithSchema(
|
|
1571
|
-
raw,
|
|
1572
|
-
encryptionHandshakeResponseSchema,
|
|
1573
|
-
"encryption handshake response"
|
|
1574
|
-
);
|
|
1575
|
-
return response.handshake;
|
|
1167
|
+
/**
|
|
1168
|
+
* Retrieves a specific contract result by transaction ID or hash.
|
|
1169
|
+
* @param transactionIdOrHash The transaction ID or hash.
|
|
1170
|
+
* @param nonce Optional nonce filter.
|
|
1171
|
+
* @returns A promise that resolves to a ContractResult or null.
|
|
1172
|
+
*/
|
|
1173
|
+
async getContractResult(transactionIdOrHash, nonce) {
|
|
1174
|
+
this.logger.info(`Getting contract result for ${transactionIdOrHash}`);
|
|
1175
|
+
let url = `/api/v1/contracts/results/${transactionIdOrHash}`;
|
|
1176
|
+
if (nonce !== void 0) {
|
|
1177
|
+
url += `?nonce=${nonce}`;
|
|
1178
|
+
}
|
|
1179
|
+
try {
|
|
1180
|
+
const response = await this._requestWithRetry(url);
|
|
1181
|
+
return response;
|
|
1182
|
+
} catch (error) {
|
|
1183
|
+
this.logger.error(
|
|
1184
|
+
`Error fetching contract result for ${transactionIdOrHash}: ${error.message}`
|
|
1185
|
+
);
|
|
1186
|
+
return null;
|
|
1187
|
+
}
|
|
1576
1188
|
}
|
|
1577
|
-
|
|
1578
|
-
|
|
1579
|
-
|
|
1580
|
-
|
|
1581
|
-
|
|
1582
|
-
|
|
1583
|
-
|
|
1584
|
-
|
|
1585
|
-
|
|
1586
|
-
"register encryption key response"
|
|
1189
|
+
/**
|
|
1190
|
+
* Retrieves contract results for a specific contract.
|
|
1191
|
+
* @param contractIdOrAddress The contract ID or EVM address.
|
|
1192
|
+
* @param options Optional parameters for filtering.
|
|
1193
|
+
* @returns A promise that resolves to an array of ContractResult or null.
|
|
1194
|
+
*/
|
|
1195
|
+
async getContractResultsByContract(contractIdOrAddress, options) {
|
|
1196
|
+
this.logger.info(
|
|
1197
|
+
`Getting contract results for contract ${contractIdOrAddress}`
|
|
1587
1198
|
);
|
|
1588
|
-
|
|
1589
|
-
|
|
1590
|
-
|
|
1591
|
-
|
|
1592
|
-
};
|
|
1593
|
-
if (payload.streaming !== void 0) {
|
|
1594
|
-
body.streaming = payload.streaming;
|
|
1199
|
+
let url = `/api/v1/contracts/${contractIdOrAddress}/results`;
|
|
1200
|
+
const params = new URLSearchParams();
|
|
1201
|
+
if (options?.blockHash) {
|
|
1202
|
+
params.append("block.hash", options.blockHash);
|
|
1595
1203
|
}
|
|
1596
|
-
if (
|
|
1597
|
-
|
|
1204
|
+
if (options?.blockNumber) {
|
|
1205
|
+
params.append("block.number", options.blockNumber);
|
|
1598
1206
|
}
|
|
1599
|
-
if (
|
|
1600
|
-
|
|
1207
|
+
if (options?.from) {
|
|
1208
|
+
params.append("from", options.from);
|
|
1601
1209
|
}
|
|
1602
|
-
if (
|
|
1603
|
-
|
|
1210
|
+
if (options?.internal !== void 0) {
|
|
1211
|
+
params.append("internal", options.internal.toString());
|
|
1604
1212
|
}
|
|
1605
|
-
if (
|
|
1606
|
-
|
|
1213
|
+
if (options?.limit) {
|
|
1214
|
+
params.append("limit", options.limit.toString());
|
|
1607
1215
|
}
|
|
1608
|
-
|
|
1609
|
-
|
|
1610
|
-
|
|
1611
|
-
|
|
1612
|
-
|
|
1613
|
-
|
|
1614
|
-
|
|
1615
|
-
|
|
1616
|
-
|
|
1617
|
-
|
|
1618
|
-
|
|
1619
|
-
|
|
1620
|
-
|
|
1621
|
-
|
|
1622
|
-
|
|
1623
|
-
|
|
1624
|
-
|
|
1625
|
-
|
|
1626
|
-
|
|
1627
|
-
|
|
1628
|
-
|
|
1629
|
-
method: "POST",
|
|
1630
|
-
body,
|
|
1631
|
-
headers: { "content-type": "application/json" }
|
|
1632
|
-
});
|
|
1633
|
-
return this.parseWithSchema(
|
|
1634
|
-
raw,
|
|
1635
|
-
sendMessageResponseSchema,
|
|
1636
|
-
"chat message response"
|
|
1637
|
-
);
|
|
1638
|
-
}
|
|
1639
|
-
async endSession(sessionId) {
|
|
1640
|
-
await this.request(`/chat/session/${encodeURIComponent(sessionId)}`, {
|
|
1641
|
-
method: "DELETE"
|
|
1642
|
-
});
|
|
1643
|
-
}
|
|
1644
|
-
async generateEncryptionKeyPair(options = {}) {
|
|
1645
|
-
this.assertNodeRuntime("generateEncryptionKeyPair");
|
|
1646
|
-
const keyType = options.keyType ?? "secp256k1";
|
|
1647
|
-
if (keyType !== "secp256k1") {
|
|
1648
|
-
throw new Error("Only secp256k1 key generation is supported currently");
|
|
1649
|
-
}
|
|
1650
|
-
const privateKeyBytes = randomBytes(32);
|
|
1651
|
-
const privateKey = Buffer.from(privateKeyBytes).toString("hex");
|
|
1652
|
-
const publicKeyBytes = secp256k1.getPublicKey(privateKeyBytes, true);
|
|
1653
|
-
const publicKey = Buffer.from(publicKeyBytes).toString("hex");
|
|
1654
|
-
const envVar = options.envVar ?? "RB_ENCRYPTION_PRIVATE_KEY";
|
|
1655
|
-
const resolvedPath = options.envPath ? path.resolve(options.envPath) : void 0;
|
|
1656
|
-
if (resolvedPath) {
|
|
1657
|
-
const fsModule = getFs();
|
|
1658
|
-
if (!fsModule) {
|
|
1659
|
-
throw new Error(
|
|
1660
|
-
"File system module is not available; cannot write encryption key env file"
|
|
1661
|
-
);
|
|
1662
|
-
}
|
|
1663
|
-
const envLine = `${envVar}=${privateKey}`;
|
|
1664
|
-
if (fsModule.existsSync(resolvedPath)) {
|
|
1665
|
-
const content = fsModule.readFileSync(resolvedPath, "utf-8");
|
|
1666
|
-
const lineRegex = new RegExp(`^${envVar}=.*$`, "m");
|
|
1667
|
-
if (lineRegex.test(content)) {
|
|
1668
|
-
if (!options.overwrite) {
|
|
1669
|
-
throw new Error(
|
|
1670
|
-
`${envVar} already exists in ${resolvedPath}; set overwrite=true to replace it`
|
|
1671
|
-
);
|
|
1672
|
-
}
|
|
1673
|
-
const updated = content.replace(lineRegex, envLine);
|
|
1674
|
-
fsModule.writeFileSync(resolvedPath, updated);
|
|
1675
|
-
} else {
|
|
1676
|
-
const needsNewline = !content.endsWith("\n");
|
|
1677
|
-
fsModule.appendFileSync(
|
|
1678
|
-
resolvedPath,
|
|
1679
|
-
`${needsNewline ? "\n" : ""}${envLine}
|
|
1680
|
-
`
|
|
1681
|
-
);
|
|
1682
|
-
}
|
|
1683
|
-
} else {
|
|
1684
|
-
fsModule.writeFileSync(resolvedPath, `${envLine}
|
|
1685
|
-
`);
|
|
1686
|
-
}
|
|
1216
|
+
if (options?.order) {
|
|
1217
|
+
params.append("order", options.order);
|
|
1218
|
+
}
|
|
1219
|
+
if (options?.timestamp) {
|
|
1220
|
+
params.append("timestamp", options.timestamp);
|
|
1221
|
+
}
|
|
1222
|
+
if (options?.transactionIndex) {
|
|
1223
|
+
params.append("transaction.index", options.transactionIndex.toString());
|
|
1224
|
+
}
|
|
1225
|
+
const queryString = params.toString();
|
|
1226
|
+
if (queryString) {
|
|
1227
|
+
url += `?${queryString}`;
|
|
1228
|
+
}
|
|
1229
|
+
try {
|
|
1230
|
+
const response = await this._requestWithRetry(url);
|
|
1231
|
+
return response.results || [];
|
|
1232
|
+
} catch (error) {
|
|
1233
|
+
this.logger.error(
|
|
1234
|
+
`Error fetching contract results for ${contractIdOrAddress}: ${error.message}`
|
|
1235
|
+
);
|
|
1236
|
+
return null;
|
|
1687
1237
|
}
|
|
1688
|
-
return {
|
|
1689
|
-
privateKey,
|
|
1690
|
-
publicKey,
|
|
1691
|
-
envPath: resolvedPath,
|
|
1692
|
-
envVar
|
|
1693
|
-
};
|
|
1694
|
-
}
|
|
1695
|
-
buildUrl(path2) {
|
|
1696
|
-
const normalisedPath = path2.startsWith("/") ? path2 : `/${path2}`;
|
|
1697
|
-
return `${this.baseUrl}${normalisedPath}`;
|
|
1698
1238
|
}
|
|
1699
|
-
|
|
1700
|
-
|
|
1701
|
-
|
|
1702
|
-
|
|
1703
|
-
|
|
1704
|
-
|
|
1705
|
-
|
|
1706
|
-
|
|
1239
|
+
/**
|
|
1240
|
+
* Retrieves contract state for a specific contract.
|
|
1241
|
+
* @param contractIdOrAddress The contract ID or EVM address.
|
|
1242
|
+
* @param options Optional parameters for filtering.
|
|
1243
|
+
* @returns A promise that resolves to an array of ContractState or null.
|
|
1244
|
+
*/
|
|
1245
|
+
async getContractState(contractIdOrAddress, options) {
|
|
1246
|
+
this.logger.info(`Getting contract state for ${contractIdOrAddress}`);
|
|
1247
|
+
let url = `/api/v1/contracts/${contractIdOrAddress}/state`;
|
|
1248
|
+
const params = new URLSearchParams();
|
|
1249
|
+
if (options?.limit) {
|
|
1250
|
+
params.append("limit", options.limit.toString());
|
|
1707
1251
|
}
|
|
1708
|
-
if (
|
|
1709
|
-
|
|
1710
|
-
params.limit = limit;
|
|
1711
|
-
params.page = Math.floor(request.offset / limit) + 1;
|
|
1252
|
+
if (options?.order) {
|
|
1253
|
+
params.append("order", options.order);
|
|
1712
1254
|
}
|
|
1713
|
-
if (
|
|
1714
|
-
params.
|
|
1255
|
+
if (options?.slot) {
|
|
1256
|
+
params.append("slot", options.slot);
|
|
1715
1257
|
}
|
|
1716
|
-
if (
|
|
1717
|
-
params.
|
|
1258
|
+
if (options?.timestamp) {
|
|
1259
|
+
params.append("timestamp", options.timestamp);
|
|
1718
1260
|
}
|
|
1719
|
-
|
|
1720
|
-
|
|
1261
|
+
const queryString = params.toString();
|
|
1262
|
+
if (queryString) {
|
|
1263
|
+
url += `?${queryString}`;
|
|
1721
1264
|
}
|
|
1722
|
-
|
|
1723
|
-
|
|
1724
|
-
|
|
1265
|
+
try {
|
|
1266
|
+
const response = await this._requestWithRetry(url);
|
|
1267
|
+
return response.state || [];
|
|
1268
|
+
} catch (error) {
|
|
1269
|
+
this.logger.error(
|
|
1270
|
+
`Error fetching contract state for ${contractIdOrAddress}: ${error.message}`
|
|
1725
1271
|
);
|
|
1272
|
+
return null;
|
|
1726
1273
|
}
|
|
1727
|
-
if (request.filter?.type) {
|
|
1728
|
-
params.type = request.filter.type;
|
|
1729
|
-
}
|
|
1730
|
-
return params;
|
|
1731
|
-
}
|
|
1732
|
-
convertSearchResultToVectorResponse(result) {
|
|
1733
|
-
const hits = result.hits.map((agent) => ({
|
|
1734
|
-
agent,
|
|
1735
|
-
score: 0,
|
|
1736
|
-
highlights: {}
|
|
1737
|
-
}));
|
|
1738
|
-
const total = result.total;
|
|
1739
|
-
const limit = result.limit;
|
|
1740
|
-
const page = result.page;
|
|
1741
|
-
const totalVisible = page * limit;
|
|
1742
|
-
const limited = total > totalVisible || page > 1;
|
|
1743
|
-
return {
|
|
1744
|
-
hits,
|
|
1745
|
-
total,
|
|
1746
|
-
took: 0,
|
|
1747
|
-
totalAvailable: total,
|
|
1748
|
-
visible: hits.length,
|
|
1749
|
-
limited,
|
|
1750
|
-
credits_used: 0
|
|
1751
|
-
};
|
|
1752
1274
|
}
|
|
1753
|
-
|
|
1754
|
-
|
|
1755
|
-
|
|
1756
|
-
|
|
1757
|
-
|
|
1758
|
-
|
|
1275
|
+
/**
|
|
1276
|
+
* Retrieves contract actions for a specific transaction.
|
|
1277
|
+
* @param transactionIdOrHash The transaction ID or hash.
|
|
1278
|
+
* @param options Optional parameters for filtering.
|
|
1279
|
+
* @returns A promise that resolves to an array of ContractAction or null.
|
|
1280
|
+
*/
|
|
1281
|
+
async getContractActions(transactionIdOrHash, options) {
|
|
1282
|
+
this.logger.info(`Getting contract actions for ${transactionIdOrHash}`);
|
|
1283
|
+
let url = `/api/v1/contracts/results/${transactionIdOrHash}/actions`;
|
|
1284
|
+
const params = new URLSearchParams();
|
|
1285
|
+
if (options?.index) {
|
|
1286
|
+
params.append("index", options.index);
|
|
1759
1287
|
}
|
|
1760
|
-
|
|
1761
|
-
|
|
1762
|
-
|
|
1763
|
-
|
|
1764
|
-
|
|
1765
|
-
|
|
1766
|
-
|
|
1767
|
-
|
|
1768
|
-
|
|
1769
|
-
|
|
1770
|
-
|
|
1771
|
-
|
|
1772
|
-
|
|
1773
|
-
|
|
1774
|
-
|
|
1775
|
-
|
|
1776
|
-
return;
|
|
1777
|
-
}
|
|
1778
|
-
signal.addEventListener("abort", onAbort, { once: true });
|
|
1779
|
-
}
|
|
1780
|
-
});
|
|
1781
|
-
}
|
|
1782
|
-
async requestJson(path2, config) {
|
|
1783
|
-
const response = await this.request(path2, config);
|
|
1784
|
-
const contentType = response.headers?.get("content-type") ?? "";
|
|
1785
|
-
if (!JSON_CONTENT_TYPE.test(contentType)) {
|
|
1786
|
-
const body = await response.text();
|
|
1787
|
-
throw new RegistryBrokerParseError(
|
|
1788
|
-
"Expected JSON response from registry broker",
|
|
1789
|
-
body
|
|
1288
|
+
if (options?.limit) {
|
|
1289
|
+
params.append("limit", options.limit.toString());
|
|
1290
|
+
}
|
|
1291
|
+
if (options?.order) {
|
|
1292
|
+
params.append("order", options.order);
|
|
1293
|
+
}
|
|
1294
|
+
const queryString = params.toString();
|
|
1295
|
+
if (queryString) {
|
|
1296
|
+
url += `?${queryString}`;
|
|
1297
|
+
}
|
|
1298
|
+
try {
|
|
1299
|
+
const response = await this._requestWithRetry(url);
|
|
1300
|
+
return response.actions || [];
|
|
1301
|
+
} catch (error) {
|
|
1302
|
+
this.logger.error(
|
|
1303
|
+
`Error fetching contract actions for ${transactionIdOrHash}: ${error.message}`
|
|
1790
1304
|
);
|
|
1305
|
+
return null;
|
|
1791
1306
|
}
|
|
1792
|
-
return await response.json();
|
|
1793
1307
|
}
|
|
1794
|
-
|
|
1795
|
-
|
|
1796
|
-
|
|
1797
|
-
|
|
1798
|
-
|
|
1799
|
-
|
|
1800
|
-
|
|
1801
|
-
|
|
1308
|
+
/**
|
|
1309
|
+
* Retrieves contract logs from the network.
|
|
1310
|
+
* @param options Optional parameters for filtering logs.
|
|
1311
|
+
* @returns A promise that resolves to an array of ContractLog or null.
|
|
1312
|
+
*/
|
|
1313
|
+
async getContractLogs(options) {
|
|
1314
|
+
this.logger.info("Getting contract logs from the network");
|
|
1315
|
+
let url = `/api/v1/contracts/results/logs`;
|
|
1316
|
+
const params = new URLSearchParams();
|
|
1317
|
+
if (options?.index) {
|
|
1318
|
+
params.append("index", options.index);
|
|
1319
|
+
}
|
|
1320
|
+
if (options?.limit) {
|
|
1321
|
+
params.append("limit", options.limit.toString());
|
|
1322
|
+
}
|
|
1323
|
+
if (options?.order) {
|
|
1324
|
+
params.append("order", options.order);
|
|
1325
|
+
}
|
|
1326
|
+
if (options?.timestamp) {
|
|
1327
|
+
params.append("timestamp", options.timestamp);
|
|
1328
|
+
}
|
|
1329
|
+
if (options?.topic0) {
|
|
1330
|
+
params.append("topic0", options.topic0);
|
|
1331
|
+
}
|
|
1332
|
+
if (options?.topic1) {
|
|
1333
|
+
params.append("topic1", options.topic1);
|
|
1334
|
+
}
|
|
1335
|
+
if (options?.topic2) {
|
|
1336
|
+
params.append("topic2", options.topic2);
|
|
1337
|
+
}
|
|
1338
|
+
if (options?.topic3) {
|
|
1339
|
+
params.append("topic3", options.topic3);
|
|
1340
|
+
}
|
|
1341
|
+
if (options?.transactionHash) {
|
|
1342
|
+
params.append("transaction.hash", options.transactionHash);
|
|
1343
|
+
}
|
|
1344
|
+
const queryString = params.toString();
|
|
1345
|
+
if (queryString) {
|
|
1346
|
+
url += `?${queryString}`;
|
|
1802
1347
|
}
|
|
1803
1348
|
try {
|
|
1804
|
-
|
|
1349
|
+
const response = await this._requestWithRetry(url);
|
|
1350
|
+
return response.logs || [];
|
|
1805
1351
|
} catch (error) {
|
|
1806
|
-
|
|
1352
|
+
this.logger.error(`Error fetching contract logs: ${error.message}`);
|
|
1353
|
+
return null;
|
|
1807
1354
|
}
|
|
1808
1355
|
}
|
|
1809
|
-
|
|
1356
|
+
/**
|
|
1357
|
+
* Retrieves contract logs for a specific contract.
|
|
1358
|
+
* @param contractIdOrAddress The contract ID or EVM address.
|
|
1359
|
+
* @param options Optional parameters for filtering logs.
|
|
1360
|
+
* @returns A promise that resolves to an array of ContractLog or null.
|
|
1361
|
+
*/
|
|
1362
|
+
async getContractLogsByContract(contractIdOrAddress, options) {
|
|
1363
|
+
this.logger.info(
|
|
1364
|
+
`Getting contract logs for contract ${contractIdOrAddress}`
|
|
1365
|
+
);
|
|
1366
|
+
let url = `/api/v1/contracts/${contractIdOrAddress}/results/logs`;
|
|
1367
|
+
const params = new URLSearchParams();
|
|
1368
|
+
if (options?.index) {
|
|
1369
|
+
params.append("index", options.index);
|
|
1370
|
+
}
|
|
1371
|
+
if (options?.limit) {
|
|
1372
|
+
params.append("limit", options.limit.toString());
|
|
1373
|
+
}
|
|
1374
|
+
if (options?.order) {
|
|
1375
|
+
params.append("order", options.order);
|
|
1376
|
+
}
|
|
1377
|
+
if (options?.timestamp) {
|
|
1378
|
+
params.append("timestamp", options.timestamp);
|
|
1379
|
+
}
|
|
1380
|
+
if (options?.topic0) {
|
|
1381
|
+
params.append("topic0", options.topic0);
|
|
1382
|
+
}
|
|
1383
|
+
if (options?.topic1) {
|
|
1384
|
+
params.append("topic1", options.topic1);
|
|
1385
|
+
}
|
|
1386
|
+
if (options?.topic2) {
|
|
1387
|
+
params.append("topic2", options.topic2);
|
|
1388
|
+
}
|
|
1389
|
+
if (options?.topic3) {
|
|
1390
|
+
params.append("topic3", options.topic3);
|
|
1391
|
+
}
|
|
1392
|
+
const queryString = params.toString();
|
|
1393
|
+
if (queryString) {
|
|
1394
|
+
url += `?${queryString}`;
|
|
1395
|
+
}
|
|
1810
1396
|
try {
|
|
1811
|
-
|
|
1397
|
+
const response = await this._requestWithRetry(url);
|
|
1398
|
+
return response.logs || [];
|
|
1812
1399
|
} catch (error) {
|
|
1813
|
-
|
|
1814
|
-
`
|
|
1815
|
-
error instanceof ZodError || error instanceof Error ? error : String(error),
|
|
1816
|
-
value
|
|
1400
|
+
this.logger.error(
|
|
1401
|
+
`Error fetching contract logs for ${contractIdOrAddress}: ${error.message}`
|
|
1817
1402
|
);
|
|
1403
|
+
return null;
|
|
1818
1404
|
}
|
|
1819
1405
|
}
|
|
1820
|
-
|
|
1821
|
-
|
|
1822
|
-
|
|
1406
|
+
/**
|
|
1407
|
+
* Retrieves NFT information by token ID and serial number.
|
|
1408
|
+
* @param tokenId The token ID.
|
|
1409
|
+
* @param serialNumber The serial number of the NFT.
|
|
1410
|
+
* @returns A promise that resolves to an NftInfo or null.
|
|
1411
|
+
*/
|
|
1412
|
+
async getNftInfo(tokenId, serialNumber) {
|
|
1413
|
+
this.logger.info(`Getting NFT info for ${tokenId}/${serialNumber}`);
|
|
1414
|
+
const url = `/api/v1/tokens/${tokenId}/nfts/${serialNumber}`;
|
|
1415
|
+
try {
|
|
1416
|
+
const response = await this._requestWithRetry(url);
|
|
1417
|
+
return response;
|
|
1418
|
+
} catch (error) {
|
|
1419
|
+
this.logger.error(
|
|
1420
|
+
`Error fetching NFT info for ${tokenId}/${serialNumber}: ${error.message}`
|
|
1421
|
+
);
|
|
1422
|
+
return null;
|
|
1823
1423
|
}
|
|
1824
1424
|
}
|
|
1825
|
-
|
|
1826
|
-
|
|
1827
|
-
|
|
1828
|
-
|
|
1829
|
-
|
|
1830
|
-
|
|
1831
|
-
|
|
1832
|
-
};
|
|
1833
|
-
|
|
1834
|
-
|
|
1835
|
-
|
|
1836
|
-
|
|
1837
|
-
const peerPublicKey = this.hexToBuffer(options.peerPublicKey);
|
|
1838
|
-
const shared = secp256k1.getSharedSecret(privateKey, peerPublicKey, true);
|
|
1839
|
-
return createHash("sha256").update(Buffer.from(shared)).digest();
|
|
1840
|
-
}
|
|
1841
|
-
buildCipherEnvelope(options) {
|
|
1842
|
-
this.assertNodeRuntime("encryptCipherEnvelope");
|
|
1843
|
-
const sharedSecret = this.normalizeSharedSecret(options.sharedSecret);
|
|
1844
|
-
const iv = randomBytes(12);
|
|
1845
|
-
const cipher = createCipheriv("aes-256-gcm", sharedSecret, iv);
|
|
1846
|
-
const aadSource = options.associatedData ?? options.sessionId;
|
|
1847
|
-
const associatedDataEncoded = aadSource ? Buffer.from(aadSource, "utf8").toString("base64") : void 0;
|
|
1848
|
-
if (aadSource) {
|
|
1849
|
-
cipher.setAAD(Buffer.from(aadSource, "utf8"));
|
|
1850
|
-
}
|
|
1851
|
-
const ciphertext = Buffer.concat([
|
|
1852
|
-
cipher.update(Buffer.from(options.plaintext, "utf8")),
|
|
1853
|
-
cipher.final()
|
|
1854
|
-
]);
|
|
1855
|
-
const tag = cipher.getAuthTag();
|
|
1856
|
-
const payload = Buffer.concat([ciphertext, tag]);
|
|
1857
|
-
return {
|
|
1858
|
-
algorithm: "aes-256-gcm",
|
|
1859
|
-
ciphertext: payload.toString("base64"),
|
|
1860
|
-
nonce: iv.toString("base64"),
|
|
1861
|
-
associatedData: associatedDataEncoded,
|
|
1862
|
-
keyLocator: {
|
|
1863
|
-
sessionId: options.sessionId,
|
|
1864
|
-
revision: options.revision ?? 1
|
|
1865
|
-
},
|
|
1866
|
-
recipients: options.recipients.map((recipient) => ({
|
|
1867
|
-
...recipient,
|
|
1868
|
-
encryptedShare: ""
|
|
1869
|
-
}))
|
|
1870
|
-
};
|
|
1871
|
-
}
|
|
1872
|
-
openCipherEnvelope(options) {
|
|
1873
|
-
this.assertNodeRuntime("decryptCipherEnvelope");
|
|
1874
|
-
const sharedSecret = this.normalizeSharedSecret(options.sharedSecret);
|
|
1875
|
-
const payload = Buffer.from(options.envelope.ciphertext, "base64");
|
|
1876
|
-
const nonce = Buffer.from(options.envelope.nonce, "base64");
|
|
1877
|
-
const ciphertext = payload.slice(0, payload.length - 16);
|
|
1878
|
-
const tag = payload.slice(payload.length - 16);
|
|
1879
|
-
const decipher = createDecipheriv("aes-256-gcm", sharedSecret, nonce);
|
|
1880
|
-
if (options.envelope.associatedData) {
|
|
1881
|
-
decipher.setAAD(Buffer.from(options.envelope.associatedData, "base64"));
|
|
1882
|
-
}
|
|
1883
|
-
decipher.setAuthTag(tag);
|
|
1884
|
-
const plaintext = Buffer.concat([
|
|
1885
|
-
decipher.update(ciphertext),
|
|
1886
|
-
decipher.final()
|
|
1887
|
-
]);
|
|
1888
|
-
return plaintext.toString(options.encoding ?? "utf8");
|
|
1889
|
-
}
|
|
1890
|
-
normalizeSharedSecret(input) {
|
|
1891
|
-
if (Buffer.isBuffer(input)) {
|
|
1892
|
-
return Buffer.from(input);
|
|
1893
|
-
}
|
|
1894
|
-
if (input instanceof Uint8Array) {
|
|
1895
|
-
return Buffer.from(input);
|
|
1425
|
+
/**
|
|
1426
|
+
* Retrieves NFTs for a specific token.
|
|
1427
|
+
* @param tokenId The token ID.
|
|
1428
|
+
* @param options Optional parameters for filtering NFTs.
|
|
1429
|
+
* @returns A promise that resolves to an array of NftInfo or null.
|
|
1430
|
+
*/
|
|
1431
|
+
async getNftsByToken(tokenId, options) {
|
|
1432
|
+
this.logger.info(`Getting NFTs for token ${tokenId}`);
|
|
1433
|
+
let url = `/api/v1/tokens/${tokenId}/nfts`;
|
|
1434
|
+
const params = new URLSearchParams();
|
|
1435
|
+
if (options?.accountId) {
|
|
1436
|
+
params.append("account.id", options.accountId);
|
|
1896
1437
|
}
|
|
1897
|
-
if (
|
|
1898
|
-
|
|
1438
|
+
if (options?.limit) {
|
|
1439
|
+
params.append("limit", options.limit.toString());
|
|
1899
1440
|
}
|
|
1900
|
-
|
|
1901
|
-
|
|
1902
|
-
bufferFromString(value) {
|
|
1903
|
-
const trimmed = value.trim();
|
|
1904
|
-
if (!trimmed) {
|
|
1905
|
-
throw new Error("sharedSecret string cannot be empty");
|
|
1441
|
+
if (options?.order) {
|
|
1442
|
+
params.append("order", options.order);
|
|
1906
1443
|
}
|
|
1907
|
-
|
|
1908
|
-
|
|
1909
|
-
return Buffer.from(normalized, "hex");
|
|
1444
|
+
if (options?.serialNumber) {
|
|
1445
|
+
params.append("serialnumber", options.serialNumber);
|
|
1910
1446
|
}
|
|
1911
|
-
|
|
1912
|
-
|
|
1913
|
-
|
|
1914
|
-
const normalized = value.startsWith("0x") ? value.slice(2) : value;
|
|
1915
|
-
if (!/^[0-9a-fA-F]+$/.test(normalized) || normalized.length % 2 !== 0) {
|
|
1916
|
-
throw new Error("Expected hex-encoded value");
|
|
1447
|
+
const queryString = params.toString();
|
|
1448
|
+
if (queryString) {
|
|
1449
|
+
url += `?${queryString}`;
|
|
1917
1450
|
}
|
|
1918
|
-
|
|
1919
|
-
|
|
1920
|
-
|
|
1921
|
-
|
|
1922
|
-
|
|
1923
|
-
|
|
1924
|
-
class EncryptedChatManager {
|
|
1925
|
-
constructor(client) {
|
|
1926
|
-
this.client = client;
|
|
1927
|
-
}
|
|
1928
|
-
registerConversationContext(context) {
|
|
1929
|
-
this.client.registerConversationContextForEncryption(context);
|
|
1930
|
-
}
|
|
1931
|
-
async startSession(options) {
|
|
1932
|
-
await this.client.encryptionReady();
|
|
1933
|
-
const session = await this.client.chat.createSession({
|
|
1934
|
-
uaid: options.uaid,
|
|
1935
|
-
senderUaid: options.senderUaid,
|
|
1936
|
-
encryptionRequested: true,
|
|
1937
|
-
historyTtlSeconds: options.historyTtlSeconds,
|
|
1938
|
-
auth: options.auth
|
|
1939
|
-
});
|
|
1940
|
-
options.onSessionCreated?.(session.sessionId);
|
|
1941
|
-
const summary = session.encryption;
|
|
1942
|
-
if (!summary?.enabled) {
|
|
1943
|
-
throw new EncryptionUnavailableError(
|
|
1944
|
-
session.sessionId,
|
|
1945
|
-
session.encryption ?? null
|
|
1451
|
+
try {
|
|
1452
|
+
const response = await this._requestWithRetry(url);
|
|
1453
|
+
return response.nfts || [];
|
|
1454
|
+
} catch (error) {
|
|
1455
|
+
this.logger.error(
|
|
1456
|
+
`Error fetching NFTs for token ${tokenId}: ${error.message}`
|
|
1946
1457
|
);
|
|
1458
|
+
return null;
|
|
1947
1459
|
}
|
|
1948
|
-
const handle = await this.establishRequesterContext({
|
|
1949
|
-
sessionId: session.sessionId,
|
|
1950
|
-
summary,
|
|
1951
|
-
senderUaid: options.senderUaid,
|
|
1952
|
-
handshakeTimeoutMs: options.handshakeTimeoutMs,
|
|
1953
|
-
pollIntervalMs: options.pollIntervalMs
|
|
1954
|
-
});
|
|
1955
|
-
return handle;
|
|
1956
|
-
}
|
|
1957
|
-
async acceptSession(options) {
|
|
1958
|
-
await this.client.encryptionReady();
|
|
1959
|
-
const summary = await this.waitForEncryptionSummary(
|
|
1960
|
-
options.sessionId,
|
|
1961
|
-
options.handshakeTimeoutMs,
|
|
1962
|
-
options.pollIntervalMs
|
|
1963
|
-
);
|
|
1964
|
-
const handle = await this.establishResponderContext({
|
|
1965
|
-
sessionId: options.sessionId,
|
|
1966
|
-
summary,
|
|
1967
|
-
responderUaid: options.responderUaid,
|
|
1968
|
-
handshakeTimeoutMs: options.handshakeTimeoutMs,
|
|
1969
|
-
pollIntervalMs: options.pollIntervalMs
|
|
1970
|
-
});
|
|
1971
|
-
return handle;
|
|
1972
|
-
}
|
|
1973
|
-
async establishRequesterContext(params) {
|
|
1974
|
-
const keyPair = this.client.encryption.generateEphemeralKeyPair();
|
|
1975
|
-
await this.client.chat.submitEncryptionHandshake(params.sessionId, {
|
|
1976
|
-
role: "requester",
|
|
1977
|
-
keyType: "secp256k1",
|
|
1978
|
-
ephemeralPublicKey: keyPair.publicKey,
|
|
1979
|
-
uaid: params.senderUaid ?? params.summary.requester?.uaid ?? void 0
|
|
1980
|
-
});
|
|
1981
|
-
const { summary, record } = await this.waitForHandshakeCompletion(
|
|
1982
|
-
params.sessionId,
|
|
1983
|
-
params.handshakeTimeoutMs,
|
|
1984
|
-
params.pollIntervalMs
|
|
1985
|
-
);
|
|
1986
|
-
const responderKey = record.responder?.ephemeralPublicKey;
|
|
1987
|
-
if (!responderKey) {
|
|
1988
|
-
throw new Error("Responder handshake was not completed in time");
|
|
1989
|
-
}
|
|
1990
|
-
const sharedSecret = this.client.encryption.deriveSharedSecret({
|
|
1991
|
-
privateKey: keyPair.privateKey,
|
|
1992
|
-
peerPublicKey: responderKey
|
|
1993
|
-
}).subarray();
|
|
1994
|
-
const recipients = this.buildRecipients(summary);
|
|
1995
|
-
return this.createHandle({
|
|
1996
|
-
sessionId: params.sessionId,
|
|
1997
|
-
sharedSecret,
|
|
1998
|
-
summary,
|
|
1999
|
-
recipients,
|
|
2000
|
-
identity: summary.requester ?? void 0
|
|
2001
|
-
});
|
|
2002
|
-
}
|
|
2003
|
-
async establishResponderContext(params) {
|
|
2004
|
-
const keyPair = this.client.encryption.generateEphemeralKeyPair();
|
|
2005
|
-
await this.client.chat.submitEncryptionHandshake(params.sessionId, {
|
|
2006
|
-
role: "responder",
|
|
2007
|
-
keyType: "secp256k1",
|
|
2008
|
-
ephemeralPublicKey: keyPair.publicKey,
|
|
2009
|
-
uaid: params.responderUaid ?? params.summary.responder?.uaid ?? void 0
|
|
2010
|
-
});
|
|
2011
|
-
const { summary, record } = await this.waitForHandshakeCompletion(
|
|
2012
|
-
params.sessionId,
|
|
2013
|
-
params.handshakeTimeoutMs,
|
|
2014
|
-
params.pollIntervalMs
|
|
2015
|
-
);
|
|
2016
|
-
const requesterKey = record.requester?.ephemeralPublicKey;
|
|
2017
|
-
if (!requesterKey) {
|
|
2018
|
-
throw new Error("Requester handshake was not detected in time");
|
|
2019
|
-
}
|
|
2020
|
-
const sharedSecret = this.client.encryption.deriveSharedSecret({
|
|
2021
|
-
privateKey: keyPair.privateKey,
|
|
2022
|
-
peerPublicKey: requesterKey
|
|
2023
|
-
}).subarray();
|
|
2024
|
-
const recipients = this.buildRecipients(summary);
|
|
2025
|
-
return this.createHandle({
|
|
2026
|
-
sessionId: params.sessionId,
|
|
2027
|
-
sharedSecret,
|
|
2028
|
-
summary,
|
|
2029
|
-
recipients,
|
|
2030
|
-
identity: summary.responder ?? void 0
|
|
2031
|
-
});
|
|
2032
1460
|
}
|
|
2033
|
-
|
|
2034
|
-
|
|
2035
|
-
|
|
2036
|
-
|
|
2037
|
-
|
|
2038
|
-
|
|
2039
|
-
|
|
2040
|
-
|
|
2041
|
-
|
|
2042
|
-
|
|
2043
|
-
|
|
2044
|
-
}
|
|
2045
|
-
|
|
1461
|
+
/**
|
|
1462
|
+
* Retrieves network information.
|
|
1463
|
+
* @returns A promise that resolves to NetworkInfo or null.
|
|
1464
|
+
*/
|
|
1465
|
+
async getNetworkInfo() {
|
|
1466
|
+
this.logger.info("Getting network information");
|
|
1467
|
+
const url = `/api/v1/network/nodes`;
|
|
1468
|
+
try {
|
|
1469
|
+
const response = await this._requestWithRetry(url);
|
|
1470
|
+
return response;
|
|
1471
|
+
} catch (error) {
|
|
1472
|
+
this.logger.error(`Error fetching network info: ${error.message}`);
|
|
1473
|
+
return null;
|
|
2046
1474
|
}
|
|
2047
1475
|
}
|
|
2048
|
-
|
|
2049
|
-
|
|
2050
|
-
|
|
2051
|
-
|
|
2052
|
-
|
|
2053
|
-
|
|
2054
|
-
|
|
1476
|
+
/**
|
|
1477
|
+
* Retrieves network fees.
|
|
1478
|
+
* @param timestamp Optional timestamp for historical fees.
|
|
1479
|
+
* @returns A promise that resolves to NetworkFees or null.
|
|
1480
|
+
*/
|
|
1481
|
+
async getNetworkFees(timestamp) {
|
|
1482
|
+
this.logger.info("Getting network fees");
|
|
1483
|
+
let url = `/api/v1/network/fees`;
|
|
1484
|
+
if (timestamp) {
|
|
1485
|
+
url += `?timestamp=${timestamp}`;
|
|
1486
|
+
}
|
|
1487
|
+
try {
|
|
1488
|
+
const response = await this._requestWithRetry(url);
|
|
1489
|
+
return response;
|
|
1490
|
+
} catch (error) {
|
|
1491
|
+
this.logger.error(`Error fetching network fees: ${error.message}`);
|
|
1492
|
+
return null;
|
|
2055
1493
|
}
|
|
2056
|
-
return status.encryption;
|
|
2057
1494
|
}
|
|
2058
|
-
|
|
2059
|
-
|
|
2060
|
-
|
|
2061
|
-
|
|
2062
|
-
|
|
2063
|
-
|
|
2064
|
-
|
|
2065
|
-
|
|
2066
|
-
|
|
2067
|
-
}
|
|
2068
|
-
if (candidate.ledgerAccountId) {
|
|
2069
|
-
recipient.ledgerAccountId = candidate.ledgerAccountId;
|
|
2070
|
-
}
|
|
2071
|
-
if (candidate.userId) {
|
|
2072
|
-
recipient.userId = candidate.userId;
|
|
2073
|
-
}
|
|
2074
|
-
if (candidate.email) {
|
|
2075
|
-
recipient.email = candidate.email;
|
|
2076
|
-
}
|
|
2077
|
-
return recipient;
|
|
2078
|
-
}).filter(
|
|
2079
|
-
(entry) => Boolean(
|
|
2080
|
-
entry?.uaid || entry?.ledgerAccountId || entry?.userId || entry?.email
|
|
2081
|
-
)
|
|
2082
|
-
);
|
|
2083
|
-
if (normalized.length > 0) {
|
|
2084
|
-
return normalized;
|
|
1495
|
+
/**
|
|
1496
|
+
* Retrieves network supply information.
|
|
1497
|
+
* @param timestamp Optional timestamp for historical supply data.
|
|
1498
|
+
* @returns A promise that resolves to NetworkSupply or null.
|
|
1499
|
+
*/
|
|
1500
|
+
async getNetworkSupply(timestamp) {
|
|
1501
|
+
this.logger.info("Getting network supply");
|
|
1502
|
+
let url = `/api/v1/network/supply`;
|
|
1503
|
+
if (timestamp) {
|
|
1504
|
+
url += `?timestamp=${timestamp}`;
|
|
2085
1505
|
}
|
|
2086
|
-
|
|
2087
|
-
|
|
1506
|
+
try {
|
|
1507
|
+
const response = await this._requestWithRetry(url);
|
|
1508
|
+
return response;
|
|
1509
|
+
} catch (error) {
|
|
1510
|
+
this.logger.error(`Error fetching network supply: ${error.message}`);
|
|
1511
|
+
return null;
|
|
2088
1512
|
}
|
|
2089
|
-
return [];
|
|
2090
1513
|
}
|
|
2091
|
-
|
|
2092
|
-
|
|
2093
|
-
|
|
2094
|
-
|
|
2095
|
-
|
|
2096
|
-
|
|
2097
|
-
|
|
2098
|
-
|
|
2099
|
-
|
|
2100
|
-
|
|
2101
|
-
sessionId: context.sessionId,
|
|
2102
|
-
message: options.message ?? "[ciphertext omitted]",
|
|
2103
|
-
streaming: options.streaming,
|
|
2104
|
-
auth: options.auth,
|
|
2105
|
-
uaid,
|
|
2106
|
-
encryption: {
|
|
2107
|
-
plaintext: options.plaintext,
|
|
2108
|
-
sharedSecret: Buffer.from(sharedSecret),
|
|
2109
|
-
recipients
|
|
2110
|
-
}
|
|
2111
|
-
});
|
|
2112
|
-
},
|
|
2113
|
-
decryptHistoryEntry: (entry) => this.decryptEntry(entry, context.identity, sharedSecret)
|
|
2114
|
-
};
|
|
2115
|
-
this.registerConversationContext({
|
|
2116
|
-
sessionId: context.sessionId,
|
|
2117
|
-
sharedSecret,
|
|
2118
|
-
identity: context.identity
|
|
2119
|
-
});
|
|
2120
|
-
return handle;
|
|
2121
|
-
}
|
|
2122
|
-
decryptEntry(entry, identity, fallbackSecret) {
|
|
2123
|
-
const envelope = entry.cipherEnvelope;
|
|
2124
|
-
if (!envelope) {
|
|
2125
|
-
return null;
|
|
1514
|
+
/**
|
|
1515
|
+
* Retrieves network stake information.
|
|
1516
|
+
* @param timestamp Optional timestamp for historical stake data.
|
|
1517
|
+
* @returns A promise that resolves to NetworkStake or null.
|
|
1518
|
+
*/
|
|
1519
|
+
async getNetworkStake(timestamp) {
|
|
1520
|
+
this.logger.info("Getting network stake");
|
|
1521
|
+
let url = `/api/v1/network/stake`;
|
|
1522
|
+
if (timestamp) {
|
|
1523
|
+
url += `?timestamp=${timestamp}`;
|
|
2126
1524
|
}
|
|
2127
|
-
const secret = Buffer.from(fallbackSecret);
|
|
2128
1525
|
try {
|
|
2129
|
-
|
|
2130
|
-
|
|
2131
|
-
|
|
2132
|
-
});
|
|
2133
|
-
} catch (_error) {
|
|
1526
|
+
const response = await this._requestWithRetry(url);
|
|
1527
|
+
return response;
|
|
1528
|
+
} catch (error) {
|
|
1529
|
+
this.logger.error(`Error fetching network stake: ${error.message}`);
|
|
2134
1530
|
return null;
|
|
2135
1531
|
}
|
|
2136
1532
|
}
|
|
2137
|
-
|
|
2138
|
-
|
|
2139
|
-
|
|
1533
|
+
/**
|
|
1534
|
+
* Retrieves opcode traces for a specific transaction.
|
|
1535
|
+
* @param transactionIdOrHash The transaction ID or hash.
|
|
1536
|
+
* @param options Optional parameters for trace details.
|
|
1537
|
+
* @returns A promise that resolves to an OpcodesResponse or null.
|
|
1538
|
+
*/
|
|
1539
|
+
async getOpcodeTraces(transactionIdOrHash, options) {
|
|
1540
|
+
this.logger.info(`Getting opcode traces for ${transactionIdOrHash}`);
|
|
1541
|
+
let url = `/api/v1/contracts/results/${transactionIdOrHash}/opcodes`;
|
|
1542
|
+
const params = new URLSearchParams();
|
|
1543
|
+
if (options?.stack !== void 0) {
|
|
1544
|
+
params.append("stack", options.stack.toString());
|
|
2140
1545
|
}
|
|
2141
|
-
if (
|
|
2142
|
-
|
|
1546
|
+
if (options?.memory !== void 0) {
|
|
1547
|
+
params.append("memory", options.memory.toString());
|
|
2143
1548
|
}
|
|
2144
|
-
if (
|
|
2145
|
-
|
|
1549
|
+
if (options?.storage !== void 0) {
|
|
1550
|
+
params.append("storage", options.storage.toString());
|
|
2146
1551
|
}
|
|
2147
|
-
|
|
2148
|
-
|
|
1552
|
+
const queryString = params.toString();
|
|
1553
|
+
if (queryString) {
|
|
1554
|
+
url += `?${queryString}`;
|
|
2149
1555
|
}
|
|
2150
|
-
|
|
2151
|
-
|
|
2152
|
-
|
|
2153
|
-
|
|
2154
|
-
|
|
1556
|
+
try {
|
|
1557
|
+
const response = await this._requestWithRetry(url);
|
|
1558
|
+
return response;
|
|
1559
|
+
} catch (error) {
|
|
1560
|
+
this.logger.error(
|
|
1561
|
+
`Error fetching opcode traces for ${transactionIdOrHash}: ${error.message}`
|
|
1562
|
+
);
|
|
1563
|
+
return null;
|
|
2155
1564
|
}
|
|
2156
|
-
await new Promise((resolve) => setTimeout(resolve, ms));
|
|
2157
|
-
}
|
|
2158
|
-
}
|
|
2159
|
-
class EncryptionUnavailableError extends Error {
|
|
2160
|
-
constructor(sessionId, summary) {
|
|
2161
|
-
super("Encryption is not enabled for this session");
|
|
2162
|
-
this.sessionId = sessionId;
|
|
2163
|
-
this.summary = summary;
|
|
2164
1565
|
}
|
|
2165
1566
|
}
|
|
2166
1567
|
export {
|
|
2167
|
-
|
|
2168
|
-
RegistryBrokerError,
|
|
2169
|
-
RegistryBrokerParseError,
|
|
2170
|
-
isPartialRegisterAgentResponse,
|
|
2171
|
-
isPendingRegisterAgentResponse,
|
|
2172
|
-
isSuccessRegisterAgentResponse
|
|
1568
|
+
HederaMirrorNode
|
|
2173
1569
|
};
|
|
2174
1570
|
//# sourceMappingURL=standards-sdk.es127.js.map
|