-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #20 from MystenLabs/mh--add-typesafe-wasm-bindings
Add typesafe interface over wasm bindings
- Loading branch information
Showing
6 changed files
with
121 additions
and
34 deletions.
There are no files selected for viewing
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
Binary file not shown.
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 |
---|---|---|
@@ -0,0 +1,83 @@ | ||
// Copyright (c) Mysten Labs, Inc. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
import { BlobEncoder, from_signed_messages_and_indices, MessageType } from '@mysten/walrus-wasm'; | ||
|
||
import { blobIdFromBytes } from './utils/bcs.js'; | ||
import type { BlobMetadataWithId, SliverPair } from './utils/bcs.js'; | ||
|
||
export interface EncodedBlob { | ||
sliverPairs: (typeof SliverPair.$inferInput)[]; | ||
metadata: typeof BlobMetadataWithId.$inferInput & { blob_id: string }; | ||
rootHash: Uint8Array; | ||
} | ||
|
||
export function encodeBlob(nShards: number, bytes: Uint8Array): EncodedBlob { | ||
const encoder = new BlobEncoder(nShards); | ||
|
||
const [sliverPairs, metadata, rootHash] = encoder.encode_with_metadata(bytes); | ||
|
||
return { | ||
sliverPairs, | ||
metadata: { | ||
...metadata, | ||
blob_id: blobIdFromBytes(new Uint8Array(metadata.blob_id)), | ||
}, | ||
rootHash: new Uint8Array(rootHash.Digest), | ||
}; | ||
} | ||
|
||
export interface CombinedSignature { | ||
signers: number[]; | ||
serializedMessage: Uint8Array; | ||
signature: string; | ||
} | ||
|
||
export interface Confirmation { | ||
serializedMessage: string; | ||
signature: string; | ||
} | ||
|
||
export function combineSignatures( | ||
confirmations: Confirmation[], | ||
signerIndices: number[], | ||
): CombinedSignature { | ||
const combined = from_signed_messages_and_indices( | ||
MessageType.Confirmation, | ||
confirmations, | ||
new Uint16Array(signerIndices), | ||
) as { | ||
signers: number[]; | ||
serialized_message: number[]; | ||
signature: string; | ||
}; | ||
|
||
return { | ||
signers: signerIndices, | ||
serializedMessage: new Uint8Array(combined.serialized_message), | ||
signature: combined.signature, | ||
}; | ||
} | ||
|
||
export function decodePrimarySlivers( | ||
nShards: number, | ||
size: number | bigint, | ||
sliverPairs: (typeof SliverPair.$inferInput)[], | ||
): Uint8Array { | ||
const encoder = new BlobEncoder(nShards); | ||
const bytes = encoder.decode_primary(BigInt(size), sliverPairs); | ||
return new Uint8Array(bytes); | ||
} | ||
|
||
export function computeMetadata( | ||
nShards: number, | ||
bytes: Uint8Array, | ||
): typeof BlobMetadataWithId.$inferInput & { blob_id: string } { | ||
const encoder = new BlobEncoder(nShards); | ||
const metadata = encoder.compute_metadata(bytes); | ||
|
||
return { | ||
...metadata, | ||
blob_id: blobIdFromBytes(new Uint8Array(metadata.blob_id)), | ||
}; | ||
} |