Skip to content

Commit 6c0ee43

Browse files
committed
refactor: sdk rhp
1 parent 8e57f7e commit 6c0ee43

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+1507
-1846
lines changed

.changeset/tiny-cougars-refuse.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@siafoundation/sdk': minor
3+
---
4+
5+
Updated SDK to latest core changes, updated structure.

.env.example

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
11
GITHUB_TOKEN=secret_token
22
NOTION_TOKEN=secret_token
33
ASSETS=/User/bob/web/assets
4+
5+
# Make Go use UTC for time formatting
6+
TZ=UTC

.github/actions/setup/action.yml

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ runs:
2424
registry-url: https://registry.npmjs.org
2525
- uses: actions/setup-go@v4
2626
with:
27-
go-version: '1.20'
27+
go-version: '1.21.7'
2828
- uses: acifani/setup-tinygo@v2
2929
with:
3030
tinygo-version: '0.30.0'
@@ -41,8 +41,13 @@ runs:
4141
# If source files changed but packages didn't, rebuild from a prior cache.
4242
restore-keys: |
4343
${{ runner.os }}-${{ inputs.node_version }}-${{ hashFiles('**/package-lock.json') }}-
44-
- name: Install
44+
- name: Install JavaScript dependencies
4545
# could do this since its a ci, but it force rebuilds node_modules
4646
# run: npm ci
4747
run: npm install
4848
shell: bash
49+
- name: Install Go dependencies
50+
run: |
51+
go mod tidy
52+
go mod download
53+
shell: bash

.github/workflows/pr.yml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,16 @@ jobs:
2525
- name: Commit lint
2626
shell: bash
2727
run: npx commitlint --from ${{ github.event.pull_request.base.sha }} --to ${{ github.event.pull_request.head.sha }}
28-
- name: Lint
28+
- name: Lint TypeScript
2929
shell: bash
3030
run: npx nx affected --target=lint --parallel=5
3131
- name: Lint Go
3232
uses: golangci/golangci-lint-action@v3
3333
with:
3434
skip-cache: true
35+
- name: Compile
36+
shell: bash
37+
run: npx nx affected --target=compile --parallel=5
3538
- name: Test
3639
shell: bash
3740
run: npx nx affected --target=test --parallel=5

.github/workflows/release-main.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@ jobs:
3232
- name: Lint
3333
shell: bash
3434
run: npx nx run-many --target=lint --all --parallel=5
35+
- name: Compile
36+
shell: bash
37+
run: npx nx run-many --target=compile --all --parallel=5
3538
- name: Test
3639
shell: bash
3740
run: npx nx run-many --target=test --all --parallel=5

go.work

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
go 1.20
1+
go 1.21.7
22

33
use (
44
./
55
./hostd
66
./renterd
7-
./walletd
87
./sdk
8+
./walletd
99
)

go.work.sum

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,7 @@
1+
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
2+
go.etcd.io/bbolt v1.3.8/go.mod h1:N9Mkw9X8x5fupy0IKsmuqVtoGDyxsaDlbk4Rd05IAQw=
3+
go.etcd.io/gofail v0.1.0/go.mod h1:VZBCXYGZhHAinaBiiqYvuDynvahNsAyLFwB3kEHKz1M=
14
go.sia.tech/mux v1.2.0/go.mod h1:Yyo6wZelOYTyvrHmJZ6aQfRoer3o4xyKQ4NmQLJrBSo=
5+
golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
6+
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
7+
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=

