diff --git a/.github/actions/setup/action.yml b/.github/actions/setup/action.yml index ab978b107..daa956919 100644 --- a/.github/actions/setup/action.yml +++ b/.github/actions/setup/action.yml @@ -24,7 +24,7 @@ runs: registry-url: https://registry.npmjs.org - uses: actions/setup-go@v4 with: - go-version: '1.20' + go-version: '1.21.7' - uses: acifani/setup-tinygo@v2 with: tinygo-version: '0.30.0' diff --git a/go.work b/go.work index 390c0833d..b2201135d 100644 --- a/go.work +++ b/go.work @@ -1,9 +1,9 @@ -go 1.20 +go 1.21.7 use ( ./ ./hostd ./renterd - ./walletd ./sdk + ./walletd ) diff --git a/go.work.sum b/go.work.sum index 87555cbed..0b7cdcd2e 100644 --- a/go.work.sum +++ b/go.work.sum @@ -1 +1,7 @@ +github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE= +go.etcd.io/bbolt v1.3.8/go.mod h1:N9Mkw9X8x5fupy0IKsmuqVtoGDyxsaDlbk4Rd05IAQw= +go.etcd.io/gofail v0.1.0/go.mod h1:VZBCXYGZhHAinaBiiqYvuDynvahNsAyLFwB3kEHKz1M= go.sia.tech/mux v1.2.0/go.mod h1:Yyo6wZelOYTyvrHmJZ6aQfRoer3o4xyKQ4NmQLJrBSo= +golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y= +golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= +golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= diff --git a/libs/sdk/src/rhp.spec.ts b/libs/sdk/src/rhp.spec.ts new file mode 100644 index 000000000..95fbc1101 --- /dev/null +++ b/libs/sdk/src/rhp.spec.ts @@ -0,0 +1,263 @@ +/* eslint-disable @typescript-eslint/no-non-null-assertion */ +import { + HostPrices, + HostSettings, + RPCReadSectorRequest, + RPCReadSectorResponse, + RPCSettingsResponse, + RPCWriteSectorRequest, + RPCWriteSectorResponse, +} from './types' +import { initSDKTest } from './initTest' + +describe('rhp', () => { + describe('generateAccount', () => { + it('works', async () => { + const sdk = await initSDKTest() + const { privateKey, account, error } = sdk.rhp.generateAccount() + expect(error).toBeUndefined() + expect(privateKey).toBeDefined() + expect(privateKey?.length).toBeGreaterThan(40) + expect(account).toBeDefined() + expect(account?.length).toBeGreaterThan(40) + }) + }) + describe('settings', () => { + describe('request', () => { + it('valid', async () => { + const sdk = await initSDKTest() + const encode = sdk.rhp.encodeSettingsRequest() + expect(encode.rpc).toBeDefined() + expect(encode.error).not.toBeDefined() + const decode = sdk.rhp.decodeSettingsRequest(encode.rpc!) + expect(decode.data).toEqual({}) + expect(decode.error).toBeUndefined() + }) + }) + describe('response', () => { + it('valid', async () => { + const sdk = await initSDKTest() + const json = getSampleRPCSettingsResponse() + const encode = sdk.rhp.encodeSettingsResponse(json) + expect(encode.rpc).toBeDefined() + expect(encode.rpc?.length).toEqual(323) + expect(encode.error).toBeUndefined() + const decode = sdk.rhp.decodeSettingsResponse(encode.rpc!) + expect(decode.data).toEqual(json) + expect(decode.error).toBeUndefined() + }) + it('encode error', async () => { + const sdk = await initSDKTest() + const json = { + settings: { + walletAddress: 'invalid', + }, + } as RPCSettingsResponse + const encode = sdk.rhp.encodeSettingsResponse(json) + expect(encode.rpc).toBeUndefined() + expect(encode.error).toEqual( + "decoding addr: failed: encoding/hex: invalid byte: U+0069 'i'" + ) + }) + it('decode error', async () => { + const sdk = await initSDKTest() + const json = getSampleRPCSettingsResponse() + const encode = sdk.rhp.encodeSettingsResponse(json) + // manipulate the valid rpc to make it invalid + encode.rpc!.set([1, 1], 30) + const decode = sdk.rhp.decodeSettingsResponse(encode.rpc!) + expect(decode.data).not.toEqual(json) + expect(decode.error).toEqual( + 'encoded object contains invalid length prefix (65806 elems > 11227 bytes left in stream)' + ) + }) + }) + }) + describe('read', () => { + describe('request', () => { + it('valid', async () => { + const sdk = await initSDKTest() + const json: RPCReadSectorRequest = { + token: { + account: + 'acct:1b6793e900df020dc9a43c6df5f5d10dc5793956d44831ca5bbfec659021b75e', + validUntil: '2022-12-31T00:00:00Z', + signature: + 'sig:457256d6a1603bef7fa957a70b5ba96a9def2fea8b4c1483060d7ba5cf8a072cfddf242a1ef033dd7d669c711e846c59cb916f804a03d72d279ffef7e6583404', + }, + root: 'h:457256d6a1603bef7fa957a70b5ba96a9def2fea8b4c1483060d7ba5cf8a072c', + prices: getSampleHostPrices(), + offset: 0, + length: 4, + } + const encode = sdk.rhp.encodeReadSectorRequest(json) + expect(encode.rpc?.length).toEqual(312) + expect(encode.error).toBeUndefined() + const decode = sdk.rhp.decodeReadSectorRequest(encode.rpc!) + expect(decode.data).toEqual(json) + expect(decode.error).toBeUndefined() + }) + it('encode error', async () => { + const sdk = await initSDKTest() + const json: RPCReadSectorRequest = { + token: { + account: 'invalid', + validUntil: '2022-12-31T00:00:00Z', + signature: + 'sig:457256d6a1603bef7fa957a70b5ba96a9def2fea8b4c1483060d7ba5cf8a072cfddf242a1ef033dd7d669c711e846c59cb916f804a03d72d279ffef7e6583404', + }, + root: 'h:457256d6a1603bef7fa957a70b5ba96a9def2fea8b4c1483060d7ba5cf8a072c', + prices: getSampleHostPrices(), + offset: 0, + length: 4, + } + const encode = sdk.rhp.encodeReadSectorRequest(json) + expect(encode.rpc).toBeUndefined() + expect(encode.error).toEqual( + "decoding acct: failed: encoding/hex: invalid byte: U+0069 'i'" + ) + }) + }) + describe('response', () => { + it('valid', async () => { + const sdk = await initSDKTest() + const json: RPCReadSectorResponse = { + proof: [ + 'h:457256d6a1603bef7fa957a70b5ba96a9def2fea8b4c1483060d7ba5cf8a072c', + ], + sector: 'AQID', + } + const encode = sdk.rhp.encodeReadSectorResponse(json) + expect(encode.rpc?.toString()).toEqual( + [ + 0, 1, 0, 0, 0, 0, 0, 0, 0, 69, 114, 86, 214, 161, 96, 59, 239, 127, + 169, 87, 167, 11, 91, 169, 106, 157, 239, 47, 234, 139, 76, 20, 131, + 6, 13, 123, 165, 207, 138, 7, 44, 3, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, + ].toString() + ) + expect(encode.error).toBeUndefined() + const decode = sdk.rhp.decodeReadSectorResponse(encode.rpc!) + expect(decode.data).toEqual(json) + expect(decode.error).toBeUndefined() + }) + it('encode error', async () => { + const sdk = await initSDKTest() + const json: RPCReadSectorResponse = { + proof: ['invalid'], + sector: 'AQID', + } + const encode = sdk.rhp.encodeReadSectorResponse(json) + expect(encode.rpc).toBeUndefined() + expect(encode.error).toEqual('decoding h: failed: unexpected EOF') + }) + }) + }) + describe('write', () => { + describe('request', () => { + it('valid', async () => { + const sdk = await initSDKTest() + const json: RPCWriteSectorRequest = { + token: { + account: + 'acct:1b6793e900df020dc9a43c6df5f5d10dc5793956d44831ca5bbfec659021b75e', + validUntil: '2022-12-31T00:00:00Z', + signature: + 'sig:457256d6a1603bef7fa957a70b5ba96a9def2fea8b4c1483060d7ba5cf8a072cfddf242a1ef033dd7d669c711e846c59cb916f804a03d72d279ffef7e6583404', + }, + sector: 'AQID', + prices: getSampleHostPrices(), + } + const encode = sdk.rhp.encodeWriteSectorRequest(json) + expect(encode.rpc?.length).toEqual(275) + expect(encode.error).toBeUndefined() + const decode = sdk.rhp.decodeWriteSectorRequest(encode.rpc!) + expect(decode.data).toEqual(json) + expect(decode.error).toBeUndefined() + }) + it('encode error', async () => { + const sdk = await initSDKTest() + const json = { + token: { + account: 'invalid', + validUntil: '2022-12-31T00:00:00Z', + signature: + 'sig:457256d6a1603bef7fa957a70b5ba96a9def2fea8b4c1483060d7ba5cf8a072cfddf242a1ef033dd7d669c711e846c59cb916f804a03d72d279ffef7e6583404', + }, + sector: 'AQID', + prices: getSampleHostPrices(), + } as RPCWriteSectorRequest + const encode = sdk.rhp.encodeWriteSectorRequest(json) + expect(encode.rpc).toBeUndefined() + expect(encode.error).toEqual( + "decoding acct: failed: encoding/hex: invalid byte: U+0069 'i'" + ) + }) + }) + describe('response', () => { + it('valid', async () => { + const sdk = await initSDKTest() + const json: RPCWriteSectorResponse = { + root: 'h:457256d6a1603bef7fa957a70b5ba96a9def2fea8b4c1483060d7ba5cf8a072c', + } + const encode = sdk.rhp.encodeWriteSectorResponse(json) + expect(encode.rpc?.toString()).toEqual( + [ + 0, 69, 114, 86, 214, 161, 96, 59, 239, 127, 169, 87, 167, 11, 91, + 169, 106, 157, 239, 47, 234, 139, 76, 20, 131, 6, 13, 123, 165, 207, + 138, 7, 44, + ].toString() + ) + expect(encode.error).toBeUndefined() + const decode = sdk.rhp.decodeWriteSectorResponse(encode.rpc!) + expect(decode.data).toEqual(json) + expect(decode.error).toBeUndefined() + }) + it('encode error', async () => { + const sdk = await initSDKTest() + const json = { + root: 'invalid', + } as RPCWriteSectorResponse + const encode = sdk.rhp.encodeWriteSectorResponse(json) + expect(encode.rpc).toBeUndefined() + expect(encode.error).toEqual('decoding h: failed: unexpected EOF') + }) + }) + }) +}) + +function getSampleHostPrices(): HostPrices { + return { + contractPrice: '1000000000', + collateral: '2000000000', + storagePrice: '3000000000', + ingressPrice: '4000000000', + egressPrice: '5000000000', + tipHeight: 450_000, + validUntil: '2022-12-31T00:00:00Z', + signature: + 'sig:457256d6a1603bef7fa957a70b5ba96a9def2fea8b4c1483060d7ba5cf8a072cfddf242a1ef033dd7d669c711e846c59cb916f804a03d72d279ffef7e6583404', + } +} + +function getSampleRPCSettingsResponse(): RPCSettingsResponse { + const prices = getSampleHostPrices() + const settings: HostSettings = { + version: [1, 2, 3], + netAddresses: [ + { protocol: 'protocol1', address: 'address1longer' }, + { protocol: 'protocol2longer', address: 'address2' }, + ], + // 32 bytes + walletAddress: + 'addr:eec8160897cf7058332040675d120c008dc32d96925e9b32a812b646e31676d7d52c118cad2c', + acceptingContracts: true, + maxCollateral: '1000000000', + maxDuration: 100, + remainingStorage: 100, + totalStorage: 100, + prices, + } + return { + settings, + } +} diff --git a/libs/sdk/src/sdk.ts b/libs/sdk/src/sdk.ts index 0cd01daac..92c384e4c 100644 --- a/libs/sdk/src/sdk.ts +++ b/libs/sdk/src/sdk.ts @@ -5,7 +5,7 @@ export function getSDK() { // eslint-disable-next-line @typescript-eslint/no-explicit-any const wasm = (global as any).sia as WASM return { - wasm, + rhp: wasm.rhp, WebTransportClient, } } diff --git a/libs/sdk/src/types.ts b/libs/sdk/src/types.ts index 87e64ef04..ca34508f1 100644 --- a/libs/sdk/src/types.ts +++ b/libs/sdk/src/types.ts @@ -88,10 +88,8 @@ export type RPC = RPCSettings | RPCReadSector | RPCWriteSector export type WASM = { rhp: { generateAccount: () => { - data?: { - privateKey: PrivateKey - account?: PublicKey - } + privateKey?: PrivateKey + account?: PublicKey error?: string } // settings diff --git a/libs/sdk/src/wasm.spec.ts b/libs/sdk/src/wasm.spec.ts deleted file mode 100644 index 00ea2f500..000000000 --- a/libs/sdk/src/wasm.spec.ts +++ /dev/null @@ -1,290 +0,0 @@ -import { - HostPrices, - HostSettings, - RPCReadSectorRequest, - RPCReadSectorResponse, - RPCSettingsResponse, - RPCWriteSectorRequest, - RPCWriteSectorResponse, -} from './types' -import { initSDKTest } from './initTest' - -describe('wasm', () => { - describe('rhp', () => { - describe('generateAccount', () => { - it('works', async () => { - const sdk = await initSDKTest() - const { data, error } = sdk.wasm.rhp.generateAccount() - expect(error).toBeUndefined() - expect(data?.privateKey).toBeDefined() - expect(data?.privateKey?.length).toBeGreaterThan(40) - expect(data?.account).toBeDefined() - expect(data?.account?.length).toBeGreaterThan(40) - }) - }) - describe('settings', () => { - describe('request', () => { - it('valid', async () => { - const sdk = await initSDKTest() - const encode = sdk.wasm.rhp.encodeSettingsRequest() - expect(encode.rpc).toBeDefined() - expect(encode.error).not.toBeDefined() - if (!encode.rpc) { - throw new Error('rpc is undefined') - } - const decode = sdk.wasm.rhp.decodeSettingsRequest(encode.rpc) - expect(decode.data).toEqual({}) - expect(decode.error).toBeUndefined() - }) - }) - describe('response', () => { - it('valid', async () => { - const sdk = await initSDKTest() - const json = getSampleRPCSettingsResponse() - const encode = sdk.wasm.rhp.encodeSettingsResponse(json) - expect(encode.rpc).toBeDefined() - expect(encode.rpc?.length).toEqual(323) - expect(encode.error).toBeUndefined() - if (!encode.rpc) { - throw new Error('rpc is undefined') - } - const decode = sdk.wasm.rhp.decodeSettingsResponse(encode.rpc) - expect(decode.data).toEqual(json) - expect(decode.error).toBeUndefined() - }) - it('encode error', async () => { - const sdk = await initSDKTest() - const json = { - settings: { - walletAddress: 'invalid', - }, - } as RPCSettingsResponse - const encode = sdk.wasm.rhp.encodeSettingsResponse(json) - expect(encode.rpc).toBeUndefined() - expect(encode.error).toEqual( - "decoding addr: failed: encoding/hex: invalid byte: U+0069 'i'" - ) - }) - it('decode error', async () => { - const sdk = await initSDKTest() - const json = getSampleRPCSettingsResponse() - const encode = sdk.wasm.rhp.encodeSettingsResponse(json) - if (!encode.rpc) { - throw new Error('rpc is undefined') - } - // manipulate the valid rpc to make it invalid - encode.rpc.set([1, 1], 30) - const decode = sdk.wasm.rhp.decodeSettingsResponse(encode.rpc) - expect(decode.data).not.toEqual(json) - expect(decode.error).toEqual( - 'encoded object contains invalid length prefix (65806 elems > 11227 bytes left in stream)' - ) - }) - }) - }) - describe('read', () => { - describe('request', () => { - it('valid', async () => { - const sdk = await initSDKTest() - const json: RPCReadSectorRequest = { - token: { - account: - 'acct:1b6793e900df020dc9a43c6df5f5d10dc5793956d44831ca5bbfec659021b75e', - validUntil: '2022-12-31T00:00:00Z', - signature: - 'sig:457256d6a1603bef7fa957a70b5ba96a9def2fea8b4c1483060d7ba5cf8a072cfddf242a1ef033dd7d669c711e846c59cb916f804a03d72d279ffef7e6583404', - }, - root: 'h:457256d6a1603bef7fa957a70b5ba96a9def2fea8b4c1483060d7ba5cf8a072c', - prices: getSampleHostPrices(), - offset: 0, - length: 4, - } - const encode = sdk.wasm.rhp.encodeReadSectorRequest(json) - expect(encode.rpc?.length).toEqual(312) - expect(encode.error).toBeUndefined() - if (!encode.rpc) { - throw new Error('rpc is undefined') - } - const decode = sdk.wasm.rhp.decodeReadSectorRequest(encode.rpc) - expect(decode.data).toEqual(json) - expect(decode.error).toBeUndefined() - }) - it('encode error', async () => { - const sdk = await initSDKTest() - const json: RPCReadSectorRequest = { - token: { - account: 'invalid', - validUntil: '2022-12-31T00:00:00Z', - signature: - 'sig:457256d6a1603bef7fa957a70b5ba96a9def2fea8b4c1483060d7ba5cf8a072cfddf242a1ef033dd7d669c711e846c59cb916f804a03d72d279ffef7e6583404', - }, - root: 'h:457256d6a1603bef7fa957a70b5ba96a9def2fea8b4c1483060d7ba5cf8a072c', - prices: getSampleHostPrices(), - offset: 0, - length: 4, - } - const encode = sdk.wasm.rhp.encodeReadSectorRequest(json) - expect(encode.rpc).toBeUndefined() - expect(encode.error).toEqual( - "decoding acct: failed: encoding/hex: invalid byte: U+0069 'i'" - ) - }) - }) - describe('response', () => { - it('valid', async () => { - const sdk = await initSDKTest() - const json: RPCReadSectorResponse = { - proof: [ - 'h:457256d6a1603bef7fa957a70b5ba96a9def2fea8b4c1483060d7ba5cf8a072c', - ], - sector: 'AQID', - } - const encode = sdk.wasm.rhp.encodeReadSectorResponse(json) - expect(encode.rpc?.toString()).toEqual( - [ - 0, 1, 0, 0, 0, 0, 0, 0, 0, 69, 114, 86, 214, 161, 96, 59, 239, - 127, 169, 87, 167, 11, 91, 169, 106, 157, 239, 47, 234, 139, 76, - 20, 131, 6, 13, 123, 165, 207, 138, 7, 44, 3, 0, 0, 0, 0, 0, 0, 0, - 1, 2, 3, - ].toString() - ) - expect(encode.error).toBeUndefined() - if (!encode.rpc) { - throw new Error('rpc is undefined') - } - const decode = sdk.wasm.rhp.decodeReadSectorResponse(encode.rpc) - expect(decode.data).toEqual(json) - expect(decode.error).toBeUndefined() - }) - it('encode error', async () => { - const sdk = await initSDKTest() - const json: RPCReadSectorResponse = { - proof: ['invalid'], - sector: 'AQID', - } - const encode = sdk.wasm.rhp.encodeReadSectorResponse(json) - expect(encode.rpc).toBeUndefined() - expect(encode.error).toEqual( - 'decoding h: failed: unexpected EOF' - ) - }) - }) - }) - describe('write', () => { - describe('request', () => { - it('valid', async () => { - const sdk = await initSDKTest() - const json: RPCWriteSectorRequest = { - token: { - account: - 'acct:1b6793e900df020dc9a43c6df5f5d10dc5793956d44831ca5bbfec659021b75e', - validUntil: '2022-12-31T00:00:00Z', - signature: - 'sig:457256d6a1603bef7fa957a70b5ba96a9def2fea8b4c1483060d7ba5cf8a072cfddf242a1ef033dd7d669c711e846c59cb916f804a03d72d279ffef7e6583404', - }, - sector: 'AQID', - prices: getSampleHostPrices(), - } - const encode = sdk.wasm.rhp.encodeWriteSectorRequest(json) - expect(encode.rpc?.length).toEqual(275) - expect(encode.error).toBeUndefined() - if (!encode.rpc) { - throw new Error('rpc is undefined') - } - const decode = sdk.wasm.rhp.decodeWriteSectorRequest(encode.rpc) - expect(decode.data).toEqual(json) - expect(decode.error).toBeUndefined() - }) - it('encode error', async () => { - const sdk = await initSDKTest() - const json = { - token: { - account: 'invalid', - validUntil: '2022-12-31T00:00:00Z', - signature: - 'sig:457256d6a1603bef7fa957a70b5ba96a9def2fea8b4c1483060d7ba5cf8a072cfddf242a1ef033dd7d669c711e846c59cb916f804a03d72d279ffef7e6583404', - }, - sector: 'AQID', - prices: getSampleHostPrices(), - } as RPCWriteSectorRequest - const encode = sdk.wasm.rhp.encodeWriteSectorRequest(json) - expect(encode.rpc).toBeUndefined() - expect(encode.error).toEqual( - "decoding acct: failed: encoding/hex: invalid byte: U+0069 'i'" - ) - }) - }) - describe('response', () => { - it('valid', async () => { - const sdk = await initSDKTest() - const json: RPCWriteSectorResponse = { - root: 'h:457256d6a1603bef7fa957a70b5ba96a9def2fea8b4c1483060d7ba5cf8a072c', - } - const encode = sdk.wasm.rhp.encodeWriteSectorResponse(json) - expect(encode.rpc?.toString()).toEqual( - [ - 0, 69, 114, 86, 214, 161, 96, 59, 239, 127, 169, 87, 167, 11, 91, - 169, 106, 157, 239, 47, 234, 139, 76, 20, 131, 6, 13, 123, 165, - 207, 138, 7, 44, - ].toString() - ) - expect(encode.error).toBeUndefined() - if (!encode.rpc) { - throw new Error('rpc is undefined') - } - const decode = sdk.wasm.rhp.decodeWriteSectorResponse(encode.rpc) - expect(decode.data).toEqual(json) - expect(decode.error).toBeUndefined() - }) - it('encode error', async () => { - const sdk = await initSDKTest() - const json = { - root: 'invalid', - } as RPCWriteSectorResponse - const encode = sdk.wasm.rhp.encodeWriteSectorResponse(json) - expect(encode.rpc).toBeUndefined() - expect(encode.error).toEqual( - 'decoding h: failed: unexpected EOF' - ) - }) - }) - }) - }) -}) - -function getSampleHostPrices(): HostPrices { - return { - contractPrice: '1000000000', - collateral: '2000000000', - storagePrice: '3000000000', - ingressPrice: '4000000000', - egressPrice: '5000000000', - tipHeight: 450_000, - validUntil: '2022-12-31T00:00:00Z', - signature: - 'sig:457256d6a1603bef7fa957a70b5ba96a9def2fea8b4c1483060d7ba5cf8a072cfddf242a1ef033dd7d669c711e846c59cb916f804a03d72d279ffef7e6583404', - } -} - -function getSampleRPCSettingsResponse(): RPCSettingsResponse { - const prices = getSampleHostPrices() - const settings: HostSettings = { - version: [1, 2, 3], - netAddresses: [ - { protocol: 'protocol1', address: 'address1longer' }, - { protocol: 'protocol2longer', address: 'address2' }, - ], - // 32 bytes - walletAddress: - 'addr:eec8160897cf7058332040675d120c008dc32d96925e9b32a812b646e31676d7d52c118cad2c', - acceptingContracts: true, - maxCollateral: '1000000000', - maxDuration: 100, - remainingStorage: 100, - totalStorage: 100, - prices, - } - return { - settings, - } -} diff --git a/sdk/api/api.go b/sdk/api/api.go new file mode 100644 index 000000000..df4151bd3 --- /dev/null +++ b/sdk/api/api.go @@ -0,0 +1,23 @@ +package api + +import ( + "syscall/js" +) + +type Result = map[string]any + +func ResultErr(err error) Result { + return map[string]any{"error": err.Error()} +} + +func ResultErrStr(err string) Result { + return map[string]any{"error": err} +} + +func ResultRPC(rpc js.Value) Result { + return map[string]any{"rpc": rpc} +} + +func ResultData(data any) Result { + return map[string]any{"data": data} +} diff --git a/sdk/go.mod b/sdk/go.mod index dee208d71..075de5993 100644 --- a/sdk/go.mod +++ b/sdk/go.mod @@ -1,14 +1,16 @@ module go.sia.tech/web/sdk -go 1.20 +go 1.21.7 require ( go.sia.tech/core v0.2.2-0.20240229154321-d97c1d5b2172 - go.sia.tech/web v0.0.0-20230628194305-c6e1696bad89 + go.sia.tech/coreutils v0.0.3 ) require ( github.com/aead/chacha20 v0.0.0-20180709150244-8b13a72661da // indirect + go.uber.org/multierr v1.10.0 // indirect + go.uber.org/zap v1.26.0 // indirect golang.org/x/crypto v0.0.0-20220507011949-2cf3adece122 // indirect golang.org/x/sys v0.5.0 // indirect lukechampine.com/frand v1.4.2 // indirect diff --git a/sdk/go.sum b/sdk/go.sum index 7470405a9..8feb98ce7 100644 --- a/sdk/go.sum +++ b/sdk/go.sum @@ -1,21 +1,27 @@ github.com/aead/chacha20 v0.0.0-20180709150244-8b13a72661da h1:KjTM2ks9d14ZYCvmHS9iAKVt9AyzRSqNU1qabPih5BY= github.com/aead/chacha20 v0.0.0-20180709150244-8b13a72661da/go.mod h1:eHEWzANqSiWQsof+nXEI9bUVUyV6F53Fp89EuCh2EAA= -go.sia.tech/core v0.1.12-0.20230807160906-ad76cac3058f h1:ZPWqj1RphySPSdVvhW09VYI/nKc+TiqJlUnx9FcI0lY= -go.sia.tech/core v0.1.12-0.20230807160906-ad76cac3058f/go.mod h1:D17UWSn99SEfQnEaR9G9n6Kz9+BwqMoUgZ6Cl424LsQ= -go.sia.tech/core v0.2.2-0.20240202170315-3e6d0eca7490 h1:pfmR0dva8GQ1Oxb5VpF7JWfDuWgmfbgZKK8oAKWT24g= -go.sia.tech/core v0.2.2-0.20240202170315-3e6d0eca7490/go.mod h1:3EoY+rR78w1/uGoXXVqcYdwSjSJKuEMI5bL7WROA27Q= -go.sia.tech/core v0.2.2-0.20240209000234-ef2053eea93e h1:Bps0MIQGHkWpyG4aPnG7G2v2bvUajI5Ly3zXPkQ1RFo= -go.sia.tech/core v0.2.2-0.20240209000234-ef2053eea93e/go.mod h1:3EoY+rR78w1/uGoXXVqcYdwSjSJKuEMI5bL7WROA27Q= -go.sia.tech/core v0.2.2-0.20240229150213-044552edecf7 h1:YDurKmoU+oVzlZgAcaDSTrdr8mpebMW9oJieXrhFh6A= -go.sia.tech/core v0.2.2-0.20240229150213-044552edecf7/go.mod h1:3EoY+rR78w1/uGoXXVqcYdwSjSJKuEMI5bL7WROA27Q= +github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk= +github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4= go.sia.tech/core v0.2.2-0.20240229154321-d97c1d5b2172 h1:uET7VyK5mz02bsicyNeoEut/RarvmQXAvXgUqZ1YJoE= go.sia.tech/core v0.2.2-0.20240229154321-d97c1d5b2172/go.mod h1:3EoY+rR78w1/uGoXXVqcYdwSjSJKuEMI5bL7WROA27Q= -go.sia.tech/web v0.0.0-20230628194305-c6e1696bad89 h1:wB/JRFeTEs6gviB6k7QARY7Goh54ufkADsdBdn0ZhRo= -go.sia.tech/web v0.0.0-20230628194305-c6e1696bad89/go.mod h1:RKODSdOmR3VtObPAcGwQqm4qnqntDVFylbvOBbWYYBU= +go.sia.tech/coreutils v0.0.3 h1:ZxuzovRpQMvfy/pCOV4om1cPF6sE15GyJyK36kIrF1Y= +go.sia.tech/coreutils v0.0.3/go.mod h1:UBFc77wXiE//eyilO5HLOncIEj7F69j0Nv2OkFujtP0= +go.uber.org/goleak v1.2.0 h1:xqgm/S+aQvhWFTtR0XK3Jvg7z8kGV8P4X14IzwN3Eqk= +go.uber.org/goleak v1.2.0/go.mod h1:XJYK+MuIchqpmGmUSAzotztawfKvYLUIgg7guXrwVUo= +go.uber.org/multierr v1.10.0 h1:S0h4aNzvfcFsC3dRF1jLoaov7oRaKqRGC/pUEJ2yvPQ= +go.uber.org/multierr v1.10.0/go.mod h1:20+QtiLqy0Nd6FdQB9TLXag12DsQkrbs3htMFfDN80Y= +go.uber.org/zap v1.26.0 h1:sI7k6L95XOKS281NhVKOFCUNIvv9e0w4BF8N3u+tCRo= +go.uber.org/zap v1.26.0/go.mod h1:dtElttAiwGvoJ/vj4IwHBS/gXsEu/pZ50mUIRWuG0so= golang.org/x/crypto v0.0.0-20220507011949-2cf3adece122 h1:NvGWuYG8dkDHFSKksI1P9faiVJ9rayE6l0+ouWVIDs8= golang.org/x/crypto v0.0.0-20220507011949-2cf3adece122/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4= golang.org/x/sys v0.0.0-20190626221950-04f50cda93cb/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.5.0 h1:MUK/U/4lj1t1oPg0HfuXDN/Z1wv31ZJ/YcPiGccS4DU= golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= +gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= lukechampine.com/frand v1.4.2 h1:RzFIpOvkMXuPMBb9maa4ND4wjBn71E1Jpf8BzJHMaVw= lukechampine.com/frand v1.4.2/go.mod h1:4S/TM2ZgrKejMcKMbeLjISpJMO+/eZ1zu3vYX9dtj3s= diff --git a/sdk/main.go b/sdk/main.go index 02f5a54a1..ed9754d4c 100644 --- a/sdk/main.go +++ b/sdk/main.go @@ -8,7 +8,7 @@ import ( func main() { js.Global().Set("sia", map[string]any{ - "rhp": map[string]js.Func{ + "rhp": map[string]any{ "generateAccount": jsFunc(rhp.GenerateAccount), // settings "encodeSettingsRequest": jsFunc(rhp.EncodeSettingsRequest), diff --git a/sdk/rhp/encode.go b/sdk/rhp/encode.go index c7ac4b1b6..3eae656fa 100644 --- a/sdk/rhp/encode.go +++ b/sdk/rhp/encode.go @@ -5,63 +5,64 @@ import ( "syscall/js" "go.sia.tech/core/rhp/v4" + "go.sia.tech/web/sdk/api" "go.sia.tech/web/sdk/marshal" ) -func encodeRPCRequest(data js.Value, req rhp.Request) result { +func encodeRPCRequest(data js.Value, req rhp.Request) api.Result { if data.Type() != js.TypeUndefined { if err := marshal.UnmarshalStruct(data, &req); err != nil { - return errResult(err) + return api.ResultErr(err) } } buf := bytes.NewBuffer(nil) if err := rhp.WriteRequest(buf, req); err != nil { - return errResult(err) + return api.ResultErr(err) } - return rpcResult(marshal.MarshalUint8Array(buf.Bytes())) + return api.ResultRPC(marshal.MarshalUint8Array(buf.Bytes())) } -func decodeRPCRequest(rpcJsData js.Value, res rhp.Request) result { +func decodeRPCRequest(rpcJsData js.Value, res rhp.Request) api.Result { rpcData, err := marshal.UnmarshalUint8Array(rpcJsData) if err != nil { - return errResult(err) + return api.ResultErr(err) } err = rhp.ReadRequest(bytes.NewReader(rpcData), res) if err != nil { - return errResult(err) + return api.ResultErr(err) } d, err := marshal.MarshalStruct(res) if err != nil { - return errResult(err) + return api.ResultErr(err) } - return dataResult(d) + return api.ResultData(d) } -func encodeRPCResponse(data js.Value, req rhp.Object) result { +func encodeRPCResponse(data js.Value, req rhp.Object) api.Result { if data.Type() != js.TypeUndefined { if err := marshal.UnmarshalStruct(data, &req); err != nil { - return errResult(err) + return api.ResultErr(err) } } buf := bytes.NewBuffer(nil) if err := rhp.WriteResponse(buf, req); err != nil { - return errResult(err) + return api.ResultErr(err) } - return rpcResult(marshal.MarshalUint8Array(buf.Bytes())) + return api.ResultRPC(marshal.MarshalUint8Array(buf.Bytes())) } -func decodeRPCResponse(rpcJsData js.Value, res rhp.Object) result { +func decodeRPCResponse(rpcJsData js.Value, res rhp.Object) api.Result { rpcData, err := marshal.UnmarshalUint8Array(rpcJsData) if err != nil { - return errResult(err) + return api.ResultErr(err) } err = rhp.ReadResponse(bytes.NewReader(rpcData), res) if err != nil { - return errResult(err) + return api.ResultErr(err) } d, err := marshal.MarshalStruct(res) if err != nil { - return errResult(err) + return api.ResultErr(err) } - return dataResult(d) + return api.ResultData(d) } diff --git a/sdk/rhp/result.go b/sdk/rhp/result.go deleted file mode 100644 index 76b8a7e03..000000000 --- a/sdk/rhp/result.go +++ /dev/null @@ -1,19 +0,0 @@ -package rhp - -import ( - "syscall/js" -) - -type result = map[string]any - -func errResult(err error) result { - return map[string]any{"error": err.Error()} -} - -func rpcResult(rpc js.Value) result { - return map[string]any{"rpc": rpc} -} - -func dataResult(data any) result { - return map[string]any{"data": data} -} diff --git a/sdk/rhp/rhp.go b/sdk/rhp/rhp.go index c37b8e0c1..d7b23e9a7 100644 --- a/sdk/rhp/rhp.go +++ b/sdk/rhp/rhp.go @@ -5,12 +5,13 @@ import ( "syscall/js" "go.sia.tech/core/rhp/v4" + "go.sia.tech/web/sdk/api" "go.sia.tech/web/sdk/utils" ) -func GenerateAccount(this js.Value, args []js.Value) result { +func GenerateAccount(this js.Value, args []js.Value) api.Result { if err := utils.CheckArgs(args); err != nil { - return errResult(err) + return api.ResultErr(err) } pk, a := rhp.GenerateAccount() @@ -18,7 +19,7 @@ func GenerateAccount(this js.Value, args []js.Value) result { privateKey := hex.EncodeToString(pk) account := hex.EncodeToString(a[:]) - return dataResult(map[string]any{ + return api.Result(map[string]any{ "privateKey": privateKey, "account": account, }) @@ -26,33 +27,33 @@ func GenerateAccount(this js.Value, args []js.Value) result { // settings -func EncodeSettingsRequest(this js.Value, args []js.Value) result { +func EncodeSettingsRequest(this js.Value, args []js.Value) api.Result { if err := utils.CheckArgs(args); err != nil { - return errResult(err) + return api.ResultErr(err) } var r rhp.RPCSettingsRequest return encodeRPCRequest(js.Undefined(), &r) } -func DecodeSettingsRequest(this js.Value, args []js.Value) result { +func DecodeSettingsRequest(this js.Value, args []js.Value) api.Result { if err := utils.CheckArgs(args, js.TypeObject); err != nil { - return errResult(err) + return api.ResultErr(err) } var r rhp.RPCSettingsRequest return decodeRPCRequest(args[0], &r) } -func EncodeSettingsResponse(this js.Value, args []js.Value) result { +func EncodeSettingsResponse(this js.Value, args []js.Value) api.Result { if err := utils.CheckArgs(args, js.TypeObject); err != nil { - return errResult(err) + return api.ResultErr(err) } var r rhp.RPCSettingsResponse return encodeRPCResponse(args[0], &r) } -func DecodeSettingsResponse(this js.Value, args []js.Value) result { +func DecodeSettingsResponse(this js.Value, args []js.Value) api.Result { if err := utils.CheckArgs(args, js.TypeObject); err != nil { - return errResult(err) + return api.ResultErr(err) } var r rhp.RPCSettingsResponse return decodeRPCResponse(args[0], &r) @@ -60,34 +61,34 @@ func DecodeSettingsResponse(this js.Value, args []js.Value) result { // read sector -func EncodeReadSectorRequest(this js.Value, args []js.Value) result { +func EncodeReadSectorRequest(this js.Value, args []js.Value) api.Result { if err := utils.CheckArgs(args, js.TypeObject); err != nil { - return errResult(err) + return api.ResultErr(err) } var r rhp.RPCReadSectorRequest return encodeRPCRequest(args[0], &r) } -func DecodeReadSectorRequest(this js.Value, args []js.Value) result { +func DecodeReadSectorRequest(this js.Value, args []js.Value) api.Result { if err := utils.CheckArgs(args, js.TypeObject); err != nil { - return errResult(err) + return api.ResultErr(err) } var r rhp.RPCReadSectorRequest return decodeRPCRequest(args[0], &r) } -func EncodeReadSectorResponse(this js.Value, args []js.Value) result { +func EncodeReadSectorResponse(this js.Value, args []js.Value) api.Result { if err := utils.CheckArgs(args, js.TypeObject); err != nil { - return errResult(err) + return api.ResultErr(err) } var r rhp.RPCReadSectorResponse return encodeRPCResponse(args[0], &r) } -func DecodeReadSectorResponse(this js.Value, args []js.Value) result { +func DecodeReadSectorResponse(this js.Value, args []js.Value) api.Result { if err := utils.CheckArgs(args, js.TypeObject); err != nil { - return errResult(err) + return api.ResultErr(err) } var r rhp.RPCReadSectorResponse return decodeRPCResponse(args[0], &r) @@ -95,34 +96,34 @@ func DecodeReadSectorResponse(this js.Value, args []js.Value) result { // write sector -func EncodeWriteSectorRequest(this js.Value, args []js.Value) result { +func EncodeWriteSectorRequest(this js.Value, args []js.Value) api.Result { if err := utils.CheckArgs(args, js.TypeObject); err != nil { - return errResult(err) + return api.ResultErr(err) } var r rhp.RPCWriteSectorRequest return encodeRPCRequest(args[0], &r) } -func DecodeWriteSectorRequest(this js.Value, args []js.Value) result { +func DecodeWriteSectorRequest(this js.Value, args []js.Value) api.Result { if err := utils.CheckArgs(args, js.TypeObject); err != nil { - return errResult(err) + return api.ResultErr(err) } var r rhp.RPCWriteSectorRequest return decodeRPCRequest(args[0], &r) } -func EncodeWriteSectorResponse(this js.Value, args []js.Value) result { +func EncodeWriteSectorResponse(this js.Value, args []js.Value) api.Result { if err := utils.CheckArgs(args, js.TypeObject); err != nil { - return errResult(err) + return api.ResultErr(err) } var r rhp.RPCWriteSectorResponse return encodeRPCResponse(args[0], &r) } -func DecodeWriteSectorResponse(this js.Value, args []js.Value) result { +func DecodeWriteSectorResponse(this js.Value, args []js.Value) api.Result { if err := utils.CheckArgs(args, js.TypeObject); err != nil { - return errResult(err) + return api.ResultErr(err) } var r rhp.RPCWriteSectorResponse return decodeRPCResponse(args[0], &r)