Skip to content

Commit f0996c0

Browse files
authored
switch to uuid (#219)
1 parent ec0fb5c commit f0996c0

File tree

7 files changed

+16
-23
lines changed

7 files changed

+16
-23
lines changed

packages/hypergraph-react/test/HypergraphSpaceContext.test.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ describe('HypergraphSpaceContext', () => {
3434
name: Type.Text,
3535
}) {}
3636

37-
const spaceId = '52gTkePWSoGdXmgZF3nRU';
37+
const spaceId = '1e5e39da-a00d-4fd8-b53b-98095337112f';
3838

3939
let repo = new Repo({});
4040
let wrapper = ({ children }: Readonly<{ children: React.ReactNode }>) => (
Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,18 @@
11
import bs58check from 'bs58check';
2-
3-
import { decodeBase58, encodeBase58 } from './internal/base58Utils.js';
2+
import { parse as parseUuid, stringify as stringifyUuid } from 'uuid';
43

54
/**
6-
* Converts a raw Base58-encoded UUID into Base58Check
5+
* Converts a UUID into Base58Check
76
*/
8-
export function idToAutomergeId(rawBase58Uuid: string, _versionByte = 0x00) {
9-
const payload = decodeBase58(rawBase58Uuid);
7+
export function idToAutomergeId(uuid: string, _versionByte = 0x00) {
8+
const payload = parseUuid(uuid);
109
return bs58check.encode(payload);
1110
}
1211

1312
/**
14-
* Converts a Base58Check-encoded UUID back to raw Base58
13+
* Converts a Base58Check-encoded UUID back to UUID
1514
*/
1615
export function automergeIdToId(base58CheckUuid: string) {
1716
const versionedPayload = bs58check.decode(base58CheckUuid);
18-
return encodeBase58(versionedPayload);
17+
return stringifyUuid(versionedPayload);
1918
}
Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,18 @@
11
import { v4 as uuidv4 } from 'uuid';
22

3-
import { encodeBase58 } from './base58.js';
4-
53
/**
64
* Generate a v4 UUID.
7-
* Remove the dashes to make it a 32bit value.
8-
* Base58 encode it and return.
95
*
106
* @example
117
* ```
128
* import { generateId } from '@graph-framework/utils'
139
*
1410
* const id = generateId()
15-
* console.log(id) // Gw9uTVTnJdhtczyuzBkL3X
11+
* console.log(id)
1612
* ```
1713
*
18-
* @returns base58 encoded v4 UUID
14+
* @returns v4 UUID
1915
*/
2016
export function generateId() {
21-
const uuid = uuidv4();
22-
const stripped = uuid.replaceAll(/-/g, '');
23-
return encodeBase58(stripped);
17+
return uuidv4();
2418
}

packages/hypergraph/test/entity/entity.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ describe('Entity', () => {
2525
name: Type.Text,
2626
}) {}
2727

28-
const spaceId = '52gTkePWSoGdXmgZF3nRU';
28+
const spaceId = '1e5e39da-a00d-4fd8-b53b-98095337112f';
2929
const automergeDocId = idToAutomergeId(spaceId);
3030

3131
let repo: Repo;

packages/hypergraph/test/entity/findMany.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ describe('findMany with filters', () => {
2020
category: Type.Text,
2121
}) {}
2222

23-
const spaceId = '52gTkePWSoGdXmgZF3nRU';
23+
const spaceId = '1e5e39da-a00d-4fd8-b53b-98095337112f';
2424
const automergeDocId = idToAutomergeId(spaceId);
2525

2626
let repo: Repo;

packages/hypergraph/test/messages/signed-update-message.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ describe('sign updates and recover key', () => {
1111
const signaturePrivateKeyBytes = secp256k1.utils.randomPrivateKey();
1212
const signaturePrivateKey = bytesToHex(signaturePrivateKeyBytes);
1313
const signaturePublicKey = bytesToHex(secp256k1.getPublicKey(signaturePrivateKeyBytes));
14-
const spaceId = '0x1234';
14+
const spaceId = '1e5e39da-a00d-4fd8-b53b-98095337112f';
1515
const updateId = bytesToHex(randomBytes(32));
1616

1717
const message = hexToBytes('0x01234abcdef01234');
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1+
import { validate as validateUuid } from 'uuid';
12
import { expect, it } from 'vitest';
2-
33
import { generateId } from '../../src/utils/generateId.js';
44

5-
it('should generate a base58 encoded uuid of 22 char length', () => {
5+
it('should generate a valid uuid', () => {
66
const id = generateId();
77
expect(id).toBeTypeOf('string');
8-
expect(id.length === 21 || id.length === 22).toBe(true);
8+
expect(validateUuid(id)).toBe(true);
99
});

0 commit comments

Comments
 (0)