Skip to content

Commit

Permalink
refactor(sdk): sdk cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
alexfreska committed Feb 3, 2025
1 parent 53c8c33 commit f4f94d9
Show file tree
Hide file tree
Showing 6 changed files with 109 additions and 99 deletions.
5 changes: 5 additions & 0 deletions .changeset/cool-tomatoes-deliver.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@siafoundation/sdk': minor
---

Added v2TransactionInputSigHash and signHash methods to API.
5 changes: 5 additions & 0 deletions .changeset/tidy-lobsters-float.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@siafoundation/sdk': minor
---

Improved the accuracy of all return types.
184 changes: 89 additions & 95 deletions libs/sdk/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import {
Hash256,
PublicKey,
PrivateKey,
V2Transaction,
Result,
} from '@siafoundation/types'

type AccountToken = {
Expand Down Expand Up @@ -97,114 +99,106 @@ export type RPC = RPCSettings | RPCReadSector | RPCWriteSector

export type WasmApi = {
rhp: {
generateAccount: () => {
privateKey?: PrivateKey
account?: PublicKey
error?: string
}
generateAccount: () => Result<{
privateKey: PrivateKey
account: PublicKey
}>
// settings
encodeSettingsRequest: (data: RPCSettingsRequest) => {
rpc?: Uint8Array
error?: string
}
decodeSettingsRequest: (rpc: Uint8Array) => {
data?: Record<string, never>
error?: string
}
encodeSettingsResponse: (data: RPCSettingsResponse) => {
rpc?: Uint8Array
error?: string
}
decodeSettingsResponse: (rpc: Uint8Array) => {
data?: RPCSettingsResponse
error?: string
}
encodeSettingsRequest: (data: RPCSettingsRequest) => Result<{
rpc: Uint8Array
}>
decodeSettingsRequest: (rpc: Uint8Array) => Result<{
data: Record<string, never>
}>
encodeSettingsResponse: (data: RPCSettingsResponse) => Result<{
rpc: Uint8Array
}>
decodeSettingsResponse: (rpc: Uint8Array) => Result<{
data: RPCSettingsResponse
}>
// read sector
encodeReadSectorRequest: (data: RPCReadSectorRequest) => {
rpc?: Uint8Array
error?: string
}
decodeReadSectorRequest: (rpc: Uint8Array) => {
data?: RPCReadSectorRequest
error?: string
}
encodeReadSectorResponse: (data: RPCReadSectorResponse) => {
rpc?: Uint8Array
error?: string
}
decodeReadSectorResponse: (rpc: Uint8Array) => {
data?: RPCReadSectorResponse
error?: string
}
encodeReadSectorRequest: (data: RPCReadSectorRequest) => Result<{
rpc: Uint8Array
}>
decodeReadSectorRequest: (rpc: Uint8Array) => Result<{
data: RPCReadSectorRequest
}>
encodeReadSectorResponse: (data: RPCReadSectorResponse) => Result<{
rpc: Uint8Array
}>
decodeReadSectorResponse: (rpc: Uint8Array) => Result<{
data: RPCReadSectorResponse
}>
// read sector
encodeWriteSectorRequest: (data: RPCWriteSectorRequest) => {
rpc?: Uint8Array
error?: string
}
decodeWriteSectorRequest: (rpc: Uint8Array) => {
data?: RPCWriteSectorRequest
error?: string
}
encodeWriteSectorResponse: (data: RPCWriteSectorResponse) => {
rpc?: Uint8Array
error?: string
}
decodeWriteSectorResponse: (rpc: Uint8Array) => {
data?: RPCWriteSectorResponse
error?: string
}
encodeWriteSectorRequest: (data: RPCWriteSectorRequest) => Result<{
rpc: Uint8Array
}>
decodeWriteSectorRequest: (rpc: Uint8Array) => Result<{
data: RPCWriteSectorRequest
}>
encodeWriteSectorResponse: (data: RPCWriteSectorResponse) => Result<{
rpc: Uint8Array
}>
decodeWriteSectorResponse: (rpc: Uint8Array) => Result<{
data: RPCWriteSectorResponse
}>
}
wallet: {
generateSeedPhrase: () => {
phrase?: string
error?: string
}
generateKeyPair: () => {
privateKey?: string
publicKey?: string
error?: string
}
generateSeedPhrase: () => Result<{
phrase: string
}>
generateKeyPair: () => Result<{
privateKey: string
publicKey: string
}>
keyPairFromSeedPhrase: (
phrase: string,
index: number
) => {
privateKey?: string
publicKey?: string
error?: string
}
standardUnlockConditions: (publicKey: string) => {
unlockConditions?: UnlockConditions
error?: string
}
standardUnlockHash: (publicKey: string) => {
address?: string
error?: string
}
addressFromUnlockConditions: (unlockConditions: UnlockConditions) => {
address?: string
error?: string
}
addressFromSpendPolicy: (spendPolicy: Record<string, unknown>) => {
address?: string
error?: string
}
encodeTransaction: (txn: Transaction) => {
encodedTransaction?: string
error?: string
}
) => Result<{
privateKey: string
publicKey: string
}>
standardUnlockConditions: (publicKey: string) => Result<{
unlockConditions: UnlockConditions
}>
standardUnlockHash: (publicKey: string) => Result<{
address: string
}>
addressFromUnlockConditions: (
unlockConditions: UnlockConditions
) => Result<{
address: string
}>
addressFromSpendPolicy: (spendPolicy: Record<string, unknown>) => Result<{
address: string
}>
encodeTransaction: (txn: Transaction) => Result<{
encodedTransaction: string
}>
signTransactionV1: (
cs: ConsensusState,
cn: ConsensusNetwork,
txn: Transaction,
sigIndex: number,
privateKey: string
) => {
signature?: string
error?: string
}
transactionId: (txn: Transaction) => {
id?: string
error?: string
}
) => Result<{
signature: string
}>
v2TransactionInputSigHash: (
state: ConsensusState,
network: ConsensusNetwork,
txn: V2Transaction
) => Result<{
sigHash: string
}>
signHash: (
privateKey: string,
hash: string
) => Result<{
signature: string
}>
transactionId: (txn: Transaction) => Result<{
id: string
}>
}
}
6 changes: 6 additions & 0 deletions libs/types/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,9 @@ export type Nullish<T> = T | null | undefined
export type NoUndefined<T> = {
[K in keyof T]: Exclude<T[K], undefined>
}

