@ottocode/ai-sdk 0.1.5 → 0.1.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ottocode/ai-sdk",
3
- "version": "0.1.5",
3
+ "version": "0.1.6",
4
4
  "type": "module",
5
5
  "exports": {
6
6
  ".": "./src/index.ts",
package/src/auth.ts CHANGED
@@ -1,15 +1,14 @@
1
1
  import { Keypair } from '@solana/web3.js';
2
2
  import bs58 from 'bs58';
3
3
  import nacl from 'tweetnacl';
4
- import type { SetuAuth, LegacySigner } from './types.ts';
5
- import type { TransactionSigner } from '@solana/kit';
4
+ import type { SetuAuth } from './types.ts';
6
5
 
7
6
  export interface WalletContext {
8
7
  walletAddress: string;
9
8
  buildHeaders: () => Promise<Record<string, string>> | Record<string, string>;
10
9
  keypair?: Keypair;
11
10
  privateKeyBytes?: Uint8Array;
12
- transactionSigner?: TransactionSigner | LegacySigner;
11
+ signTransaction?: (transaction: Uint8Array) => Promise<Uint8Array>;
13
12
  }
14
13
 
15
14
  export function createWalletContext(auth: SetuAuth): WalletContext {
@@ -21,7 +20,7 @@ export function createWalletContext(auth: SetuAuth): WalletContext {
21
20
  } = auth.signer;
22
21
  return {
23
22
  walletAddress,
24
- transactionSigner: signTransaction,
23
+ signTransaction,
25
24
  buildHeaders: async () => {
26
25
  const nonce = Date.now().toString();
27
26
  const signature = await customSignNonce(nonce);
package/src/index.ts CHANGED
@@ -5,7 +5,6 @@ export type {
5
5
  SetuConfig,
6
6
  SetuAuth,
7
7
  ExternalSigner,
8
- LegacySigner,
9
8
  ProviderId,
10
9
  ProviderApiFormat,
11
10
  ProviderConfig,
package/src/payment.ts CHANGED
@@ -7,8 +7,15 @@ import type {
7
7
  ExactPaymentRequirement,
8
8
  PaymentPayload,
9
9
  PaymentCallbacks,
10
- LegacySigner,
11
10
  } from './types.ts';
11
+ import {
12
+ address,
13
+ getTransactionEncoder,
14
+ getTransactionDecoder,
15
+ type Transaction,
16
+ type TransactionWithLifetime,
17
+ type TransactionWithinSizeLimit,
18
+ } from '@solana/kit';
12
19
 
13
20
  function simplifyPaymentError(errMsg: string): string {
14
21
  const lower = errMsg.toLowerCase();
@@ -45,27 +52,43 @@ export function pickPaymentRequirement(
45
52
  return accepts.find((opt) => opt && opt.scheme === 'exact') ?? null;
46
53
  }
47
54
 
48
- function isLegacySigner(signer: unknown): signer is LegacySigner {
49
- return (
50
- typeof signer === 'object' &&
51
- signer !== null &&
52
- 'secretKey' in signer &&
53
- signer.secretKey instanceof Uint8Array &&
54
- 'publicKey' in signer &&
55
- typeof (signer as LegacySigner).publicKey?.toBase58 === 'function'
56
- );
55
+ function wrapCallbackAsSigner(
56
+ walletAddress: string,
57
+ callback: (transaction: Uint8Array) => Promise<Uint8Array>,
58
+ ) {
59
+ const encoder = getTransactionEncoder();
60
+ const decoder = getTransactionDecoder();
61
+ return {
62
+ address: address(walletAddress),
63
+ modifyAndSignTransactions: async (
64
+ transactions: readonly (
65
+ | Transaction
66
+ | (Transaction & TransactionWithLifetime)
67
+ )[],
68
+ ): Promise<
69
+ readonly (Transaction &
70
+ TransactionWithinSizeLimit &
71
+ TransactionWithLifetime)[]
72
+ > => {
73
+ const results = [];
74
+ for (const tx of transactions) {
75
+ const bytes = new Uint8Array(encoder.encode(tx));
76
+ const signedBytes = await callback(bytes);
77
+ const signed = decoder.decode(signedBytes);
78
+ results.push(
79
+ signed as Transaction &
80
+ TransactionWithinSizeLimit &
81
+ TransactionWithLifetime,
82
+ );
83
+ }
84
+ return results;
85
+ },
86
+ };
57
87
  }
58
88
 
59
89
  async function resolvePaymentSigner(wallet: WalletContext) {
60
- if (wallet.transactionSigner) {
61
- if (isLegacySigner(wallet.transactionSigner)) {
62
- const privateKeyBase58 = bs58.encode(wallet.transactionSigner.secretKey);
63
- return svm.createSignerFromBase58(privateKeyBase58);
64
- }
65
- return wallet.transactionSigner as Exclude<
66
- typeof wallet.transactionSigner,
67
- LegacySigner
68
- >;
90
+ if (wallet.signTransaction) {
91
+ return wrapCallbackAsSigner(wallet.walletAddress, wallet.signTransaction);
69
92
  }
70
93
  if (wallet.keypair) {
71
94
  const privateKeyBase58 = bs58.encode(wallet.keypair.secretKey);
package/src/types.ts CHANGED
@@ -1,5 +1,4 @@
1
1
  import type { LanguageModelV3Middleware } from '@ai-sdk/provider';
2
- import type { TransactionSigner } from '@solana/kit';
3
2
 
4
3
  export type ProviderId =
5
4
  | 'openai'
@@ -28,15 +27,10 @@ export interface ProviderConfig {
28
27
  modelPrefix?: string;
29
28
  }
30
29
 
31
- export interface LegacySigner {
32
- publicKey: { toBase58(): string };
33
- secretKey: Uint8Array;
34
- }
35
-
36
30
  export interface ExternalSigner {
37
31
  walletAddress: string;
38
32
  signNonce: (nonce: string) => Promise<string> | string;
39
- signTransaction?: TransactionSigner | LegacySigner;
33
+ signTransaction?: (transaction: Uint8Array) => Promise<Uint8Array>;
40
34
  }
41
35
 
42
36
  export interface SetuAuth {