-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: use @spartan-hc/holo-hash for managing holo-hashes (#259)
* Use @spartan-hc/holo-hash for base64 utils * Update hash-parts.ts methods to support HoloHash classes * Export HoloHash class types but keep support for Uint8Array input * Use explicit check for expected type Co-authored-by: Jost Schulte <[email protected]> --------- Co-authored-by: Jost Schulte <[email protected]>
- Loading branch information
1 parent
d01fc80
commit b410db4
Showing
10 changed files
with
100 additions
and
101 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,77 +1,77 @@ | ||
import { range } from "lodash-es"; | ||
import { randomByteArray } from "../api/zome-call-signing.js"; | ||
import { DnaHash, ActionHash, AgentPubKey, EntryHash } from "../types.js"; | ||
import { dhtLocationFrom32 } from "./hash-parts.js"; | ||
|
||
async function fakeValidHash<T extends Uint8Array>( | ||
prefix: number[], | ||
coreByte: number | undefined | ||
): Promise<Uint8Array> { | ||
let core; | ||
if (coreByte === undefined) { | ||
core = await randomByteArray(32); | ||
} else { | ||
core = Uint8Array.from(range(0, 32).map(() => coreByte)); | ||
} | ||
const checksum = dhtLocationFrom32(core); | ||
|
||
return new Uint8Array([...prefix, ...core, ...Array.from(checksum)]) as T; | ||
} | ||
|
||
/** | ||
* Generate a valid hash of a non-existing entry. | ||
* | ||
* From https://github.com/holochain/holochain/blob/develop/crates/holo_hash/src/hash_type/primitive.rs | ||
* | ||
* @param coreByte - Optionally specify a byte to repeat for all core 32 bytes. If undefined will generate random core 32 bytes. | ||
* @returns An {@link EntryHash}. | ||
* @returns An instance of EntryHash. | ||
* | ||
* @public | ||
*/ | ||
export async function fakeEntryHash( | ||
coreByte: number | undefined = undefined | ||
): Promise<EntryHash> { | ||
return fakeValidHash<EntryHash>([0x84, 0x21, 0x24], coreByte); | ||
return new EntryHash( | ||
coreByte | ||
? Uint8Array.from(range(0, 32).map(() => coreByte)) | ||
: await randomByteArray(32) | ||
); | ||
} | ||
|
||
/** | ||
* Generate a valid agent key of a non-existing agent. | ||
* | ||
* @param coreByte - Optionally specify a byte to repeat for all core 32 bytes. If undefined will generate random core 32 bytes. | ||
* @returns An {@link AgentPubKey}. | ||
* @returns An instance ofAgentPubKey. | ||
* | ||
* @public | ||
*/ | ||
export async function fakeAgentPubKey( | ||
coreByte: number | undefined = undefined | ||
): Promise<AgentPubKey> { | ||
return fakeValidHash<AgentPubKey>([0x84, 0x20, 0x24], coreByte); | ||
return new AgentPubKey( | ||
coreByte | ||
? Uint8Array.from(range(0, 32).map(() => coreByte)) | ||
: await randomByteArray(32) | ||
); | ||
} | ||
|
||
/** | ||
* Generate a valid hash of a non-existing action. | ||
* | ||
* @param coreByte - Optionally specify a byte to repeat for all core 32 bytes. If undefined will generate random core 32 bytes. | ||
* @returns An {@link ActionHash}. | ||
* @returns An instance of ActionHash. | ||
* | ||
* @public | ||
*/ | ||
export async function fakeActionHash( | ||
coreByte: number | undefined = undefined | ||
): Promise<ActionHash> { | ||
return fakeValidHash<ActionHash>([0x84, 0x29, 0x24], coreByte); | ||
return new ActionHash( | ||
coreByte | ||
? Uint8Array.from(range(0, 32).map(() => coreByte)) | ||
: await randomByteArray(32) | ||
); | ||
} | ||
|
||
/** | ||
* Generate a valid hash of a non-existing DNA. | ||
* | ||
* @param coreByte - Optionally specify a byte to repeat for all core 32 bytes. If undefined will generate random core 32 bytes. | ||
* @returns A {@link DnaHash}. | ||
* @returns A instance of DnaHash. | ||
* | ||
* @public | ||
*/ | ||
export async function fakeDnaHash( | ||
coreByte: number | undefined = undefined | ||
): Promise<DnaHash> { | ||
return fakeValidHash<DnaHash>([0x84, 0x2d, 0x24], coreByte); | ||
return new DnaHash( | ||
coreByte | ||
? Uint8Array.from(range(0, 32).map(() => coreByte)) | ||
: await randomByteArray(32) | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters