Skip to content

Commit

Permalink
refactor: migrate wallet wasm into sdk
Browse files Browse the repository at this point in the history
  • Loading branch information
alexfreska committed Mar 20, 2024
1 parent fa3f89e commit 6d41967
Show file tree
Hide file tree
Showing 15 changed files with 949 additions and 69 deletions.
5 changes: 5 additions & 0 deletions .changeset/real-carrots-cry.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@siafoundation/sdk': minor
---

Wallet and transaction signing methods are now part of the sdk.
2 changes: 1 addition & 1 deletion apps/walletd/hooks/useSyncStatus.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { hoursInMilliseconds } from '@siafoundation/design-system'
import { useAppSettings } from '@siafoundation/react-core'
import { ConsensusState } from '@siafoundation/types'
import {
ConsensusState,
useConsensusTip,
useConsensusTipState,
useEstimatedNetworkBlockHeight,
Expand Down
3 changes: 2 additions & 1 deletion apps/walletd/lib/signSeed.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { ConsensusState, ConsensusNetwork } from '@siafoundation/react-walletd'
import {
SiacoinElement,
Transaction,
SiafundElement,
ConsensusState,
ConsensusNetwork,
} from '@siafoundation/types'
import { getWalletWasm } from './wasm'
import { AddressData } from '../contexts/addresses/types'
Expand Down
2 changes: 1 addition & 1 deletion apps/walletd/lib/testMocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,7 @@ export function getConsensusState() {
return {
index: {
height: 45962,
ID: 'bid:000000000cb8ef1dfeb66afa78bc0b3b2d1a7a1df948efba22f7fc1a5571e79f',
id: 'bid:000000000cb8ef1dfeb66afa78bc0b3b2d1a7a1df948efba22f7fc1a5571e79f',
},
prevTimestamps: [
'2023-11-28T11:34:49-05:00',
Expand Down
10 changes: 3 additions & 7 deletions libs/react-walletd/src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import {
useDeleteFunc,
} from '@siafoundation/react-core'
import {
ConsensusState,
ConsensusNetwork,
Currency,
BlockHeight,
ChainIndex,
Expand All @@ -20,13 +22,7 @@ import {
SiafundElement,
Transaction,
} from '@siafoundation/types'
import {
ConsensusState,
ConsensusNetwork,
PoolTransaction,
WalletEvent,
GatewayPeer,
} from './siaTypes'
import { PoolTransaction, WalletEvent, GatewayPeer } from './siaTypes'

// consensus

Expand Down
57 changes: 0 additions & 57 deletions libs/react-walletd/src/siaTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,63 +9,6 @@ import {
FileContractElement,
} from '@siafoundation/types'

export type ConsensusNetwork = {
name: 'mainnet' | 'zen'
initialCoinbase: Currency
minimumCoinbase: Currency
initialTarget: string
hardforkDevAddr: {
height: number
oldAddress: string
newAddress: string
}
hardforkTax: {
height: number
}
hardforkStorageProof: {
height: number
}
hardforkOak: {
height: number
fixHeight: number
genesisTimestamp: string
}
hardforkASIC: {
height: number
oakTime: number
oakTarget: string
}
hardforkFoundation: {
height: number
primaryAddress: string
failsafeAddress: string
}
hardforkV2: {
allowHeight: number
requireHeight: number
}
}

export type ConsensusState = {
index: ChainIndex
prevTimestamps: string[]
depth: string
childTarget: string
siafundPool: string
oakTime: number
oakTarget: string
foundationPrimaryAddress: string
foundationFailsafeAddress: string
totalWork: string
difficulty: string
oakWork: string
elements: {
numLeaves: number
trees: string[]
}
attestations: number
}

export type GatewayPeer = {
addr: string
inbound: boolean
Expand Down
13 changes: 11 additions & 2 deletions libs/sdk/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,15 @@

SDK for interacting directly with the Sia network from browsers and web clients.

## Running unit tests
## Installation

Run `nx test sdk` to execute the unit tests via [Jest](https://jestjs.io).
`npm install @siafoundation/sdk`

## Usage

```js
import { initSDK } from '@siafoundation/sdk'

const sdk = await initSDK()
const { phrase, error } = sdk.wallet.generateSeedPhrase()
```
3 changes: 3 additions & 0 deletions libs/sdk/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
"description": "SDK for interacting directly with the Sia network from browsers and web clients.",
"version": "0.0.2",
"license": "MIT",
"dependencies": {
"@siafoundation/types": "0.1.3"
},
"devDependencies": {
"undici": "5.28.3"
},
Expand Down
1 change: 1 addition & 0 deletions libs/sdk/src/sdk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export function getSDK() {
const wasm = (global as any).sia as WASM
return {
rhp: wasm.rhp,
wallet: wasm.wallet,
WebTransportClient,
}
}
60 changes: 60 additions & 0 deletions libs/sdk/src/types.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
import {
Transaction,
UnlockConditions,
ConsensusNetwork,
ConsensusState,
} from '@siafoundation/types'

type Currency = string
type Signature = string
type Address = string
Expand Down Expand Up @@ -144,4 +151,57 @@ export type WASM = {
error?: string
}
}
wallet: {
generateSeedPhrase: () => {
phrase?: string
error?: string
}
generateKeyPair: () => {
privateKey?: string
publicKey?: string
error?: 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: string) => {
address?: string
error?: string
}
encodeTransaction: (txn: Transaction) => {
encodedTransaction?: string
error?: string
}
signTransaction: (
cs: ConsensusState,
cn: ConsensusNetwork,
txn: Transaction,
sigIndex: number,
privateKey: string
) => {
signature?: string
error?: string
}
transactionId: (txn: Transaction) => {
id?: string
error?: string
}
}
}
Loading

0 comments on commit 6d41967

Please sign in to comment.