@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 +1 -1
- package/src/auth.ts +3 -4
- package/src/index.ts +0 -1
- package/src/payment.ts +42 -19
- package/src/types.ts +1 -7
package/package.json
CHANGED
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
|
|
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
|
-
|
|
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
|
-
|
|
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
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
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
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.
|
|
61
|
-
|
|
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?:
|
|
33
|
+
signTransaction?: (transaction: Uint8Array) => Promise<Uint8Array>;
|
|
40
34
|
}
|
|
41
35
|
|
|
42
36
|
export interface SetuAuth {
|