-
Notifications
You must be signed in to change notification settings - Fork 61
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
133 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,33 @@ | ||
import { Connection, Keypair } from '@solana/web3.js'; | ||
import { SolanaPlatform } from '../platform'; | ||
import { SolanaSigner } from './signer'; | ||
import { Signer, encoding } from '@wormhole-foundation/connect-sdk'; | ||
import { SolanaSendSigner } from './sendSigner'; | ||
|
||
// returns a SignOnlySigner for the Solana platform | ||
export async function getSolanaSigner( | ||
rpc: Connection, | ||
privateKey: string, | ||
): Promise<Signer> { | ||
const [_, chain] = await SolanaPlatform.chainFromRpc(rpc); | ||
return new SolanaSigner( | ||
chain, | ||
Keypair.fromSecretKey(encoding.b58.decode(privateKey)), | ||
); | ||
} | ||
|
||
// returns a SignAndSendSigner for the Solana platform | ||
export async function getSolanaSignAndSendSigner( | ||
rpc: Connection, | ||
privateKey: string, | ||
): Promise<Signer> { | ||
const [_, chain] = await SolanaPlatform.chainFromRpc(rpc); | ||
return new SolanaSendSigner( | ||
rpc, | ||
chain, | ||
Keypair.fromSecretKey(encoding.b58.decode(privateKey)), | ||
); | ||
} | ||
|
||
export * from './signer'; | ||
export * from './sendSigner'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
import { Connection, Keypair, Transaction } from '@solana/web3.js'; | ||
import { | ||
SignAndSendSigner, | ||
UnsignedTransaction, | ||
} from '@wormhole-foundation/connect-sdk'; | ||
import { Network } from '@wormhole-foundation/sdk-base/src'; | ||
import { SolanaPlatform } from '../platform'; | ||
import { SolanaChains } from '../types'; | ||
import { SolanaUnsignedTransaction } from '../unsignedTransaction'; | ||
|
||
export class SolanaSendSigner< | ||
N extends Network, | ||
C extends SolanaChains = 'Solana', | ||
> implements SignAndSendSigner<N, C> | ||
{ | ||
constructor( | ||
private _rpc: Connection, | ||
private _chain: C, | ||
private _keypair: Keypair, | ||
private _debug: boolean = false, | ||
) {} | ||
|
||
chain(): C { | ||
return this._chain; | ||
} | ||
|
||
address(): string { | ||
return this._keypair.publicKey.toBase58(); | ||
} | ||
|
||
async signAndSend(tx: UnsignedTransaction[]): Promise<any[]> { | ||
const txids: string[] = []; | ||
|
||
const { blockhash, lastValidBlockHeight } = | ||
await SolanaPlatform.latestBlock(this._rpc); | ||
|
||
for (const txn of tx) { | ||
const { description, transaction } = txn as SolanaUnsignedTransaction< | ||
N, | ||
C | ||
>; | ||
console.log(`Signing: ${description} for ${this.address()}`); | ||
|
||
if (this._debug) { | ||
const st = transaction as Transaction; | ||
console.log(st.signatures); | ||
console.log(st.feePayer); | ||
st.instructions.forEach((ix) => { | ||
console.log('Program', ix.programId.toBase58()); | ||
console.log('Data: ', ix.data.toString('hex')); | ||
console.log( | ||
'Keys: ', | ||
ix.keys.map((k) => [k, k.pubkey.toBase58()]), | ||
); | ||
}); | ||
} | ||
|
||
transaction.partialSign(this._keypair); | ||
|
||
const txid = await this._rpc.sendRawTransaction(transaction.serialize(), { | ||
// skipPreflight: true, | ||
// preflightCommitment: this._rpc.commitment, | ||
maxRetries: 5, | ||
}); | ||
|
||
console.log(`Sent: ${description} for ${this.address()}`); | ||
|
||
await this._rpc.confirmTransaction({ | ||
signature: txid, | ||
blockhash, | ||
lastValidBlockHeight, | ||
}); | ||
|
||
txids.push(txid); | ||
} | ||
|
||
return txids; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters