SHA256
Исправить подключение и подпись в браузерном кошельке
This commit is contained in:
@@ -2,6 +2,7 @@ import { PublicKey, Transaction, VersionedTransaction } from './js/lib/vendor/so
|
||||
|
||||
const PAGE_REQUEST = 'shine-wallet-page-request';
|
||||
const PAGE_RESPONSE = 'shine-wallet-page-response';
|
||||
const PAGE_MESSAGE_TARGET_ORIGIN = '*';
|
||||
const STANDARD_REGISTER_EVENT = 'wallet-standard:register-wallet';
|
||||
const STANDARD_APP_READY_EVENT = 'wallet-standard:app-ready';
|
||||
const SOLANA_CHAINS = ['solana:mainnet', 'solana:devnet', 'solana:testnet'];
|
||||
@@ -39,6 +40,45 @@ function createProviderError(message, code = '') {
|
||||
return error;
|
||||
}
|
||||
|
||||
function summarizeTransaction(transaction) {
|
||||
const summary = {
|
||||
kind: 'legacy',
|
||||
instructionCount: 0,
|
||||
accountCount: 0,
|
||||
feePayer: '',
|
||||
recentBlockhash: '',
|
||||
programs: [],
|
||||
};
|
||||
if (!transaction) return summary;
|
||||
|
||||
const isVersioned = typeof transaction?.version === 'number' || transaction instanceof VersionedTransaction;
|
||||
summary.kind = isVersioned ? `versioned:${String(transaction.version)}` : 'legacy';
|
||||
summary.feePayer = String(transaction?.feePayer?.toBase58?.() || '').trim();
|
||||
summary.recentBlockhash = String(transaction?.recentBlockhash || transaction?.message?.recentBlockhash || '').trim();
|
||||
|
||||
if (isVersioned) {
|
||||
const message = transaction?.message || {};
|
||||
const staticKeys = Array.isArray(message?.staticAccountKeys) ? message.staticAccountKeys : [];
|
||||
const instructions = Array.isArray(message?.compiledInstructions) ? message.compiledInstructions : [];
|
||||
summary.instructionCount = instructions.length;
|
||||
summary.accountCount = staticKeys.length;
|
||||
summary.programs = instructions
|
||||
.map((instruction) => staticKeys[instruction?.programIdIndex]?.toBase58?.() || '')
|
||||
.filter(Boolean)
|
||||
.slice(0, 5);
|
||||
return summary;
|
||||
}
|
||||
|
||||
const instructions = Array.isArray(transaction?.instructions) ? transaction.instructions : [];
|
||||
summary.instructionCount = instructions.length;
|
||||
summary.accountCount = Array.isArray(transaction?.signatures) ? transaction.signatures.length : 0;
|
||||
summary.programs = instructions
|
||||
.map((instruction) => instruction?.programId?.toBase58?.() || '')
|
||||
.filter(Boolean)
|
||||
.slice(0, 5);
|
||||
return summary;
|
||||
}
|
||||
|
||||
function createRequest(method, params = {}) {
|
||||
const id = `shine-wallet-${Date.now()}-${Math.random().toString(16).slice(2, 8)}`;
|
||||
return new Promise((resolve, reject) => {
|
||||
@@ -59,7 +99,7 @@ function createRequest(method, params = {}) {
|
||||
id,
|
||||
method,
|
||||
params,
|
||||
}, window.location.origin);
|
||||
}, PAGE_MESSAGE_TARGET_ORIGIN);
|
||||
});
|
||||
}
|
||||
|
||||
@@ -122,12 +162,6 @@ class ShineProviderCore {
|
||||
|
||||
async connect(options = {}) {
|
||||
const onlyIfTrusted = !!options?.onlyIfTrusted || !!options?.silent;
|
||||
if (!onlyIfTrusted) {
|
||||
const confirmed = window.confirm(`Connect SHiNE Wallet to ${window.location.origin}?`);
|
||||
if (!confirmed) {
|
||||
throw createProviderError('User rejected wallet connection', 'USER_REJECTED');
|
||||
}
|
||||
}
|
||||
const result = await createRequest('connect', { onlyIfTrusted });
|
||||
const nextKey = new PublicKey(String(result?.publicKeyBase58 || '').trim());
|
||||
this.publicKey = nextKey;
|
||||
@@ -157,10 +191,12 @@ class ShineProviderCore {
|
||||
await this.connect();
|
||||
}
|
||||
const transactionBase64 = serializeTransactionBase64(transaction);
|
||||
const transactionSummary = summarizeTransaction(transaction);
|
||||
const result = await createRequest('signTransaction', {
|
||||
publicKeyBase58: this.publicKeyBase58,
|
||||
transactionBase64,
|
||||
comment: String(comment || '').trim() || `Site ${window.location.origin} requested transaction signature`,
|
||||
transactionSummary,
|
||||
});
|
||||
return deserializeSignedTransaction(String(result?.signedTransactionBase64 || ''), transaction);
|
||||
}
|
||||
@@ -169,10 +205,20 @@ class ShineProviderCore {
|
||||
if (!this.publicKey) {
|
||||
await this.connect();
|
||||
}
|
||||
const transactionSummary = {
|
||||
kind: 'raw-bytes',
|
||||
instructionCount: 0,
|
||||
accountCount: 0,
|
||||
feePayer: this.publicKeyBase58,
|
||||
recentBlockhash: '',
|
||||
programs: [],
|
||||
byteLength: Number(transactionBytes?.length || 0),
|
||||
};
|
||||
const result = await createRequest('signTransaction', {
|
||||
publicKeyBase58: this.publicKeyBase58,
|
||||
transactionBase64: bytesToBase64(transactionBytes),
|
||||
comment: String(comment || '').trim() || `Site ${window.location.origin} requested transaction signature`,
|
||||
transactionSummary,
|
||||
});
|
||||
return base64ToBytes(String(result?.signedTransactionBase64 || '').trim());
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user