libs/sdk/.eslintrc.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"extends": ["plugin:@nx/react", "../../.eslintrc.json"],
3-
"ignorePatterns": ["!**/*"],
3+
"ignorePatterns": ["!**/*", "src/utils/wasm_exec.js"],
44
"rules": {
55
"@nx/dependency-checks": [
66
"error",

libs/sdk/package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
"description": "SDK for interacting directly with the Sia network from browsers and web clients.",
44
"version": "0.0.2",
55
"license": "MIT",
6-
"dependencies": {},
76
"devDependencies": {
87
"undici": "5.28.3"
98
},

libs/sdk/project.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@
1515
"cache": true,
1616
"options": {
1717
"commands": [
18-
"tinygo build -o libs/sdk/src/wasm/resources/sdk.wasm -target wasm ./sdk"
18+
"cp $(go env GOROOT)/misc/wasm/wasm_exec.js libs/sdk/src/utils/wasm_exec.js",
19+
"GOOS=js GOARCH=wasm go build -o libs/sdk/src/resources/sdk.wasm ./sdk"
1920
]
2021
}
2122
},
@@ -50,6 +51,7 @@
5051
"test": {
5152
"executor": "@nx/jest:jest",
5253
"outputs": ["{workspaceRoot}/coverage/libs/sdk"],
54+
"dependsOn": ["compile"],
5355
"options": {
5456
"jestConfig": "libs/sdk/jest.config.ts"
5557
}
File renamed without changes.

libs/sdk/src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1+
export * from './init'
12
export * from './types'
2-
export * from './wasm'

libs/sdk/src/init.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { getSDK } from './sdk'
2+
import { initWASM } from './wasm'
3+
4+
export async function initSDK() {
5+
await initWASM()
6+
return getSDK()
7+
}

libs/sdk/src/initTest.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { getSDK } from './sdk'
2+
import { initWASMTest } from './wasmTest'
3+
4+
export async function initSDKTest() {
5+
await initWASMTest()
6+
return getSDK()
7+
}

libs/sdk/src/js/encoder.ts renamed to libs/sdk/src/legacy/encoder.ts

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -90,20 +90,24 @@ export function decodeString(d: Decoder): string {
9090
return s
9191
}
9292

93-
export function encodeCurrency(e: Encoder, c: bigint) {
93+
export function encodeCurrency(e: Encoder, c: string) {
9494
// currency is 128 bits, little endian
95-
e.dataView.setBigUint64(e.offset, c & BigInt('0xFFFFFFFFFFFFFFFF'), true)
95+
e.dataView.setBigUint64(
96+
e.offset,
97+
BigInt(c) & BigInt('0xFFFFFFFFFFFFFFFF'),
98+
true
99+
)
96100
e.offset += 8
97-
e.dataView.setBigUint64(e.offset, c >> BigInt(64), true)
101+
e.dataView.setBigUint64(e.offset, BigInt(c) >> BigInt(64), true)
98102
e.offset += 8
99103
}
100104

101-
export function decodeCurrency(d: Decoder): bigint {
105+
export function decodeCurrency(d: Decoder): string {
102106
const lo = d.dataView.getBigUint64(d.offset, true)
103107
d.offset += 8
104108
const hi = d.dataView.getBigUint64(d.offset, true)
105109
d.offset += 8
106-
return (hi << BigInt(64)) | lo
110+
return String((hi << BigInt(64)) | lo)
107111
}
108112

109113
export function encodeAddress(e: Encoder, a: string) {

libs/sdk/src/js/encoding.spec.ts renamed to libs/sdk/src/legacy/encoding.spec.ts

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,16 @@ import {
55
decodeHostSettings,
66
} from './encoding'
77
import { newEncoder, newDecoder } from './encoder'
8-
import { HostPrices, HostSettings } from '../types'
8+
import { HostPrices, HostSettings } from './types'
99

1010
describe('encoding', () => {
1111
it('encodeHostPrices', () => {
1212
const hostPrices: HostPrices = {
13-
contractPrice: BigInt(1000000000),
14-
collateral: BigInt(2000000000),
15-
storagePrice: BigInt(3000000000),
16-
ingressPrice: BigInt(4000000000),
17-
egressPrice: BigInt(5000000000),
13+
contractPrice: '1000000000',
14+
collateral: '2000000000',
15+
storagePrice: '3000000000',
16+
ingressPrice: '4000000000',
17+
egressPrice: '5000000000',
1818
tipHeight: 450_000,
1919
validUntil: '2022-12-31T00:00:00.000Z',
2020
signature:
@@ -30,26 +30,26 @@ describe('encoding', () => {
3030

3131
it('encodeHostSettings', () => {
3232
const prices: HostPrices = {
33-
contractPrice: BigInt(1000000000),
34-
collateral: BigInt(2000000000),
35-
storagePrice: BigInt(3000000000),
36-
ingressPrice: BigInt(4000000000),
37-
egressPrice: BigInt(5000000000),
33+
contractPrice: '1000000000',
34+
collateral: '2000000000',
35+
storagePrice: '3000000000',
36+
ingressPrice: '4000000000',
37+
egressPrice: '5000000000',
3838
tipHeight: 450_000,
3939
validUntil: '2022-12-31T00:00:00.000Z',
4040
signature:
4141
'abcd567890123456789012345678901234567890123456789012345678901234',
4242
}
4343
const hostSettings: HostSettings = {
44-
version: '123',
44+
version: new Uint8Array([1, 2, 3]),
4545
netAddresses: [
4646
{ protocol: 'protocol1', address: 'address1longer' },
4747
{ protocol: 'protocol2longer', address: 'address2' },
4848
],
4949
// 32 bytes
5050
walletAddress: '12345678901234567890123456789012',
5151
acceptingContracts: true,
52-
maxCollateral: BigInt(1000000000),
52+
maxCollateral: '1000000000',
5353
maxDuration: 100,
5454
remainingStorage: 100,
5555
totalStorage: 100,

libs/sdk/src/js/encoding.ts renamed to libs/sdk/src/legacy/encoding.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,11 @@ import {
33
decodeCurrency,
44
encodeAddress,
55
encodeBoolean,
6-
encodeBytes,
76
encodeString,
87
encodeUint64,
98
encodeLengthPrefix,
109
decodeAddress,
1110
decodeBoolean,
12-
decodeBytes,
1311
decodeString,
1412
decodeUint64,
1513
decodeLengthPrefix,
@@ -19,8 +17,10 @@ import {
1917
decodeTime,
2018
Encoder,
2119
Decoder,
20+
decodeUint8Array,
21+
encodeUint8Array,
2222
} from './encoder'
23-
import { HostPrices, HostSettings, NetAddress } from '../types'
23+
import { HostPrices, HostSettings, NetAddress } from './types'
2424

2525
export function encodeHostPrices(e: Encoder, hostPrices: HostPrices) {
2626
encodeCurrency(e, hostPrices.contractPrice)
@@ -69,7 +69,7 @@ export function decodeNetAddress(d: Decoder): NetAddress {
6969
}
7070

7171
export function encodeHostSettings(e: Encoder, hostSettings: HostSettings) {
72-
encodeBytes(e, hostSettings.version)
72+
encodeUint8Array(e, hostSettings.version)
7373
encodeLengthPrefix(e, hostSettings.netAddresses.length)
7474
for (let i = 0; i < hostSettings.netAddresses.length; i++) {
7575
encodeNetAddress(e, hostSettings.netAddresses[i])
@@ -84,7 +84,7 @@ export function encodeHostSettings(e: Encoder, hostSettings: HostSettings) {
8484
}
8585

8686
export function decodeHostSettings(d: Decoder): HostSettings {
87-
const version = decodeBytes(d, 3)
87+
const version = decodeUint8Array(d, 3)
8888
const netAddresses = []
8989
const length = decodeLengthPrefix(d)
9090
for (let i = 0; i < length; i++) {
File renamed without changes.

libs/sdk/src/js/rpc.spec.ts renamed to libs/sdk/src/legacy/rpc.spec.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import {
99
RPCSettingsResponse,
1010
RPCWriteSectorRequest,
1111
RPCWriteSectorResponse,
12-
} from '../types'
12+
} from './types'
1313
import {
1414
decodeRpcRequestReadSector,
1515
decodeRpcRequestSettings,
@@ -26,26 +26,26 @@ import {
2626
} from './rpc'
2727

2828
const prices: HostPrices = {
29-
contractPrice: BigInt(1000000000),
30-
collateral: BigInt(2000000000),
31-
storagePrice: BigInt(3000000000),
32-
ingressPrice: BigInt(4000000000),
33-
egressPrice: BigInt(5000000000),
29+
contractPrice: '1000000000',
30+
collateral: '2000000000',
31+
storagePrice: '3000000000',
32+
ingressPrice: '4000000000',
33+
egressPrice: '5000000000',
3434
tipHeight: 450_000,
3535
validUntil: '2022-12-31T00:00:00.000Z',
3636
signature: 'abcd567890123456789012345678901234567890123456789012345678901234',
3737
}
3838

3939
const hostSettings: HostSettings = {
40-
version: '123',
40+
version: new Uint8Array([1, 2, 3]),
4141
netAddresses: [
4242
{ protocol: 'protocol1', address: 'address1longer' },
4343
{ protocol: 'protocol2longer', address: 'address2' },
4444
],
4545
// 32 bytes
4646
walletAddress: '12345678901234567890123456789012',
4747
acceptingContracts: true,
48-
maxCollateral: BigInt(1000000000),
48+
maxCollateral: '1000000000',
4949
maxDuration: 100,
5050
remainingStorage: 100,
5151
totalStorage: 100,

libs/sdk/src/js/rpc.ts renamed to libs/sdk/src/legacy/rpc.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ import {
2727
RPCSettingsResponse,
2828
RPCWriteSectorRequest,
2929
RPCWriteSectorResponse,
30-
} from '../types'
30+
} from './types'
3131

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

libs/sdk/src/js/transport.ts renamed to libs/sdk/src/legacy/transport.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import {
1717
RPCReadSector,
1818
RPCWriteSector,
1919
RPCSettings,
20-
} from '../types'
20+
} from './types'
2121

2222
export class WebTransportClient {
2323
private transport!: WebTransport

0 commit comments

Comments
 (0)