export type Result<T> =
| T
| {
error: string
}
6 changes: 3 additions & 3 deletions sdk/wallet.go
Original file line number Diff line number Diff line change
Expand Up @@ -211,9 +211,9 @@ func transactionID(this js.Value, args []js.Value) result {
})
}

// v2TransactionInputSigHash returns the input sighash of a v2 transaction.
// v2TransactionInputSigHash returns the input sigHash of a v2 transaction.
func v2TransactionInputSigHash(_ js.Value, args []js.Value) result {
if err := checkArgs(args, js.TypeObject, js.TypeObject, js.TypeObject, js.TypeNumber, js.TypeString); err != nil {
if err := checkArgs(args, js.TypeObject, js.TypeObject, js.TypeObject); err != nil {
return resultErr(err)
}

Expand All @@ -234,7 +234,7 @@ func v2TransactionInputSigHash(_ js.Value, args []js.Value) result {
cs.Network = &cn

return result(map[string]any{
"sighash": cs.InputSigHash(txn).String(),
"sigHash": cs.InputSigHash(txn).String(),
})
}

Expand Down
2 changes: 1 addition & 1 deletion tsconfig.base.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
"@siafoundation/explored-js": ["libs/explored-js/src/index.ts"],
// "buildLibsFromSource": false does not work with the next executor
// this is a very annoying workaround
"@siafoundation/sdk": ["dist/libs/sdk/index.esm.js"],
"@siafoundation/sdk": ["dist/libs/sdk"],
"@siafoundation/sia-central-types": [
"libs/sia-central-types/src/index.ts"
],
Expand Down

0 comments on commit f4f94d9

Please sign in to comment.