Skip to content

Commit

Permalink
feat: web wasm sdk rhp4
Browse files Browse the repository at this point in the history
  • Loading branch information
alexfreska committed Feb 8, 2024
1 parent 83ebc85 commit 44a84f0
Show file tree
Hide file tree
Showing 34 changed files with 2,077 additions and 52 deletions.
5 changes: 4 additions & 1 deletion .github/actions/setup/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,10 @@ runs:
registry-url: https://registry.npmjs.org
- uses: actions/setup-go@v4
with:
go-version: 'stable'
go-version: '1.20'
- uses: acifani/setup-tinygo@v2
with:
tinygo-version: '0.30.0'
- name: Set up node cache
uses: actions/cache@v3
with:
Expand Down
6 changes: 6 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,11 @@
],
"[go]": {
"editor.defaultFormatter": "golang.go"
},
"gopls": {
"build.env": {
"GOOS": "js",
"GOARCH": "wasm"
}
}
}
40 changes: 15 additions & 25 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,35 +8,16 @@ Web packages for the Sia project and ecosystem.

User interfaces for the Sia software. The latest Sia software takes a modular approach, with separate daemons and user interfaces for renting, hosting, the explorer, and advanced wallet functionality.

### [renterd](apps/renterd)

![stability-mature](https://img.shields.io/badge/stability-mature-008000.svg)

The [`renterd`](https://github.com/siafoundation/renterd) user interface, focused on renting functionality.

### [hostd](apps/hostd)

![stability-mature](https://img.shields.io/badge/stability-mature-008000.svg)

The [`hostd`](https://github.com/siafoundation/hostd) user interface, focused on hosting functionality.

### [walletd](apps/walletd)

![stability-alpha](https://img.shields.io/badge/stability-alpha-orange.svg)

The [`walletd`](https://github.com/siafoundation/walletd) user interface, includes a wallet with support for hot, cold, and hardware wallets.

### [explorer](apps/explorer)

![stability-mature](https://img.shields.io/badge/stability-mature-008000.svg)

The `explorer` user interface, a Sia blockchain explorer interface that powers [siascan.com](https://siascan.com) and [zen.siascan.com](https://zen.siascan.com).
- [renterd](apps/renterd) - The [`renterd`](https://github.com/siafoundation/renterd) user interface, focused on renting functionality.
- [hostd](apps/hostd) - The [`hostd`](https://github.com/siafoundation/hostd) user interface, focused on hosting functionality.
- [walletd](apps/walletd) - The [`walletd`](https://github.com/siafoundation/walletd) user interface, includes a wallet with support for hot, cold, and hardware wallets.
- [explorer](apps/explorer) - The `explorer` user interface, a Sia blockchain explorer interface that powers [siascan.com](https://siascan.com) and [zen.siascan.com](https://zen.siascan.com).

## Libraries

![stability-wip](https://img.shields.io/badge/stability-work_in_progress-orange.svg)
![stability-beta](https://img.shields.io/badge/stability-beta-yellow.svg)

> ⚠️ Please note that all libraries are currently unstable and subject to change or completely move. Do not depend on these libraries until version 1.0.0.
### TypeScript

The Sia web libraries provide developers with convenient TypeScript SDKs for using Sia core types, blockchain utilities, data fetching, daemon-specific React hooks, and components for common functionality such as Siacoin/fiat input fields, transaction lists, files, and more.

Expand All @@ -53,6 +34,15 @@ The Sia web libraries provide developers with convenient TypeScript SDKs for usi
- [@siafoundation/units](libs/units) - Methods and types for converting and displaying units.
- [@siafoundation/types](libs/types) - Core Sia types and library methods.

### Go

- [go.sia.tech/web/ui](ui) - Library for embedding NextJS applications in Go.
- [go.sia.tech/web/sdk](sdk) - SDK for encoding RPCs, computing merkle proofs, and more. Compiled with WASM for use in the TypeScript SDK.
- [go.sia.tech/web/walletd/wasm](walletd/wasm) - Wallet library for signing transactions. Compiled with WASM for use in the `walletd` application.
- [go.sia.tech/web/walletd](walletd) - HTTP handler with embedded `walletd` application.
- [go.sia.tech/web/renterd](renterd) - HTTP handler with embedded `renterd` application.
- [go.sia.tech/web/hostd](hostd) - HTTP handler with embedded `hostd` application.

## Internal

- [website](apps/website) - The main [sia.tech](https://sia.tech) website with information on the Sia project and the Sia Foundation.
Expand Down
12 changes: 6 additions & 6 deletions apps/renterd/next.config.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const { composePlugins, withNx } = require('@nx/next');
const { composePlugins, withNx } = require('@nx/next')

/**
* @type {import('@nx/next/plugins/with-nx').WithNxOptions}
Expand Down Expand Up @@ -32,10 +32,10 @@ const nextConfigExport = {
output: 'export',
}

const nextConfig = process.env.NEXT_OUTPUT_EXPORT ? nextConfigExport : nextConfigServe
const nextConfig = process.env.NEXT_OUTPUT_EXPORT
? nextConfigExport
: nextConfigServe

const plugins = [
withNx,
];
const plugins = [withNx]

module.exports = composePlugins(...plugins)(nextConfig);
module.exports = composePlugins(...plugins)(nextConfig)
1 change: 1 addition & 0 deletions go.work
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ use (
./hostd
./renterd
./walletd
./sdk
)
3 changes: 2 additions & 1 deletion libs/sdk/.eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
"@nx/dependency-checks": [
"error",
{
"ignoredFiles": ["libs/sdk/rollup.config.js"]
"ignoredFiles": ["libs/sdk/rollup.config.js"],
"ignoredDependencies": ["util"]
}
]
},
Expand Down
1 change: 1 addition & 0 deletions libs/sdk/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
*.wasm
18 changes: 17 additions & 1 deletion libs/sdk/project.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,26 @@
"sourceRoot": "libs/sdk/src",
"projectType": "library",
"tags": [],
"namedInputs": {
"go": ["{workspaceRoot}/sdk/**/*"],
"production": ["default", "go"]
},
"targets": {
"compile": {
"executor": "nx:run-commands",
"inputs": ["go"],
"cache": true,
"options": {
"commands": [
"tinygo build -o libs/sdk/src/wasm/resources/sdk.wasm -target wasm ./sdk"
]
}
},
"build": {
"executor": "@nrwl/rollup:rollup",
"executor": "@nx/rollup:rollup",
"outputs": ["{options.outputPath}"],
"dependsOn": ["compile", "^build"],
"inputs": ["production", "^production"],
"options": {
"outputPath": "dist/libs/sdk",
"tsConfig": "libs/sdk/tsconfig.lib.json",
Expand Down
9 changes: 8 additions & 1 deletion libs/sdk/rollup.config.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// eslint-disable-next-line @typescript-eslint/no-var-requires
const preserveDirectives = require('rollup-plugin-preserve-directives')
const { wasm } = require('@rollup/plugin-wasm')

// https://github.com/rollup/rollup/issues/4699#issuecomment-1465302665
function getRollupOptions(options) {
Expand All @@ -11,7 +12,13 @@ function getRollupOptions(options) {
format: 'esm',
sourcemap: true,
},
plugins: options.plugins.concat(preserveDirectives.default()),
plugins: options.plugins
.concat(
wasm({
targetEnv: 'auto-inline',
})
)
.concat(preserveDirectives.default()),
}
}

Expand Down
2 changes: 1 addition & 1 deletion libs/sdk/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
export * from './types'
export * from './transport'
export * from './wasm'
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
decodeHostSettings,
} from './encoding'
import { newEncoder, newDecoder } from './encoder'
import { HostPrices, HostSettings } from './types'
import { HostPrices, HostSettings } from '../types'
import { TextDecoder, TextEncoder } from 'node:util'

Object.defineProperties(globalThis, {
Expand Down
2 changes: 1 addition & 1 deletion libs/sdk/src/encoding.ts → libs/sdk/src/js/encoding.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import {
Encoder,
Decoder,
} from './encoder'
import { HostPrices, HostSettings, NetAddress } from './types'
import { HostPrices, HostSettings, NetAddress } from '../types'

export function encodeHostPrices(e: Encoder, hostPrices: HostPrices) {
encodeCurrency(e, hostPrices.contractPrice)
Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion libs/sdk/src/rpc.spec.ts → libs/sdk/src/js/rpc.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
RPCSettingsResponse,
RPCWriteSectorRequest,
RPCWriteSectorResponse,
} from './types'
} from '../types'
import { TextDecoder, TextEncoder } from 'node:util'
import {
decodeRpcRequestReadSector,
Expand Down
6 changes: 5 additions & 1 deletion libs/sdk/src/rpc.ts → libs/sdk/src/js/rpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,11 @@ import {
RPCSettingsResponse,
RPCWriteSectorRequest,
RPCWriteSectorResponse,
} from './types'
} from '../types'

// NOTE: This JavaScript RPC and encoding implementations is not currently used
// and may be incomplete or incorrect. It was written as a comparison to the WASM
// RPC and encoding implementations which are used and exported from the SDK.

// settings request

Expand Down
20 changes: 10 additions & 10 deletions libs/sdk/src/transport.ts → libs/sdk/src/js/transport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,7 @@ import {
RPCReadSector,
RPCWriteSector,
RPCSettings,
} from './types'

function base64ToArrayBuffer(base64: string) {
const binaryString = window.atob(base64)
const bytes = new Uint8Array(binaryString.length)
for (let i = 0; i < binaryString.length; i++) {
bytes[i] = binaryString.charCodeAt(i)
}
return bytes.buffer
}
} from '../types'

export class WebTransportClient {
private transport!: WebTransport
Expand Down Expand Up @@ -134,3 +125,12 @@ export class WebTransportClient {
)
}
}

function base64ToArrayBuffer(base64: string) {
const binaryString = window.atob(base64)
const bytes = new Uint8Array(binaryString.length)
for (let i = 0; i < binaryString.length; i++) {
bytes[i] = binaryString.charCodeAt(i)
}
return bytes.buffer
}
10 changes: 10 additions & 0 deletions libs/sdk/src/wasm/global.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
declare module '*.wasm' {
const func: (
imports: WebAssembly.Imports
) => Promise<WebAssembly.WebAssemblyInstantiatedSource>
export default func
}

interface Window {
Go: typeof Go
}
24 changes: 24 additions & 0 deletions libs/sdk/src/wasm/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import './utils/wasm_exec_tinygo'
import wasm from './resources/sdk.wasm'
import { SDK } from './types'

export function getSDK() {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
return (global as any).sdk as SDK
}

export async function initSDK(): Promise<{ sdk?: SDK; error?: string }> {
try {
const go = new window.Go()
const source = await wasm(go.importObject)
await go.run(source.instance)
return {
sdk: getSDK(),
}
} catch (e) {
console.log(e)
return {
error: (e as Error).message,
}
}
}
Empty file.
68 changes: 68 additions & 0 deletions libs/sdk/src/wasm/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import {
RPCReadSectorRequest,
RPCReadSectorResponse,
RPCSettingsRequest,
RPCSettingsResponse,
RPCWriteSectorRequest,
RPCWriteSectorResponse,
} from '../types'

export type SDK = {
rhp: {
// settings
encodeSettingsRequest: () => {
rpc?: Uint8Array
error?: string
}
decodeSettingsRequest: () => {
data?: RPCSettingsRequest
error?: string
}
encodeSettingsResponse: () => {
rpc?: Uint8Array
error?: string
}
decodeSettingsResponse: () => {
data?: RPCSettingsResponse
error?: string
}
// read sector
encodeReadSectorRequest: () => {
rpc?: Uint8Array
error?: string
}
decodeReadSectorRequest: () => {
data?: RPCReadSectorRequest
error?: string
}
encodeReadSectorResponse: () => {
rpc?: Uint8Array
error?: string
}
decodeReadSectorResponse: () => {
data?: RPCReadSectorResponse
error?: string
}
// read sector
encodeWriteSectorRequest: () => {
rpc?: Uint8Array
error?: string
}
decodeWriteSectorRequest: () => {
data?: RPCWriteSectorRequest
error?: string
}
encodeWriteSectorResponse: () => {
rpc?: Uint8Array
error?: string
}
decodeWriteSectorResponse: () => {
data?: RPCWriteSectorResponse
error?: string
}
}
generateAccountID: () => {
accountID?: string
error?: string
}
}
10 changes: 10 additions & 0 deletions libs/sdk/src/wasm/utils/wasm_exec.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
declare class Go {
constructor()
argv: string[]
env: { [envKey: string]: string }
exit: (code: number) => void
importObject: WebAssembly.Imports
exited: boolean
mem: DataView
run(instance: WebAssembly.Instance): Promise<void>
}
Loading

0 comments on commit 44a84f0

Please sign in to comment.