Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add supporting Versioned Transaction signing #9

Merged
4 changes: 2 additions & 2 deletions packages/walletconnect-solana/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
"package": "shx echo '{ \"type\": \"commonjs\" }' > lib/cjs/package.json"
},
"devDependencies": {
"@solana/web3.js": "^1.52.0",
"@solana/web3.js": "^1.63.0",
"@types/node-fetch": "^2.6.2",
"@types/pino": "6.3.11",
"@walletconnect/types": "^2.4.5",
Expand All @@ -38,6 +38,6 @@
"bs58": "^5.0.0"
},
"peerDependencies": {
"@solana/web3.js": "^1.52.0"
"@solana/web3.js": "^1.63.0"
jnwng marked this conversation as resolved.
Show resolved Hide resolved
}
}
56 changes: 49 additions & 7 deletions packages/walletconnect-solana/src/adapter.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import type { Transaction } from '@solana/web3.js';
import { PublicKey } from '@solana/web3.js';
import { Transaction, VersionedTransaction, PublicKey } from '@solana/web3.js';
import QRCodeModal from '@walletconnect/qrcode-modal';
import WalletConnectClient from '@walletconnect/sign-client';
import type { EngineTypes, SessionTypes, SignClientTypes } from '@walletconnect/types';
Expand Down Expand Up @@ -36,6 +35,9 @@ const getConnectParams = (chainId: WalletConnectChainID): EngineTypes.FindParams
},
});

const isVersionedTransaction = (transaction: Transaction | VersionedTransaction): transaction is VersionedTransaction =>
'version' in transaction;

export class WalletConnectWallet {
private _client: WalletConnectClient | undefined;
private _session: SessionTypes.Struct | undefined;
Expand Down Expand Up @@ -115,16 +117,56 @@ export class WalletConnectWallet {
}
}

async signTransaction(transaction: Transaction): Promise<Transaction> {
async signTransaction<T extends Transaction | VersionedTransaction>(transaction: T): Promise<T> {
if (this._client && this._session) {
let rawTransaction: string;
let legacyTransaction: Transaction | undefined;
jnwng marked this conversation as resolved.
Show resolved Hide resolved

if (isVersionedTransaction(transaction)) {
rawTransaction = Buffer.from(transaction.serialize()).toString('base64');
if (transaction.version === 'legacy') {
// Build Transaction for legacy sign transaction request format
legacyTransaction = Transaction.from(transaction.serialize());
}
} else {
rawTransaction = transaction
.serialize({
requireAllSignatures: false,
verifySignatures: false,
})
.toString('base64');
legacyTransaction = transaction;
}

const signature = await this._signTransaction(rawTransaction, legacyTransaction);
transaction.addSignature(this.publicKey, Buffer.from(base58.decode(signature)));
return transaction;
} else {
throw new ClientNotInitializedError();
}
}

private async _signTransaction(
rawTransaction: string,
legacyTransaction?: Transaction | undefined
): Promise<string> {
if (this._client && this._session) {
const { signature } = await this._client.request<{ signature: string }>({
chainId: this._network,
topic: this._session.topic,
request: { method: WalletConnectRPCMethods.signTransaction, params: { ...transaction } },
request: {
method: WalletConnectRPCMethods.signTransaction,
params: {
// Passing ...legacyTransaction is deprecated.
// All new clients should rely on transaction parameter.
// The future versions will stop passing ...legacyTransaction.
...legacyTransaction,
// New base64-encoded serialized transaction request parameter
transaction: rawTransaction,
},
},
});
transaction.addSignature(this.publicKey, Buffer.from(base58.decode(signature)));

return transaction;
return signature;
} else {
throw new ClientNotInitializedError();
}
Expand Down
Loading