Skip to content

Commit

Permalink
Use staking path as a reward address, cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
xdzurman committed May 11, 2021
1 parent 6b4c2db commit 52657f5
Show file tree
Hide file tree
Showing 14 changed files with 64 additions and 75 deletions.
10 changes: 2 additions & 8 deletions app/frontend/actions/voting.tsx
Expand Up @@ -56,20 +56,15 @@ export default (store: Store) => {
const stakePubKey = xpub2pub(
Buffer.from(getSourceAccountInfo(state).stakingXpub.xpubHex, 'hex')
).toString('hex')
const changeAddress = await getWallet()
.getAccount(state.sourceAccountIndex)
.getChangeAddress()
const nonce = await getWallet()
.getAccount(state.sourceAccountIndex)
.calculateTtl()

const sourceAccount = getSourceAccountInfo(state)
const txPlanResult = await prepareTxPlan({
txType: TxType.REGISTER_VOTING,
votingPubKey,
stakePubKey,
rewardDestinationAddress: {
address: changeAddress,
},
stakingAddress: sourceAccount.stakingAddress,
nonce: BigInt(nonce),
})

Expand All @@ -82,7 +77,6 @@ export default (store: Store) => {
plan: txPlanResult.txPlan,
transactionSummary: summary,
})
const sourceAccount = getSourceAccountInfo(state)
await confirmTransaction(getState(), {
sourceAccountIndex: sourceAccount.accountIndex,
txPlan: txPlanResult.txPlan,
Expand Down
Expand Up @@ -240,12 +240,8 @@ const WithdrawReview = ({

const VotingRegistrationReview = ({
transactionSummary,
onSubmit,
onCancel,
}: {
transactionSummary: TransactionSummary & VotingRegistrationTransactionSummary
onSubmit: () => any
onCancel: () => any
}) => {
const {fee} = transactionSummary
const total = fee as Lovelace
Expand All @@ -254,10 +250,7 @@ const VotingRegistrationReview = ({
<div className="review">
<div className="review-label">Reward address</div>
<div className="review-address">
{transactionSummary.plan.change.address}
<div className="review-address-verification">
<AddressVerification address={transactionSummary.plan.change.address} />
</div>
{transactionSummary.plan.auxiliaryData.rewardDestinationAddress.address}
</div>
<div className="review-label">Voting key</div>
<div className="review-amount">
Expand Down Expand Up @@ -376,8 +369,6 @@ const ConfirmTransactionDialog = () => {
return (
<VotingRegistrationReview
transactionSummary={cachedTransactionSummaries[TxType.REGISTER_VOTING]}
onSubmit={onSubmit}
onCancel={cancelTransaction}
/>
)
}
Expand Down
4 changes: 1 addition & 3 deletions app/frontend/types.ts
Expand Up @@ -267,9 +267,7 @@ export type VotingRegistrationTxPlanArgs = {
txType: TxType.REGISTER_VOTING
votingPubKey: HexString
stakePubKey: HexString
rewardDestinationAddress: {
address: Address
}
stakingAddress: Address
nonce: BigInt
}

Expand Down
29 changes: 19 additions & 10 deletions app/frontend/wallet/account.ts
Expand Up @@ -37,6 +37,7 @@ import {aggregateTokenBundles} from './helpers/tokenFormater'
import {StakepoolDataProvider} from '../helpers/dataProviders/types'
import {unsignedPoolTxToTxPlan} from './shelley/helpers/stakepoolRegistrationUtils'
import {InternalError, InternalErrorReason, UnexpectedError, UnexpectedErrorReason} from '../errors'
import assertUnreachable from '../helpers/assertUnreachable'

const DummyAddressManager = () => {
return {
Expand Down Expand Up @@ -218,6 +219,22 @@ const Account = ({
}
}

function mapAuxiliaryData(auxiliaryData: TxAuxiliaryData): TxAuxiliaryData {
if (!auxiliaryData) return null
if (auxiliaryData.type === 'CATALYST_VOTING') {
const rewardAddress = auxiliaryData.rewardDestinationAddress.address
return {
...auxiliaryData,
rewardDestinationAddress: {
address: rewardAddress,
stakingPath: myAddresses.getAddressToAbsPathMapper()(rewardAddress),
},
}
} else {
return assertUnreachable(auxiliaryData.type)
}
}

async function prepareTxAux(txPlan: TxPlan, ttl?: number, validityIntervalStart?: number) {
const {inputs, outputs, change, fee, certificates, withdrawals, auxiliaryData} = txPlan
const txOutputs = [...outputs]
Expand All @@ -235,16 +252,8 @@ const Account = ({
// tll is null if it's deliberately empty
const txTtl = ttl === undefined ? await calculateTtl() : ttl
const txValidityIntervalStart = validityIntervalStart ?? null
const mappedAuxiliaryData: TxAuxiliaryData = auxiliaryData
? {
...auxiliaryData,
rewardDestinationAddress: {
address: change.address,
spendingPath: myAddresses.getAddressToAbsPathMapper()(change.address),
stakingPath: myAddresses.getAddressToAbsPathMapper()(stakingAddress),
},
}
: null
const mappedAuxiliaryData = mapAuxiliaryData(auxiliaryData)

return ShelleyTxAux({
inputs,
outputs: txOutputs,
Expand Down
8 changes: 3 additions & 5 deletions app/frontend/wallet/shelley/shelley-ledger-crypto-provider.ts
Expand Up @@ -454,11 +454,9 @@ const ShelleyLedgerCryptoProvider = async ({
params: {
votingPublicKeyHex: txAuxiliaryData.votingPubKey,
stakingPath: txAuxiliaryData.rewardDestinationAddress.stakingPath,
//addressToAbsPathMapper(txAuxiliaryData.stakePubKey),
rewardsDestination: {
type: LedgerTypes.AddressType.BASE,
type: LedgerTypes.AddressType.REWARD,
params: {
spendingPath: txAuxiliaryData.rewardDestinationAddress.spendingPath,
stakingPath: txAuxiliaryData.rewardDestinationAddress.stakingPath,
},
},
Expand Down Expand Up @@ -516,7 +514,7 @@ const ShelleyLedgerCryptoProvider = async ({
const validityIntervalStart = txAux.validityIntervalStart
? `${txAux.validityIntervalStart}`
: null
const votingAuxiliaryData = txAux.auxiliaryData
const formattedAuxiliaryData = txAux.auxiliaryData
? formatAuxiliaryAdata(txAux.auxiliaryData)
: null
const response = await ledger.signTransaction({
Expand All @@ -529,7 +527,7 @@ const ShelleyLedgerCryptoProvider = async ({
ttl: ttlStr,
certificates,
withdrawals,
auxiliaryData: votingAuxiliaryData,
auxiliaryData: formattedAuxiliaryData,
validityIntervalStart,
},
})
Expand Down
6 changes: 2 additions & 4 deletions app/frontend/wallet/shelley/shelley-transaction-planner.ts
Expand Up @@ -544,15 +544,13 @@ const prepareTxPlanDraft = (txPlanArgs: TxPlanArgs): TxPlanDraft => {
}

const prepareVotingRegistrationTx = (txPlanArgs: VotingRegistrationTxPlanArgs): TxPlanDraft => {
const {votingPubKey, stakePubKey, rewardDestinationAddress, nonce} = txPlanArgs
const {votingPubKey, stakePubKey, nonce, stakingAddress} = txPlanArgs
const auxiliaryData: TxAuxiliaryData = {
type: 'CATALYST_VOTING',
votingPubKey,
stakePubKey,
rewardDestinationAddress: {
//TODO: refactor type
...rewardDestinationAddress,
spendingPath: null,
address: stakingAddress,
stakingPath: null,
},
nonce,
Expand Down
2 changes: 1 addition & 1 deletion app/frontend/wallet/shelley/shelley-transaction.ts
Expand Up @@ -61,7 +61,7 @@ function ShelleyTxAux({
ttl: number
certificates: TxCertificate[]
withdrawals: TxWithdrawal[]
auxiliaryDataHash: string
auxiliaryDataHash: HexString
auxiliaryData: TxAuxiliaryData
validityIntervalStart: number
}): TxAux {
Expand Down
35 changes: 20 additions & 15 deletions app/frontend/wallet/shelley/shelley-trezor-crypto-provider.ts
Expand Up @@ -309,18 +309,23 @@ const ShelleyTrezorCryptoProvider = async ({
}

// TODO: export type from trezor if possible
const formatVotingAuxiliaryAdata = (txAuxiliaryData: TxAuxiliaryData): any => {
return {
catalystRegistrationParameters: {
votingPublicKey: txAuxiliaryData.votingPubKey,
stakingPath: txAuxiliaryData.rewardDestinationAddress.stakingPath,
rewardAddressParameters: {
addressType: 0, // base address type TODO
path: txAuxiliaryData.rewardDestinationAddress.spendingPath,
stakingPath: txAuxiliaryData.rewardDestinationAddress.stakingPath,
},
nonce: Number(txAuxiliaryData.nonce),
},
const formatAuxiliaryAdata = (txAuxiliaryData: TxAuxiliaryData): any => {
switch (txAuxiliaryData.type) {
case 'CATALYST_VOTING':
return {
catalystRegistrationParameters: {
votingPublicKey: txAuxiliaryData.votingPubKey,
stakingPath: txAuxiliaryData.rewardDestinationAddress.stakingPath,
rewardAddressParameters: {
addressType: AddressTypes.REWARD,
path: txAuxiliaryData.rewardDestinationAddress.stakingPath, //TODO: del, avoids error
stakingPath: txAuxiliaryData.rewardDestinationAddress.stakingPath,
},
nonce: `${txAuxiliaryData.nonce}`,
},
}
default:
return assertUnreachable(txAuxiliaryData.type)
}
}

Expand Down Expand Up @@ -369,8 +374,8 @@ const ShelleyTrezorCryptoProvider = async ({
const validityIntervalStart = txAux.validityIntervalStart
? `${txAux.validityIntervalStart}`
: null
const votingAuxiliaryData = txAux.auxiliaryData
? formatVotingAuxiliaryAdata(txAux.auxiliaryData)
const formattedAuxiliaryData = txAux.auxiliaryData
? formatAuxiliaryAdata(txAux.auxiliaryData)
: null
const response: TrezorSignTxResponse = await TrezorConnect.cardanoSignTransaction(
removeNullFields({
Expand All @@ -382,7 +387,7 @@ const ShelleyTrezorCryptoProvider = async ({
networkId: network.networkId,
certificates,
withdrawals,
auxiliaryData: votingAuxiliaryData,
auxiliaryData: formattedAuxiliaryData,
validityIntervalStart,
})
)
Expand Down
2 changes: 1 addition & 1 deletion app/frontend/wallet/shelley/types.ts
Expand Up @@ -13,7 +13,7 @@ export type TxAux = {
certificates: TxCertificate[]
withdrawals: TxWithdrawal[]
auxiliaryData: TxAuxiliaryData
auxiliaryDataHash: string
auxiliaryDataHash: HexString
validityIntervalStart: number
encodeCBOR: encodeCBORFn
}
Expand Down
5 changes: 3 additions & 2 deletions app/frontend/wallet/types.ts
Expand Up @@ -95,14 +95,15 @@ export type TxWithdrawal = {

export type TxAuxiliaryDataTypes = 'CATALYST_VOTING'

export type TxAuxiliaryData = {
export type TxAuxiliaryData = TxVotingAuxiliaryData

export type TxVotingAuxiliaryData = {
type: TxAuxiliaryDataTypes
votingPubKey: string
stakePubKey: HexString
nonce: BigInt
rewardDestinationAddress: {
address: Address
spendingPath: BIP32Path
stakingPath: BIP32Path
}
}
Expand Down
2 changes: 1 addition & 1 deletion app/package.json
Expand Up @@ -18,7 +18,7 @@
"homepage": "https://github.com/vacuumlabs/adalite#readme",
"scripts": {},
"dependencies": {
"@cardano-foundation/ledgerjs-hw-app-cardano": "https://github.com/vacuumlabs/ledgerjs-cardano-shelley/releases/download/v3.1.0-catalyst-rc.4/cardano-foundation-ledgerjs-hw-app-cardano-v3.1.0-catalyst-rc.4.tgz",
"@cardano-foundation/ledgerjs-hw-app-cardano": "https://github.com/vacuumlabs/ledgerjs-cardano-shelley/releases/download/v3.1.0-catalyst-rc.5/cardano-foundation-ledgerjs-hw-app-cardano-v3.1.0-catalyst-rc.5.tgz",
"@ledgerhq/hw-transport": "^5.43.0",
"@ledgerhq/hw-transport-u2f": "^5.36.0-deprecated",
"@ledgerhq/hw-transport-webusb": "^5.43.0",
Expand Down
2 changes: 1 addition & 1 deletion app/tests/src/actions/transaction-actions.js
Expand Up @@ -101,7 +101,7 @@ const votingSettings = {
state: {},
votingPubKey: '2145823c77df07a43210af5422e6447bb4d1f44f1af81a261205146cc67d2cf0',
sendTransactionSummary: {
fee: 181002,
fee: 179772,
},
},
}
Expand Down
17 changes: 6 additions & 11 deletions app/tests/src/common/tx-settings.js
Expand Up @@ -247,10 +247,7 @@ const transactionSettings = {
txType: TxType.REGISTER_VOTING,
votingPubKey: '2145823c77df07a43210af5422e6447bb4d1f44f1af81a261205146cc67d2cf0',
stakePubKey: '2ef8d7c9e19bb688860a900123e5bbe2eff7187336590b3928d43a830110cd62',
rewardDestinationAddress: {
address:
'addr1q8eakg39wqlye7lzyfmh900s2luc99zf7x9vs839pn4srjs2s3ps2plp2rc2qcgfmsa8kx2kk7s9s6hfq799tmcwpvpsjv0zk3',
},
stakingAddress: 'stake1uy9ggsc9qls4pu9qvyyacwnmr9tt0gzcdt5s0zj4au8qkqc65geks',
nonce: BigInt(25000000),
},
txPlanResult: {
Expand All @@ -271,22 +268,20 @@ const transactionSettings = {
isChange: false,
address:
'addr1q8eakg39wqlye7lzyfmh900s2luc99zf7x9vs839pn4srjs2s3ps2plp2rc2qcgfmsa8kx2kk7s9s6hfq799tmcwpvpsjv0zk3',
coins: 9818998,
coins: 9820228,
tokenBundle: inputTokens,
},
certificates: [],
deposit: 0,
fee: 181002,
baseFee: 181002,
fee: 179772,
baseFee: 179772,
additionalLovelaceAmount: 0,
withdrawals: [],
auxiliaryData: {
type: 'CATALYST_VOTING',
nonce: BigInt(25000000),
rewardDestinationAddress: {
address:
'addr1q8eakg39wqlye7lzyfmh900s2luc99zf7x9vs839pn4srjs2s3ps2plp2rc2qcgfmsa8kx2kk7s9s6hfq799tmcwpvpsjv0zk3',
spendingPath: null,
address: 'stake1uy9ggsc9qls4pu9qvyyacwnmr9tt0gzcdt5s0zj4au8qkqc65geks',
stakingPath: null,
},
stakePubKey: '2ef8d7c9e19bb688860a900123e5bbe2eff7187336590b3928d43a830110cd62',
Expand All @@ -295,7 +290,7 @@ const transactionSettings = {
},
},
ttl,
txHash: '8311efe33e4033b1a630b70dbf22525ea6f9ecbffa37d5420c29148ba7fef0f6',
txHash: '009d1b3a4b1ae81719322fa02149cd126a2f348950424014cbae0c99ea17fcac',
},
}

Expand Down
6 changes: 3 additions & 3 deletions app/yarn.lock
Expand Up @@ -38,9 +38,9 @@
dependencies:
regenerator-runtime "^0.13.4"

"@cardano-foundation/ledgerjs-hw-app-cardano@https://github.com/vacuumlabs/ledgerjs-cardano-shelley/releases/download/v3.1.0-catalyst-rc.4/cardano-foundation-ledgerjs-hw-app-cardano-v3.1.0-catalyst-rc.4.tgz":
version "3.1.0-catalyst-rc.4"
resolved "https://github.com/vacuumlabs/ledgerjs-cardano-shelley/releases/download/v3.1.0-catalyst-rc.4/cardano-foundation-ledgerjs-hw-app-cardano-v3.1.0-catalyst-rc.4.tgz#feeaeb68d376ecad27bda40a475d1ef488cffac7"
"@cardano-foundation/ledgerjs-hw-app-cardano@https://github.com/vacuumlabs/ledgerjs-cardano-shelley/releases/download/v3.1.0-catalyst-rc.5/cardano-foundation-ledgerjs-hw-app-cardano-v3.1.0-catalyst-rc.5.tgz":
version "3.1.0-catalyst-rc.5"
resolved "https://github.com/vacuumlabs/ledgerjs-cardano-shelley/releases/download/v3.1.0-catalyst-rc.5/cardano-foundation-ledgerjs-hw-app-cardano-v3.1.0-catalyst-rc.5.tgz#a69b6cc9e987e98d3cee301ff3ab48f2ec2a75ad"
dependencies:
"@ledgerhq/hw-transport" "^5.12.0"
"@types/ledgerhq__hw-transport" "^4.21.3"
Expand Down

0 comments on commit 52657f5

Please sign in to comment.