Skip to content

Commit a614bac

Browse files
committed
fix(4mat@2.0.4): non-browser versions of all random getters
1 parent 6280f41 commit a614bac

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

4mat/4mat.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,27 @@
11
export * from './4mat.browser'
22

33
import { webcrypto } from 'node:crypto'
4+
import { base16, base64, bech32, bech32m } from '@scure/base'
45

56
/** Returns Uint8Array of given length. */
67
export const randomBytes = (n: number = 16) =>
78
webcrypto.getRandomValues(new Uint8Array(n))
9+
10+
/** Returns a hex-encoded string of given length.
11+
* Default is 16 bytes, i.e. 128 bits of entropy. */
12+
export const randomBase16 = (n: number = 16) =>
13+
base16.encode(randomBytes(n))
14+
15+
/** Returns a base64-encoded string of given length.
16+
* Default is 64 bytes, i.e. 512 bits of entropy. */
17+
export const randomBase64 = (n: number = 64) =>
18+
base64.encode(randomBytes(n))
19+
20+
/** Returns a random valid bech32 address.
21+
* Default length is 32 bytes (canonical addr in Cosmos) */
22+
export const randomBech32 = (prefix = 'hackbg', n = 32) =>
23+
bech32.encode(prefix, bech32.toWords(randomBytes(n)))
24+
25+
/** Returns a random valid bech32m address. */
26+
export const randomBech32m = (prefix = 'hackbg', n = 32) =>
27+
bech32m.encode(prefix, bech32m.toWords(randomBytes(n)))

4mat/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@hackbg/4mat",
3-
"version": "2.0.3",
3+
"version": "2.0.4",
44
"main": "4mat.ts",
55
"browser": "4mat.browser.ts",
66
"type": "module",

0 commit comments

Comments
 (0)