Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ describe('HypergraphSpaceContext', () => {
name: Type.Text,
}) {}

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

let repo = new Repo({});
let wrapper = ({ children }: Readonly<{ children: React.ReactNode }>) => (
Expand Down
13 changes: 6 additions & 7 deletions packages/hypergraph/src/utils/automergeId.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
import bs58check from 'bs58check';

import { decodeBase58, encodeBase58 } from './internal/base58Utils.js';
import { parse as parseUuid, stringify as stringifyUuid } from 'uuid';

/**
* Converts a raw Base58-encoded UUID into Base58Check
* Converts a UUID into Base58Check
*/
export function idToAutomergeId(rawBase58Uuid: string, _versionByte = 0x00) {
const payload = decodeBase58(rawBase58Uuid);
export function idToAutomergeId(uuid: string, _versionByte = 0x00) {
const payload = parseUuid(uuid);
return bs58check.encode(payload);
}

/**
* Converts a Base58Check-encoded UUID back to raw Base58
* Converts a Base58Check-encoded UUID back to UUID
*/
export function automergeIdToId(base58CheckUuid: string) {
const versionedPayload = bs58check.decode(base58CheckUuid);
return encodeBase58(versionedPayload);
return stringifyUuid(versionedPayload);
}
12 changes: 3 additions & 9 deletions packages/hypergraph/src/utils/generateId.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,18 @@
import { v4 as uuidv4 } from 'uuid';

import { encodeBase58 } from './base58.js';

/**
* Generate a v4 UUID.
* Remove the dashes to make it a 32bit value.
* Base58 encode it and return.
*
* @example
* ```
* import { generateId } from '@graph-framework/utils'
*
* const id = generateId()
* console.log(id) // Gw9uTVTnJdhtczyuzBkL3X
* console.log(id)
* ```
*
* @returns base58 encoded v4 UUID
* @returns v4 UUID
*/
export function generateId() {
const uuid = uuidv4();
const stripped = uuid.replaceAll(/-/g, '');
return encodeBase58(stripped);
return uuidv4();
}
2 changes: 1 addition & 1 deletion packages/hypergraph/test/entity/entity.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ describe('Entity', () => {
name: Type.Text,
}) {}

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

let repo: Repo;
Expand Down
2 changes: 1 addition & 1 deletion packages/hypergraph/test/entity/findMany.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ describe('findMany with filters', () => {
category: Type.Text,
}) {}

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

let repo: Repo;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ describe('sign updates and recover key', () => {
const signaturePrivateKeyBytes = secp256k1.utils.randomPrivateKey();
const signaturePrivateKey = bytesToHex(signaturePrivateKeyBytes);
const signaturePublicKey = bytesToHex(secp256k1.getPublicKey(signaturePrivateKeyBytes));
const spaceId = '0x1234';
const spaceId = '1e5e39da-a00d-4fd8-b53b-98095337112f';
const updateId = bytesToHex(randomBytes(32));

const message = hexToBytes('0x01234abcdef01234');
Expand Down
6 changes: 3 additions & 3 deletions packages/hypergraph/test/utils/generateId.test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { validate as validateUuid } from 'uuid';
import { expect, it } from 'vitest';

import { generateId } from '../../src/utils/generateId.js';

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