Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extend u8aToBuffer check with hasBuffer #1871

Merged
merged 4 commits into from
Aug 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
Changes:

- Fix `u8aTo{BigInt, Bn, Number}` for non-negative `i{8, 16, 32...}` inputs
- Extend `u8aToBuffer` with `hasBuffer` check


## 12.4.1 Aug 17, 2023
Expand Down
6 changes: 5 additions & 1 deletion packages/util/src/u8a/toBuffer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import type { BufferClass, BufferObject } from '../types.js';

import { xglobal } from '@polkadot/x-global';

import { hasBuffer } from '../has.js';

/**
* @name u8aToBuffer
* @summary Creates a Buffer object from a hex string.
Expand All @@ -20,5 +22,7 @@ import { xglobal } from '@polkadot/x-global';
* ```
*/
export function u8aToBuffer <T = BufferObject> (value?: Uint8Array | null): T {
return (xglobal.Buffer as unknown as BufferClass).from(value || []);
return hasBuffer
? (xglobal.Buffer as unknown as BufferClass).from(value || [])
: new Uint8Array(value || []) as T;
}