Skip to content

Commit

Permalink
Extend u8aToBuffer check with hasBuffer (#1871)
Browse files Browse the repository at this point in the history
* Extend u8aToBuffer check with hasBuffer

* CHANGELOG

* Always return Uint8Array

* New instance of fallback
  • Loading branch information
jacogr authored Aug 23, 2023
1 parent aac9020 commit f4e0e21
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 1 deletion.
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;
}

0 comments on commit f4e0e21

Please sign in to comment.