This repository has been archived by the owner on Jun 21, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
phantom.ts
148 lines (127 loc) · 4 KB
/
phantom.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
import {
Connection,
Keypair,
PublicKey,
SendOptions,
Transaction,
VersionedTransaction,
} from "@solana/web3.js";
import {
Chain,
Network,
SignAndSendSigner,
UnsignedTransaction,
Wormhole,
} from "@wormhole-foundation/sdk";
import "./App.css";
// Note: below is from the phantom sandbox
// https://github.com/phantom/sandbox/blob/main/src/types.ts
type DisplayEncoding = "utf8" | "hex";
type PhantomEvent = "connect" | "disconnect" | "accountChanged";
type PhantomRequestMethod =
| "connect"
| "disconnect"
| "signAndSendTransaction"
| "signAndSendTransactionV0"
| "signAndSendTransactionV0WithLookupTable"
| "signTransaction"
| "signAllTransactions"
| "signMessage";
interface ConnectOpts {
onlyIfTrusted: boolean;
}
export interface PhantomProvider {
publicKey: PublicKey | null;
isConnected: boolean | null;
isPhantom: true;
signAndSendTransaction: (
transaction: Transaction | VersionedTransaction,
opts?: SendOptions
) => Promise<{ signature: string; publicKey: PublicKey }>;
signTransaction: (
transaction: Transaction | VersionedTransaction
) => Promise<Transaction | VersionedTransaction>;
signAllTransactions: (
transactions: (Transaction | VersionedTransaction)[]
) => Promise<(Transaction | VersionedTransaction)[]>;
signMessage: (
message: Uint8Array | string,
display?: DisplayEncoding
) => Promise<any>;
connect: (opts?: Partial<ConnectOpts>) => Promise<{ publicKey: PublicKey }>;
disconnect: () => Promise<void>;
on: (event: PhantomEvent, handler: (args: any) => void) => void;
request: (method: PhantomRequestMethod, params: any) => Promise<unknown>;
}
// TODO: is this really not provided by web3.js?
export function isVersionedTransaction(tx: any): tx is VersionedTransaction {
return (
(<VersionedTransaction>tx).signatures !== undefined &&
(<VersionedTransaction>tx).message !== undefined
);
}
export class PhantomSigner implements SignAndSendSigner<Network, Chain> {
private constructor(
private connection: Connection,
private provider: PhantomProvider,
private _address: string,
private _chain: Chain
) {
// TODO: Set up event handlers?
}
static async fromProvider(wh: Wormhole<Network>, provider: PhantomProvider) {
if (!provider.publicKey)
throw new Error("No public key, did you forget to call connect?");
const ctx = wh.getChain("Solana");
return new PhantomSigner(
await ctx.getRpc(),
provider,
provider.publicKey.toBase58(),
"Solana"
);
}
chain(): Chain {
return this._chain;
}
address(): string {
return this._address;
}
async signAndSend(txs: UnsignedTransaction[]): Promise<string[]> {
const txids: string[] = [];
for (const txn of txs) {
const { description, transaction } = txn as UnsignedTransaction<
Network,
"Solana"
>;
console.log(`Signing ${description}`);
const { transaction: tx, signers } = transaction as {
transaction: Transaction | VersionedTransaction;
signers?: Keypair[];
};
// Set recent blockhash
const { blockhash } = await this.connection.getLatestBlockhash();
if (isVersionedTransaction(tx)) {
tx.message.recentBlockhash = blockhash;
if (signers && signers.length > 0) tx.sign(signers);
} else {
transaction.recentBlockhash = blockhash;
if (signers && signers.length > 0) tx.partialSign(...signers);
}
// Partial sign with any signers passed in
// NOTE: this _must_ come after any modifications to the transaction
// otherwise, the signature wont verify
const { signature: txid } =
await this.provider.signAndSendTransaction(tx);
if (!txid)
throw new Error("Could not determine if transaction was sign and sent");
txids.push(txid);
}
// Make sure they're all finalized
await Promise.all(
txids.map(async (txid) =>
this.connection.confirmTransaction(txid, "finalized")
)
);
return txids;
}
}