Skip to content

Commit

Permalink
Add change address, to use in ledger address confirmation
Browse files Browse the repository at this point in the history
  • Loading branch information
Duddino committed Jan 30, 2024
1 parent b80af79 commit 6946151
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 2 deletions.
8 changes: 7 additions & 1 deletion scripts/legacy.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,15 @@ export async function createAndSendTransaction({
delegateChange = false,
changeDelegationAddress = null,
isProposal = false,
changeAddress = '',
}) {
const tx = wallet.createTransaction(address, amount, {
isDelegation,
useDelegatedInputs,
delegateChange,
changeDelegationAddress,
isProposal,
changeAddress,
});
if (!wallet.isHardwareWallet()) await wallet.sign(tx);
else {
Expand Down Expand Up @@ -72,7 +74,10 @@ export async function undelegateGUI() {
if (!validateAmount(nAmount)) return;

// Generate a new address to undelegate towards
const [address] = wallet.getNewAddress(1);

const [address] = await getNewAddress({
verify: wallet.isHardwareWallet(),
});

// Perform the TX
const cTxRes = await createAndSendTransaction({
Expand All @@ -82,6 +87,7 @@ export async function undelegateGUI() {
useDelegatedInputs: true,
delegateChange: !wallet.isHardwareWallet(),
changeDelegationAddress: await wallet.getColdStakingAddress(),
changeAddress: address,
});

if (!cTxRes.ok && cTxRes.err === 'No change addr') {
Expand Down
3 changes: 2 additions & 1 deletion scripts/wallet.js
Original file line number Diff line number Diff line change
Expand Up @@ -666,6 +666,7 @@ export class Wallet {
delegateChange = false,
changeDelegationAddress = null,
isProposal = false,
changeAddress = '',
} = {}
) {
const balance = useDelegatedInputs
Expand All @@ -689,7 +690,7 @@ export class Wallet {

// Add change output
if (changeValue > 0) {
const [changeAddress] = this.getNewAddress(1);
if (!changeAddress) [changeAddress] = this.getNewAddress(1);
if (delegateChange && changeValue > 1.01 * COIN) {
transactionBuilder.addColdStakeOutput({
address: changeAddress,
Expand Down
32 changes: 32 additions & 0 deletions tests/unit/wallet.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,38 @@ describe('Wallet transaction tests', () => {
);
});

it('Creates a tx with change address', async () => {
const wallet = new Wallet(0, false);
wallet.setMasterKey(getLegacyMainnet());
const tx = wallet.createTransaction(
'DLabsktzGMnsK5K9uRTMCF6NoYNY6ET4Bb',
0.05 * 10 ** 8,
{ changeAddress: 'D8Ervc3Ka6TuKgvXZH9Eo4ou24AiVwTbL6' }
);
expect(tx.version).toBe(1);
expect(tx.vin[0]).toStrictEqual(
new CTxIn({
outpoint: new COutpoint({
txid: 'f8f968d80ac382a7b64591cc166489f66b7c4422f95fbd89f946a5041d285d7c',
n: 1,
}),
scriptSig: '76a914f49b25384b79685227be5418f779b98a6be4c73888ac', // Script sig must be the UTXO script since it's not signed
})
);
expect(tx.vout[0]).toStrictEqual(
new CTxOut({
script: '76a91421ff8214d09d60713b89809bb413a0651ee6931488ac',
value: 4992400,
})
);
expect(tx.vout[1]).toStrictEqual(
new CTxOut({
script: '76a914a95cc6408a676232d61ec29dc56a180b5847835788ac',
value: 5000000,
})
);
});

it('Creates a proposal tx correctly', async () => {
const wallet = new Wallet(0, false);
wallet.setMasterKey(getLegacyMainnet());
Expand Down

0 comments on commit 6946151

Please sign in to comment.