Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Martinh/sdk layout #125

Open
wants to merge 15 commits into
base: main
Choose a base branch
from
Open
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
@@ -0,0 +1,6 @@
const exampleLayout = [
{ name: 'sourceChain', binary: 'uint', size: 2 },
{ name: 'orderSender', binary: 'bytes', size: 32 },
{ name: 'redeemer', binary: 'bytes', size: 32 },
{ name: 'redeemerMessage', binary: 'bytes', lengthSize: 4 },
] as const;
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
const chainCustomConversion = {
to: (chainId: number) => toChain(chainId),
from: (chain: Chain) => chainToChainId(chain),
} satisfies CustomConversion<number, Chain>;
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// Validate data structure before serialization
const exampleFill = {
sourceChain: 6,
orderSender: new Uint8Array(32), // ensure correct size and type
// more fields...
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
try {
const deserialized = deserializeLayout(fillLayout, data);
} catch (error) {
console.error('Deserialization failed:', error);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
const commonLayout = [
{ name: 'chainId', binary: 'uint', size: 2 },
{ name: 'address', binary: 'bytes', size: 32 },
];

// Reuse the common layout in different contexts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
const switchLayout = {
binary: 'switch',
idSize: 1, // size of the payload ID
idTag: 'messageType', // tag to identify the type of message
layouts: [
[[1, 'messageType1'], fillLayout], // layout for type 1
[[2, 'messageType2'], fastFillLayout], // layout for type 2
],
} as const satisfies Layout;
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
const fixedConversionLayout = {
binary: 'uint',
size: 2,
custom: {
to: 'Ethereum',
from: chainToChainId('Ethereum'),
},
} as const satisfies Layout;
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
const omittedFieldLayout = [
{ name: 'sourceChain', binary: 'uint', size: 2, omit: true }, // omitted from deserialization
] as const satisfies Layout;
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
const vaaLayout = [
{ name: 'version', binary: 'uint', size: 1 },
{ name: 'guardianSetIndex', binary: 'uint', size: 4 },
{ name: 'timestamp', binary: 'uint', size: 4 },
{ name: 'emitterChain', binary: 'uint', size: 2 },
{ name: 'emitterAddress', binary: 'bytes', size: 32 },
{ name: 'sequence', binary: 'uint', size: 8 },
{ name: 'consistencyLevel', binary: 'uint', size: 1 },
] as const satisfies Layout;
11 changes: 11 additions & 0 deletions .snippets/code/build/toolkit/wormhole-sdk/sdk-layout/layout-17.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
const vaaData = {
version: 1,
guardianSetIndex: 5,
timestamp: 1633000000,
emitterChain: 2, // Ethereum
emitterAddress: new Uint8Array(32).fill(0),
sequence: BigInt(1),
consistencyLevel: 1,
};

const serializedVAA = serializeLayout(vaaLayout, vaaData);
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
const customPayloadLayout = [
{ name: 'protocolId', binary: 'uint', size: 4 },
{ name: 'payload', binary: 'bytes', lengthSize: 4 },
] as const satisfies Layout;

const serializedCustomPayload = serializeLayout(customPayloadLayout, {
protocolId: 1234,
payload: new Uint8Array([0x01, 0x02, 0x03]),
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
const commonLayouts = [
{ name: 'chainId', binary: 'uint', size: 2 },
{ name: 'address', binary: 'bytes', size: 32 },
];
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
try {
deserializeLayout(fillLayout, corruptedData);
} catch (error) {
console.error('Error during deserialization:', error.message);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
const exampleLayout = [
{ name: 'sourceChain', binary: 'uint', size: 2 },
{ name: 'orderSender', binary: 'bytes', size: 32 },
{ name: 'redeemer', binary: 'bytes', size: 32 },
{ name: 'redeemerMessage', binary: 'bytes', lengthSize: 4 },
] as const;
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
const examplePayload = {
sourceChain: 6,
orderSender: new Uint8Array(32),
redeemer: new Uint8Array(32),
redeemerMessage: new Uint8Array([0x01, 0x02, 0x03]),
};

const serializedData = serializeLayout(exampleLayout, examplePayload);
console.log(serializedData);
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
const deserializedPayload = deserializeLayout(exampleLayout, serializedData);
console.log(deserializedPayload);
18 changes: 18 additions & 0 deletions .snippets/code/build/toolkit/wormhole-sdk/sdk-layout/layout-6.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
const nestedLayout = [
{
name: 'source',
binary: 'object',
layout: [
{ name: 'chainId', binary: 'uint', size: 2 },
{ name: 'sender', binary: 'bytes', size: 32 },
],
},
{
name: 'redeemer',
binary: 'object',
layout: [
{ name: 'address', binary: 'bytes', size: 32 },
{ name: 'message', binary: 'bytes', lengthSize: 4 },
],
},
] as const satisfies Layout;
10 changes: 10 additions & 0 deletions .snippets/code/build/toolkit/wormhole-sdk/sdk-layout/layout-7.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
const message: NestedMessage = {
source: {
chainId: 6,
sender: new Uint8Array(32),
},
redeemer: {
address: new Uint8Array(32),
message: new Uint8Array([0x01, 0x02, 0x03]),
},
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
const serializedNested = serializeLayout(nestedLayout, message);
const deserializedNested = deserializeLayout(nestedLayout, serializedNested);

console.log(deserializedNested);
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
const SIZE_32 = 32;
martin0995 marked this conversation as resolved.
Show resolved Hide resolved
const UINT_TYPE = 'uint';

// Example layout using constants
const exampleLayout = [
{ name: 'orderSender', binary: 'bytes', size: SIZE_32 },
{ name: 'sourceChain', binary: UINT_TYPE, size: 2 },
] as const;
2 changes: 1 addition & 1 deletion build/applications/.pages
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
title: Build Frontend Applications
nav:
- index.md
- 'Wormhole SDK': 'wormhole-sdk.md'
- wormhole-sdk
- queries
- connect
6 changes: 6 additions & 0 deletions build/applications/wormhole-sdk/.pages
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
title: Wormhole SDK
nav:
- index.md
- 'TypeScript SDK' : 'wormhole-sdk.md'
- 'Data Layouts': 'sdk-layout.md'

30 changes: 30 additions & 0 deletions build/applications/wormhole-sdk/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
---
title: Wormhole SDK
description: The Wormhole SDK provides tools for cross-chain communication, token bridges, and more, enabling developers to integrate with multiple blockchain environments.
---

# Wormhole SDK

## Get Started

The Wormhole SDK provides developers with essential tools for cross-chain communication, token bridges, and more. This SDK enables seamless interaction between different blockchain environments with a focus on performance and usability.

<div class="grid cards" markdown>

- :octicons-book-16:{ .lg .middle } **Wormhole SDK**

---

Learn about the core functionalities of the Wormhole SDK, including how to use its features for building cross-chain applications.

[:octicons-arrow-right-16: Explore the SDK](/docs/build/applications/wormhole-sdk/wormhole-sdk/)

- :octicons-code-16:{ .lg .middle } **Layouts**

---

Discover how to define, serialize, and deserialize data structures using the Wormhole SDK's layout system, ensuring efficient cross-chain communication.

[:octicons-arrow-right-16: Learn about Layouts](/docs/build/applications/wormhole-sdk/sdk-layout/)

</div>
Loading
Loading