Skip to content

Commit

Permalink
fix: pss target length verification
Browse files Browse the repository at this point in the history
  • Loading branch information
AuHau committed Aug 2, 2021
1 parent dcd6dd3 commit 7e07ecd
Show file tree
Hide file tree
Showing 5 changed files with 10 additions and 14 deletions.
2 changes: 1 addition & 1 deletion src/modules/pss.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export async function send(
): Promise<void> {
await safeAxios<BeeGenericResponse>({
method: 'post',
url: `${url}${endpoint}/send/${topic}/${target.slice(0, 4)}`,
url: `${url}${endpoint}/send/${topic}/${target}`,
data: await prepareData(data),
responseType: 'json',
params: { recipient },
Expand Down
1 change: 1 addition & 0 deletions src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export interface Dictionary<T> {
}

export const ADDRESS_HEX_LENGTH = 64
export const PSS_TARGET_HEX_LENGTH = 4
export const PUBKEY_HEX_LENGTH = 66
export const BATCH_ID_HEX_LENGTH = 64
export const REFERENCE_HEX_LENGTH = 64
Expand Down
5 changes: 3 additions & 2 deletions src/utils/type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import {
AllTagsOptions,
TAGS_LIMIT_MIN,
TAGS_LIMIT_MAX,
PSS_TARGET_HEX_LENGTH,
} from '../types'
import { BeeArgumentError } from './error'
import { isFile } from './file'
Expand Down Expand Up @@ -193,9 +194,9 @@ export function assertTag(value: unknown): asserts value is Tag {
export function assertAddressPrefix(value: unknown): asserts value is AddressPrefix {
assertHexString(value, undefined, 'AddressPrefix')

if (value.length > ADDRESS_HEX_LENGTH) {
if (value.length > PSS_TARGET_HEX_LENGTH) {
throw new BeeArgumentError(
`AddressPrefix must have length of ${ADDRESS_HEX_LENGTH} at most! Got string with ${value.length}`,
`AddressPrefix must have length of ${PSS_TARGET_HEX_LENGTH} at most! Got string with ${value.length}`,
value,
)
}
Expand Down
4 changes: 2 additions & 2 deletions test/integration/bee-class.browser.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ describe('Bee class - in browser', () => {
const beePeer = new window.BeeJs.Bee(BEE_PEER_URL)

const receive = bee.pssReceive(topic)
await beePeer.pssSend(batchIdPeer, topic, overlay, message)
await beePeer.pssSend(batchIdPeer, topic, overlay.slice(0, 4), message)

const msg = await receive

Expand Down Expand Up @@ -191,7 +191,7 @@ describe('Bee class - in browser', () => {
const beePeer = new window.BeeJs.Bee(BEE_PEER_URL)

const receive = bee.pssReceive(topic)
await beePeer.pssSend(batchIdPeer, topic, overlay, message, pssPublicKey)
await beePeer.pssSend(batchIdPeer, topic, overlay.slice(0, 4), message, pssPublicKey)

const msg = await receive

Expand Down
12 changes: 3 additions & 9 deletions test/unit/assertions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,19 +146,13 @@ export function testAddressPrefixAssertions(executor: (input: unknown) => void):
await expect(() => executor('')).rejects.toThrow(TypeError)

// Not an valid hexstring (ZZZ)
await expect(() => executor('ZZZfb5a872396d9693e5c9f9d7233cfa93f395c093371017ff44aa9ae6564cdd')).rejects.toThrow(
TypeError,
)
await expect(() => executor('ZZZf')).rejects.toThrow(TypeError)

// Prefixed hexstring is not accepted
await expect(() => executor('0x634fb5a872396d9693e5c9f9d7233cfa93f395c093371017ff44aa9ae6564cdd')).rejects.toThrow(
TypeError,
)
await expect(() => executor('0x634f')).rejects.toThrow(TypeError)

// Too long hexstring
await expect(() => executor('123634fb5a872396d9693e5c9f9d7233cfa93f395c093371017ff44aa9ae6564cdd')).rejects.toThrow(
BeeArgumentError,
)
await expect(() => executor('12364')).rejects.toThrow(BeeArgumentError)
})
}

Expand Down

0 comments on commit 7e07ecd

Please sign in to comment.