1
- < < < << << HEAD
2
1
import type {
3
2
SolanaReadRequest ,
4
3
SolanaTransaction ,
5
4
SolanaWalletClient ,
6
5
} from "@goat-sdk/core" ;
7
6
import {
8
7
type Connection ,
8
+ PublicKey ,
9
9
TransactionMessage ,
10
10
VersionedTransaction ,
11
- PublicKey ,
12
11
} from "@solana/web3.js" ;
13
- === === =
14
- import type { SolanaReadRequest , SolanaTransaction , SolanaWalletClient } from "@goat-sdk/core" ;
15
- import { type Connection , PublicKey , TransactionMessage , VersionedTransaction } from "@solana/web3.js" ;
16
- >>> >>> > origin / main
17
12
import bs58 from "bs58" ;
18
13
import { createCrossmintAPI } from "./api" ;
19
14
20
15
type CommonParameters = {
21
- chain : "solana" ;
22
- connection : Connection ;
23
- env ?: "staging" | "production" ;
16
+ chain : "solana" ;
17
+ connection : Connection ;
18
+ env ?: "staging" | "production" ;
24
19
} ;
25
20
26
21
type EmailLocatorParameters = CommonParameters & {
27
- email : string ;
22
+ email : string ;
28
23
} ;
29
24
30
25
type PhoneLocatorParameters = CommonParameters & {
31
- phone : string ;
26
+ phone : string ;
32
27
} ;
33
28
34
29
type UserIdLocatorParameters = CommonParameters & {
35
- userId : number ;
30
+ userId : number ;
36
31
} ;
37
32
38
33
type AddressLocatorParameters = CommonParameters & {
39
- address : string ;
34
+ address : string ;
40
35
} ;
41
36
42
37
type CustodialOptions =
43
- | EmailLocatorParameters
44
- | PhoneLocatorParameters
45
- | UserIdLocatorParameters
46
- | AddressLocatorParameters ;
38
+ | EmailLocatorParameters
39
+ | PhoneLocatorParameters
40
+ | UserIdLocatorParameters
41
+ | AddressLocatorParameters ;
47
42
48
43
function getLocator ( params : CustodialOptions ) : string | number {
49
- if ( "address" in params ) return params . address ;
50
- if ( "email" in params ) return `email:${ params . email } ` ;
51
- if ( "phone" in params ) return `phone:${ params . phone } ` ;
52
- return `userId:${ params . userId } ` ;
44
+ if ( "address" in params ) return params . address ;
45
+ if ( "email" in params ) return `email:${ params . email } ` ;
46
+ if ( "phone" in params ) return `phone:${ params . phone } ` ;
47
+ return `userId:${ params . userId } ` ;
53
48
}
54
49
55
50
export function custodialFactory ( apiKey : string ) {
56
- return async function custodial ( params : CustodialOptions ) : Promise < SolanaWalletClient > {
57
- const { connection, env = "staging" } = params ;
51
+ return async function custodial (
52
+ params : CustodialOptions ,
53
+ ) : Promise < SolanaWalletClient > {
54
+ const { connection, env = "staging" } = params ;
58
55
59
- < < < << << HEAD
60
56
const locator = `${ getLocator ( params ) } ` ;
61
57
const client = createCrossmintAPI ( apiKey , env ) ;
62
58
const { address } = await client . getWallet ( locator ) ;
@@ -77,43 +73,27 @@ export function custodialFactory(apiKey: string) {
77
73
locator ,
78
74
message ,
79
75
) ;
80
- = === ===
81
- const locator = `${ getLocator ( params ) } :solana-custodial-wallet` ;
82
- const client = createCrossmintAPI ( apiKey , env ) ;
83
- const { address } = await client . getWallet ( locator ) ;
84
-
85
- return {
86
- getAddress ( ) {
87
- return address ;
88
- } ,
89
- getChain ( ) {
90
- return {
91
- type : "solana" ,
92
- } ;
93
- } ,
94
- async signMessage ( message : string ) {
95
- try {
96
- const { id } = await client . signMessageForCustodialWallet ( locator , message ) ;
97
- > >>> >>> origin / main
98
-
99
- while ( true ) {
100
- const latestSignature = await client . checkSignatureStatus ( id , address ) ;
101
-
102
- if ( latestSignature . status === "success" ) {
103
- if ( ! latestSignature . outputSignature ) {
104
- throw new Error ( "Signature is undefined" ) ;
105
- }
106
-
107
- return {
108
- signature : latestSignature . outputSignature ,
109
- } ;
110
- }
111
-
112
- if ( latestSignature . status === "failed" ) {
113
- throw new Error ( "Signature failed" ) ;
114
- }
115
-
116
- < < < << << HEAD
76
+
77
+ while ( true ) {
78
+ const latestSignature = await client . checkSignatureStatus (
79
+ id ,
80
+ address ,
81
+ ) ;
82
+
83
+ if ( latestSignature . status === "success" ) {
84
+ if ( ! latestSignature . outputSignature ) {
85
+ throw new Error ( "Signature is undefined" ) ;
86
+ }
87
+
88
+ return {
89
+ signature : latestSignature . outputSignature ,
90
+ } ;
91
+ }
92
+
93
+ if ( latestSignature . status === "failed" ) {
94
+ throw new Error ( "Signature failed" ) ;
95
+ }
96
+
117
97
await new Promise ( ( resolve ) => setTimeout ( resolve , 3000 ) ) ; // Wait 3 seconds
118
98
}
119
99
} catch ( error ) {
@@ -135,73 +115,60 @@ export function custodialFactory(apiKey: string) {
135
115
const encodedVersionedTransaction = bs58 . encode (
136
116
serializedVersionedTransaction ,
137
117
) ;
138
- === = ===
139
- await new Promise ( ( resolve ) => setTimeout ( resolve , 3000 ) ) ; // Wait 3 seconds
140
- }
141
- } catch ( error ) {
142
- throw new Error ( `Failed to sign message: ${ error } ` ) ;
143
- }
144
- } ,
145
- async sendTransaction ( { instructions } : SolanaTransaction ) {
146
- const latestBlockhash = await connection . getLatestBlockhash ( "confirmed" ) ;
147
- const message = new TransactionMessage ( {
148
- // Placeholder payer key since Crossmint will override it
149
- payerKey : new PublicKey ( "placeholder" ) ,
150
- recentBlockhash : latestBlockhash . blockhash ,
151
- instructions,
152
- } ) . compileToV0Message ( ) ;
153
- const transaction = new VersionedTransaction ( message ) ;
154
- const serializedVersionedTransaction = transaction . serialize ( ) ;
155
- const encodedVersionedTransaction = bs58 . encode ( serializedVersionedTransaction ) ;
156
- >>> > >>> origin / main
157
-
158
- const { id : transactionId } = await client . createTransactionForCustodialWallet (
159
- locator ,
160
- encodedVersionedTransaction ,
161
- ) ;
162
-
163
- while ( true ) {
164
- const latestTransaction = await client . checkTransactionStatus ( locator , transactionId ) ;
165
-
166
- if ( latestTransaction . status === "success" ) {
167
- console . log ( `Transaction ${ latestTransaction . status } ` ) ;
168
- return {
169
- hash : latestTransaction . onChain ?. txId ?? "" ,
170
- } ;
171
- }
172
-
173
- if ( latestTransaction . status === "failed" ) {
174
- throw new Error ( `Transaction failed: ${ latestTransaction . onChain ?. txId } ` ) ;
175
- }
176
-
177
- await new Promise ( ( resolve ) => setTimeout ( resolve , 3000 ) ) ; // Wait 3 seconds
178
- }
179
- } ,
180
- async read ( request : SolanaReadRequest ) {
181
- const { accountAddress } = request ;
182
-
183
- const pubkey = new PublicKey ( accountAddress ) ;
184
- const accountInfo = await connection . getAccountInfo ( pubkey ) ;
185
-
186
- if ( ! accountInfo ) {
187
- throw new Error ( `Account ${ accountAddress } not found` ) ;
188
- }
189
-
190
- return {
191
- value : accountInfo ,
192
- } ;
193
- } ,
194
- async balanceOf ( address : string) {
195
- const pubkey = new PublicKey ( address ) ;
196
- const balance = await connection . getBalance ( pubkey ) ;
197
-
198
- return {
199
- value : BigInt ( balance ) ,
200
- decimals : 9 ,
201
- symbol: "SOL ",
202
- name : "Solana ",
203
- } ;
204
- } ,
205
- } ;
206
- } ;
118
+
119
+ const { id : transactionId } =
120
+ await client . createTransactionForCustodialWallet (
121
+ locator ,
122
+ encodedVersionedTransaction ,
123
+ ) ;
124
+
125
+ while ( true ) {
126
+ const latestTransaction = await client . checkTransactionStatus (
127
+ locator ,
128
+ transactionId ,
129
+ ) ;
130
+
131
+ if ( latestTransaction . status === "success" ) {
132
+ console . log ( `Transaction ${ latestTransaction . status } ` ) ;
133
+ return {
134
+ hash : latestTransaction . onChain ?. txId ?? "" ,
135
+ } ;
136
+ }
137
+
138
+ if ( latestTransaction . status === "failed" ) {
139
+ throw new Error (
140
+ `Transaction failed: ${ latestTransaction . onChain ?. txId } ` ,
141
+ ) ;
142
+ }
143
+
144
+ await new Promise ( ( resolve ) => setTimeout ( resolve , 3000 ) ) ; // Wait 3 seconds
145
+ }
146
+ } ,
147
+ async read ( request : SolanaReadRequest ) {
148
+ const { accountAddress } = request ;
149
+
150
+ const pubkey = new PublicKey ( accountAddress ) ;
151
+ const accountInfo = await connection . getAccountInfo ( pubkey ) ;
152
+
153
+ if ( ! accountInfo ) {
154
+ throw new Error ( `Account ${ accountAddress } not found` ) ;
155
+ }
156
+
157
+ return {
158
+ value : accountInfo ,
159
+ } ;
160
+ } ,
161
+ async balanceOf ( address : string ) {
162
+ const pubkey = new PublicKey ( address ) ;
163
+ const balance = await connection . getBalance ( pubkey ) ;
164
+
165
+ return {
166
+ value : BigInt ( balance ) ,
167
+ decimals : 9 ,
168
+ symbol : "SOL" ,
169
+ name : "Solana" ,
170
+ } ;
171
+ } ,
172
+ } ;
173
+ } ;
207
174
}
0 commit comments