Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions packages/sdk/src/sdk/createSdk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,11 @@ import { productionConfig } from './config/production'
import {
addAppInfoMiddleware,
addRequestSignatureMiddleware,
addSolanaWalletSignatureMiddleware,
addTokenRefreshMiddleware
} from './middleware'
import { OAuth } from './oauth'
import { SolanaWallet } from './solanaWallet'
import { TokenStoreLocalStorage } from './oauth/TokenStoreLocalStorage'
import { Logger, Storage, StorageNodeSelector } from './services'
import { SdkConfigSchema, type SdkConfig } from './types'
Expand Down Expand Up @@ -72,6 +74,8 @@ export const createSdk = (config: SdkConfig) => {
openUrl: services?.openUrl
})

const solanaWallet = new SolanaWallet()

if (apiSecret || services?.audiusWalletClient) {
middleware.push(
addRequestSignatureMiddleware({
Expand Down Expand Up @@ -99,6 +103,8 @@ export const createSdk = (config: SdkConfig) => {
)
}

middleware.push(addSolanaWalletSignatureMiddleware({ solanaWallet }))

// Auto-refresh middleware — intercepts 401s and retries with a fresh token.
if (apiKey && oauth) {
middleware.push(
Expand Down Expand Up @@ -137,6 +143,7 @@ export const createSdk = (config: SdkConfig) => {

return {
oauth,
solanaWallet,
tokenStore,
tracks: new TracksApi(apiConfig),
users: usersApi,
Expand Down
8 changes: 7 additions & 1 deletion packages/sdk/src/sdk/createSdkWithServices.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import { productionConfig } from './config/production'
import {
addAppInfoMiddleware,
addRequestSignatureMiddleware,
addSolanaWalletSignatureMiddleware,
addTokenRefreshMiddleware
} from './middleware'
import { OAuth } from './oauth'
Expand Down Expand Up @@ -70,6 +71,7 @@ import {
StorageNodeSelector,
getDefaultStorageNodeSelectorConfig
} from './services/StorageNodeSelector'
import { SolanaWallet } from './solanaWallet'
import { SdkConfig, SdkConfigSchema, ServicesContainer } from './types'
import fetch from './utils/fetch'

Expand Down Expand Up @@ -351,6 +353,8 @@ const initializeApis = ({
: productionConfig.network.apiEndpoint
const basePath = `${apiEndpoint}/v1`

const solanaWallet = new SolanaWallet()

const middleware = [
addAppInfoMiddleware({
apiKey,
Expand All @@ -362,7 +366,8 @@ const initializeApis = ({
services,
apiKey,
apiSecret
})
}),
addSolanaWalletSignatureMiddleware({ solanaWallet })
]

// Token store for PKCE flow — provides dynamic accessToken to Configuration
Expand Down Expand Up @@ -453,6 +458,7 @@ const initializeApis = ({

return {
oauth,
solanaWallet,
tokenStore,
tracks,
users,
Expand Down
1 change: 1 addition & 0 deletions packages/sdk/src/sdk/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ export * from './services'
export { productionConfig } from './config/production'
export { developmentConfig } from './config/development'
export * from './oauth/types'
export * from './solanaWallet'
export { ParseRequestError } from './utils/parseParams'
export * from './utils/rendezvous'
export * as Errors from './utils/errors'
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import type {
FetchParams,
Middleware,
RequestContext
} from '../api/generated/default'
import type { SolanaWallet } from '../solanaWallet'

/**
* Injects X-Solana-* headers when a wallet credential is set.
* Works alongside OAuth — both can be present so the API merges balances.
*
* @example
* ```ts
* import { sdk as audiusSdk } from '@audius/sdk'
*
* const sdk = audiusSdk({ appName: 'MyApp' })
* await sdk.solanaWallet.auth(window.solana)
* sdk.tracks.getTrack({ id: '123' })
* ```
*/
export const addSolanaWalletSignatureMiddleware = ({
solanaWallet
}: {
solanaWallet: SolanaWallet
}): Middleware => ({
pre: async (context: RequestContext): Promise<FetchParams> => {
const credential = solanaWallet.getCredential()
if (!credential) return context

const headers = context.init.headers as Record<string, string>
return {
...context,
init: {
...context.init,
headers: {
...headers,
'X-Solana-Wallet': credential.publicKey,
'X-Solana-Message': credential.message,
'X-Solana-Signature': credential.signature
}
}
}
}
})
1 change: 1 addition & 0 deletions packages/sdk/src/sdk/middleware/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export { addAppInfoMiddleware } from './addAppInfoMiddleware'
export { addRequestSignatureMiddleware } from './addRequestSignatureMiddleware'
export { addSolanaWalletSignatureMiddleware } from './addSolanaWalletSignatureMiddleware'
export { addTokenRefreshMiddleware } from './addTokenRefreshMiddleware'
61 changes: 61 additions & 0 deletions packages/sdk/src/sdk/solanaWallet/SolanaWallet.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import bs58 from 'bs58'

export type SolanaWalletCredential = {
publicKey: string
message: string
signature: string
}

/**
* Minimal interface for a Solana wallet provider (Phantom, Solflare, etc.).
* Apps pass their connected wallet to `SolanaWallet.auth()`.
*/
export type SolanaWalletProvider = {
connect(): Promise<{ publicKey: { toString(): string } }>
signMessage(
message: Uint8Array,
encoding: string
): Promise<{ signature: Uint8Array }>
}

export function createSolanaWalletSignatureMessage() {
const timestamp = Date.now()
const message = `audius:solana-wallet:${timestamp}`
const messageBytes = new TextEncoder().encode(message)
return { message, messageBytes, timestamp }
}

export class SolanaWallet {
private credential: SolanaWalletCredential | null = null

async auth(provider: SolanaWalletProvider) {
const { publicKey } = await provider.connect()
const { message, messageBytes } = createSolanaWalletSignatureMessage()
const { signature: sigBytes } = await provider.signMessage(
messageBytes,
'utf8'
)
this.credential = {
publicKey: publicKey.toString(),
message,
signature: bs58.encode(sigBytes)
}
return { publicKey: this.credential.publicKey }
}

setCredential(credential: SolanaWalletCredential) {
this.credential = credential
}

clearCredential() {
this.credential = null
}

getCredential(): SolanaWalletCredential | null {
return this.credential
}

isAuthenticated(): boolean {
return this.credential !== null
}
}
8 changes: 8 additions & 0 deletions packages/sdk/src/sdk/solanaWallet/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export {
SolanaWallet,
createSolanaWalletSignatureMessage
} from './SolanaWallet'
export type {
SolanaWalletCredential,
SolanaWalletProvider
} from './SolanaWallet'
Loading
Loading