-
Notifications
You must be signed in to change notification settings - Fork 30
Feature for confirm custom transaction #306
base: master
Are you sure you want to change the base?
Changes from 16 commits
3632946
9d87cd3
c448d8b
f4abf5c
48e86ff
14a2581
1dae483
91c83ca
f1b43b4
e2bf87b
464d6cf
b77cd48
49bf426
acd1538
cf9df05
4c741aa
4a66a1c
e75f537
ab5bc9b
15c86ed
217d344
a528678
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,3 +4,4 @@ | |
/dist/main.css | ||
*.sw[po] | ||
/store | ||
.idea |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -321,14 +321,16 @@ export class Wallet extends React.Component { | |
account: null, | ||
requestMode: false, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's change this state flag from a boolean to an enum and rename to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done |
||
requesterOrigin: '*', | ||
requestPending: false, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why did you remove this? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The user may not close the wallet window after the last request and may simply not respond to it, but may also request a new action in the DAPP therefore. For useful, I allowed DAPP to update the request, and in the future we will need a request queue. |
||
requestedPublicKey: '', | ||
requestedAmount: '', | ||
recipientPublicKey: '', | ||
recipientAmount: '', | ||
recipientIdentity: null, | ||
confirmationSignature: null, | ||
transactionConfirmed: null, | ||
unsignedTransaction: null, | ||
formattedUnsignedTransaction: '', | ||
description: '', | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please namespace this: There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done |
||
}; | ||
|
||
setConfirmationSignature(confirmationSignature) { | ||
|
@@ -414,7 +416,6 @@ export class Wallet extends React.Component { | |
}; | ||
|
||
onAddFunds(params, origin) { | ||
if (!params || this.state.requestPending) return; | ||
if (!params.pubkey || !params.network) { | ||
if (!params.pubkey) this.addError(`Request did not specify a public key`); | ||
if (!params.network) this.addError(`Request did not specify a network`); | ||
|
@@ -439,12 +440,62 @@ export class Wallet extends React.Component { | |
|
||
this.setState({ | ||
requesterOrigin: origin, | ||
requestPending: true, | ||
requestedAmount: `${params.amount || ''}`, | ||
requestedPublicKey: params.pubkey, | ||
}); | ||
} | ||
|
||
onSendCustomTransactionRequest(params, origin) { | ||
if (!params.format || !params.network || !params.transaction) { | ||
if (!params.network) this.addError(`Request did not specify a network`); | ||
if (!params.format) this.addError(`Request did not specify a transaction format`); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Looks like you aren't checking the value of There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done |
||
if (!params.transaction) this.addError(`Request did not specify a transaction`); | ||
return; | ||
} | ||
|
||
let requestedNetwork; | ||
try { | ||
requestedNetwork = new URL(params.network).origin; | ||
} catch (err) { | ||
this.addError(`Request network is invalid: "${params.network}"`); | ||
return; | ||
} | ||
|
||
const walletNetwork = new URL(this.props.store.networkEntryPoint).origin; | ||
if (requestedNetwork !== walletNetwork) { | ||
this.props.store.setNetworkEntryPoint(requestedNetwork); | ||
this.addWarning( | ||
`Changed wallet network from "${walletNetwork}" to "${requestedNetwork}"`, | ||
); | ||
} | ||
|
||
const transaction = new web3.Transaction(); | ||
const inputs = JSON.parse(params.transaction); | ||
|
||
inputs.map(input => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not a big fan of this approach because if we change Transaction structure, it would break. I would prefer to rely on the web3 sdk serialization methods instead for passing messages: I like that you are displaying the json formatted transaction in the confirmation before sending. I think that a toJSON method would be nice to have inside Web3, thoughts? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, I also wanted to do this, but SDK does not serialize an unsigned transaction, can you give an example how to do it?
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Check out solana-labs/solana-web3.js@85b354e There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. need to fill out There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Setting a placeholder is fine because the wallet will always set the
|
||
const converted = {}; | ||
converted.keys = []; | ||
converted.programId = new web3.PublicKey(input.programId); | ||
converted.data = Buffer.from(input.data, 'hex'); | ||
input.keys.map(key => { | ||
converted.keys.push({ | ||
pubkey: new web3.PublicKey(key.pubkey), | ||
isSigner: key.isSigner, | ||
isDebitable: key.isDebitable, | ||
}); | ||
}); | ||
transaction.add(converted); | ||
}); | ||
|
||
this.setState({ | ||
requesterOrigin: origin, | ||
description: params.description ? params.description : '', | ||
unsignedTransaction: transaction, | ||
formattedUnsignedTransaction: JSON.stringify(JSON.parse(params.transaction), null, 4), | ||
requestedPublicKey: null, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. please remove, we can use a different approach to distinguish between "addFunds" and "sendCustomTransaction" There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done |
||
}); | ||
} | ||
|
||
postWindowMessage(method, params) { | ||
if (window.opener) { | ||
window.opener.postMessage({method, params}, this.state.requesterOrigin); | ||
|
@@ -457,7 +508,10 @@ export class Wallet extends React.Component { | |
if (e.data) { | ||
switch (e.data.method) { | ||
case 'addFunds': | ||
this.onAddFunds(e.data.params, e.origin); | ||
this.onAddFunds(e.data.params, e.currentTarget.origin); | ||
return true; | ||
case 'sendCustomTransaction': | ||
this.onSendCustomTransactionRequest(e.data.params, e.currentTarget.origin); | ||
return true; | ||
} | ||
} | ||
|
@@ -508,7 +562,7 @@ export class Wallet extends React.Component { | |
sendTransaction(closeOnSuccess) { | ||
this.runModal('Sending Transaction', 'Please wait...', async () => { | ||
const amount = this.state.recipientAmount; | ||
this.setState({requestedAmount: '', requestPending: false}); | ||
this.setState({requestedAmount: ''}); | ||
const transaction = web3.SystemProgram.transfer( | ||
this.state.account.publicKey, | ||
new web3.PublicKey(this.state.recipientPublicKey), | ||
|
@@ -542,10 +596,43 @@ export class Wallet extends React.Component { | |
}); | ||
} | ||
|
||
signAndSendTransaction(closeOnSuccess) { | ||
this.runModal('Sending Transaction', 'Please wait...', async () => { | ||
let signature = ''; | ||
try { | ||
signature = await web3.sendAndConfirmTransaction( | ||
this.web3sol, | ||
this.state.unsignedTransaction, | ||
this.state.account, | ||
); | ||
} catch (err) { | ||
// Transaction failed but fees were still taken | ||
this.setState({ | ||
balance: await this.web3sol.getBalance(this.state.account.publicKey), | ||
}); | ||
this.postWindowMessage('sendCustomTransactionResponse', {err: true}); | ||
throw err; | ||
} | ||
|
||
this.postWindowMessage('sendCustomTransactionResponse', {signature}); | ||
if (closeOnSuccess) { | ||
window.close(); | ||
} else { | ||
this.setState({ | ||
balance: await this.web3sol.getBalance(this.state.account.publicKey), | ||
}); | ||
} | ||
}); | ||
} | ||
|
||
confirmTransaction() { | ||
jstarry marked this conversation as resolved.
Show resolved
Hide resolved
|
||
this.runModal('Confirming Transaction', 'Please wait...', async () => { | ||
const signature = await this.web3sol.sendTransaction( | ||
this.state.unsignedTransaction, | ||
this.state.account, | ||
); | ||
const result = await this.web3sol.confirmTransaction( | ||
this.state.confirmationSignature, | ||
signature, | ||
); | ||
this.setState({ | ||
transactionConfirmed: result, | ||
|
@@ -557,6 +644,8 @@ export class Wallet extends React.Component { | |
return ( | ||
this.state.recipientPublicKey === null || | ||
this.state.recipientAmount === null | ||
) && ( | ||
this.state.formattedUnsignedTransaction === null | ||
); | ||
} | ||
|
||
|
@@ -586,7 +675,7 @@ export class Wallet extends React.Component { | |
{busyModal} | ||
{settingsModal} | ||
{this.state.requestMode | ||
? this.renderTokenRequestPanel() | ||
? (this.state.requestedPublicKey ? this.renderTokenRequestPanel() : this.renderSendCustomTransactionRequestPanel()) | ||
: this.renderMainPanel()} | ||
</div> | ||
); | ||
|
@@ -817,10 +906,46 @@ export class Wallet extends React.Component { | |
</Col> | ||
</Row> | ||
</Grid> | ||
<div className="container">{this.renderPanels()}</div> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please revert this change There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done |
||
</div> | ||
); | ||
} | ||
|
||
renderSendCustomTransactionRequestPanel() { | ||
return ( | ||
<Panel> | ||
<Panel.Heading>Send custom transaction Request</Panel.Heading> | ||
<Panel.Body> | ||
<div> | ||
{this.state.description.split('\n').map((i, key) => { | ||
return <p key={key}>{i}</p>; | ||
})} | ||
</div> | ||
<div className="btns"> | ||
<Button | ||
disabled={this.sendDisabled()} | ||
onClick={() => this.signAndSendTransaction(false)} | ||
> | ||
Send | ||
</Button> | ||
<Button | ||
disabled={this.sendDisabled()} | ||
onClick={() => this.signAndSendTransaction(true)} | ||
> | ||
Send & Close | ||
</Button> | ||
</div> | ||
<p/> | ||
<div> | ||
<pre> | ||
{this.state.formattedUnsignedTransaction} | ||
</pre> | ||
</div> | ||
</Panel.Body> | ||
</Panel> | ||
); | ||
} | ||
|
||
renderSendTokensPanel() { | ||
return ( | ||
<Panel> | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We need to re-organize this README section rather than adding more things to the "Funding dApps" section. I recommend
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done