Skip to content

Commit

Permalink
fix: review
Browse files Browse the repository at this point in the history
  • Loading branch information
janmazak committed Nov 30, 2022
1 parent 74d3986 commit 25b18bf
Show file tree
Hide file tree
Showing 8 changed files with 30 additions and 104 deletions.
16 changes: 9 additions & 7 deletions src/errors/invalidDataReason.ts
Expand Up @@ -132,19 +132,21 @@ export enum InvalidDataReason {

METADATA_UNKNOWN_TYPE = "unknown metadata type",

GOVERNANCE_VOTING_REGISTRATION_INCONSISTENT_WITH_CIP15 = "governance voting registration params incosistent with CIP-15",
GOVERNANCE_VOTING_REGISTRATION_INCONSISTENT_WITH_CIP36 = "governance voting registration params incosistent with CIP-36",
GOVERNANCE_VOTING_REGISTRATION_INCONSISTENT_WITH_CIP15 = "governance voting registration params inconsistent with CIP-15",
GOVERNANCE_VOTING_REGISTRATION_INCONSISTENT_WITH_CIP36 = "governance voting registration params inconsistent with CIP-36",
GOVERNANCE_VOTING_REGISTRATION_BOTH_KEY_AND_PATH = "governance voting key given both as a key and as a derivation path",
GOVERNANCE_VOTING_REGISTRATION_INVALID_VOTING_KEY = "invalid goverance voting registration voting key",
GOVERNANCE_VOTING_REGISTRATION_INVALID_VOTING_KEY_PATH = "invalid goverance voting registration voting key path",
GOVERNANCE_VOTING_REGISTRATION_MISSING_VOTING_KEY = "governance voting key missing",
GOVERNANCE_VOTING_REGISTRATION_INVALID_VOTING_KEY = "invalid governance voting registration voting key",
GOVERNANCE_VOTING_REGISTRATION_INVALID_VOTING_KEY_PATH = "invalid governance voting registration voting key path",
GOVERNANCE_VOTING_REGISTRATION_INVALID_STAKING_KEY_PATH = "invalid governance voting registration staking key path",
GOVERNANCE_VOTING_REGISTRATION_INVALID_NONCE = "invalid governance voting registration nonce",
GOVERNANCE_VOTING_REGISTRATION_INVALID_VOTING_PURPOSE = "invalid governance voting registration voting purpose",
GOVERNANCE_VOTING_REGISTRATION_DELEGATIONS_NOT_ARRAY = "governance voting registration delegations not an array",
GOVERNANCE_VOTING_DELEGATION_UNKNOWN_FORMAT = "invalid governance voting delegation format",
GOVERNANCE_VOTING_DELEGATION_UNKNOWN_DELEGATION_TYPE = "invalid governance voting delegation type",
GOVERNANCE_VOTING_DELEGATION_INVALID_WEIGHT = "invalid governance voting delegation weight",
GOVERNANCE_VOTING_DELEGATION_INVALID_PATH = "invalid governance voting delegation path",
GOVERNANCE_VOTING_DELEGATION_INVALID_KEY = "invalid governance voting delegation key",
GOVERNANCE_VOTING_DELEGATION_UNKNOWN_TYPE = "invalid governance voting delegation type",

VALIDITY_INTERVAL_START_INVALID = "invalid validity interval start",

Expand Down Expand Up @@ -249,9 +251,9 @@ export enum InvalidDataReason {
SIGN_MODE_POOL_OPERATOR__REQUIRED_SIGNERS_NOT_ALLOWED =
"required signers not allowed in TransactionSigningMode.POOL_REGISTRATION_AS_OPERATOR",
SIGN_MODE_POOL_OPERATOR__DATUM_NOT_ALLOWED =
"datum in ouputs not allowed in TransactionSigningMode.POOL_REGISTRATION_AS_OPERATOR",
"datum in outputs not allowed in TransactionSigningMode.POOL_REGISTRATION_AS_OPERATOR",
SIGN_MODE_POOL_OPERATOR__REFERENCE_SCRIPT_NOT_ALLOWED =
"reference script in ouputs not allowed in TransactionSigningMode.POOL_REGISTRATION_AS_OPERATOR",
"reference script in outputs not allowed in TransactionSigningMode.POOL_REGISTRATION_AS_OPERATOR",
SIGN_MODE_POOL_OPERATOR__COLLATERAL_OUTPUT_NOT_ALLOWED =
"collateral output not allowed in TransactionSigningMode.POOL_REGISTRATION_AS_OPERATOR",
SIGN_MODE_POOL_OPERATOR__TOTAL_COLLATERAL_NOT_ALLOWED =
Expand Down
10 changes: 5 additions & 5 deletions src/interactions/serialization/governanceVotingRegistration.ts
Expand Up @@ -24,7 +24,7 @@ export function serializeGovernanceVotingRegistrationInit(params: ParsedGovernan
])
}

function delegationTypeBuffer(type: GovernanceVotingDelegationType): Buffer {
function serializeDelegationType(type: GovernanceVotingDelegationType): Buffer {
const delegationTypeEncoding = {
[GovernanceVotingDelegationType.KEY]: 0x01,
[GovernanceVotingDelegationType.PATH]: 0x02,
Expand All @@ -39,20 +39,20 @@ export function serializeGovernanceVotingRegistrationVotingKey(
if (votingPublicKey != null) {
assert(votingPublicKeyPath == null, "redundant governance registration voting key path")
return Buffer.concat([
delegationTypeBuffer(GovernanceVotingDelegationType.KEY),
serializeDelegationType(GovernanceVotingDelegationType.KEY),
hex_to_buf(votingPublicKey),
])
} else {
assert(votingPublicKeyPath != null, "missing governance registration voting key")
return Buffer.concat([
delegationTypeBuffer(GovernanceVotingDelegationType.PATH),
path_to_buf(votingPublicKeyPath!),
serializeDelegationType(GovernanceVotingDelegationType.PATH),
path_to_buf(votingPublicKeyPath),
])
}
}

export function serializeGovernanceVotingRegistrationDelegation(delegation: ParsedGovernanceVotingDelegation): Buffer {
const typeBuffer = delegationTypeBuffer(delegation.type)
const typeBuffer = serializeDelegationType(delegation.type)

const weightBuffer = uint32_to_buf(delegation.weight)

Expand Down
3 changes: 1 addition & 2 deletions src/interactions/signTx.ts
Expand Up @@ -29,7 +29,6 @@ import {
import type {
SignedTransactionData,
TxAuxiliaryDataSupplement} from "../types/public"
import {} from "../types/public"
import {
AddressType,
DatumType,
Expand Down Expand Up @@ -505,7 +504,7 @@ function* signTx_setAuxiliaryData(
const enum P2 {
INIT = 0x36,
VOTING_KEY = 0x30,
DELEGATION = 0x37, // TODO
DELEGATION = 0x37,
STAKING_KEY = 0x31,
VOTING_REWARDS_ADDRESS = 0x32,
NONCE = 0x33,
Expand Down
11 changes: 8 additions & 3 deletions src/parsing/transaction.ts
Expand Up @@ -281,10 +281,15 @@ function parseWithdrawal(params: Withdrawal): ParsedWithdrawal {
}
}

/*
* Typically, destination is used in output, where we forbid reward addresses.
* In some other places, we allow them. The parameter 'validateAsTxOutput'
* is used to control this validation.
*/
export function parseTxDestination(
network: Network,
destination: TxOutputDestination,
validateAsTxOutput: boolean = false,
validateAsTxOutput: boolean,
): ParsedOutputDestination {
switch (destination.type) {
case TxOutputDestinationType.THIRD_PARTY: {
Expand All @@ -301,7 +306,7 @@ export function parseTxDestination(
const addressParams = parseAddress(network, params)
if (validateAsTxOutput) {
validate(
// a reward adress cannot be used in tx output
// a reward address cannot be used in tx output
addressParams.spendingDataSource.type === SpendingDataSourceType.PATH,
InvalidDataReason.OUTPUT_INVALID_ADDRESS_PARAMS
)
Expand Down Expand Up @@ -509,7 +514,7 @@ export function parseSignTransactionRequest(request: SignTransactionRequest): Pa
}

case TransactionSigningMode.POOL_REGISTRATION_AS_OWNER: {
// all these restictions are due to fact that pool owner signature *might* accidentally/maliciously sign another part of tx
// all these restrictions are due to fact that pool owner signature *might* accidentally/maliciously sign another part of tx
// but we are not showing these parts to the user

// input should not be given with a path
Expand Down
8 changes: 4 additions & 4 deletions src/parsing/txAuxiliaryData.ts
Expand Up @@ -6,7 +6,6 @@ import { AUXILIARY_DATA_HASH_LENGTH } from "../types/internal"
import type { GovernanceVotingDelegation, GovernanceVotingRegistrationParams, Network,TxAuxiliaryData } from "../types/public"
import { GovernanceVotingDelegationType, GovernanceVotingRegistrationFormat } from "../types/public"
import { TxAuxiliaryDataType } from "../types/public"
import { unreachable } from "../utils/assert"
import { isArray, parseBIP32Path, parseHexStringOfLength, parseUint32_t, parseUint64_str } from "../utils/parse"
import { validate } from "../utils/parse"
import { parseTxDestination } from "./transaction"
Expand Down Expand Up @@ -49,7 +48,7 @@ function parseGovernanceVotingDelegation(delegation: GovernanceVotingDelegation)
weight,
}
default:
throw new InvalidData(InvalidDataReason.GOVERNANCE_VOTING_DELEGATION_UNKNOWN_TYPE)
throw new InvalidData(InvalidDataReason.GOVERNANCE_VOTING_DELEGATION_UNKNOWN_DELEGATION_TYPE)
}
}

Expand All @@ -73,11 +72,12 @@ function parseGovernanceVotingRegistrationParams(network: Network, params: Gover
} else {
validate(params.delegations == null, InvalidDataReason.GOVERNANCE_VOTING_REGISTRATION_INCONSISTENT_WITH_CIP36)
validate(params.votingPublicKeyHex == null || params.votingPublicKeyPath == null, InvalidDataReason.GOVERNANCE_VOTING_REGISTRATION_BOTH_KEY_AND_PATH)
validate(params.votingPublicKeyHex != null || params.votingPublicKeyPath != null, InvalidDataReason.GOVERNANCE_VOTING_REGISTRATION_MISSING_VOTING_KEY)
}
break

default:
unreachable(params.format)
throw new InvalidData(InvalidDataReason.GOVERNANCE_VOTING_DELEGATION_UNKNOWN_FORMAT)
}

const votingPublicKey = params.votingPublicKeyHex == null
Expand All @@ -102,7 +102,7 @@ function parseGovernanceVotingRegistrationParams(network: Network, params: Gover
votingPublicKeyPath,
delegations,
stakingPath: parseBIP32Path(params.stakingPath, InvalidDataReason.GOVERNANCE_VOTING_REGISTRATION_INVALID_STAKING_KEY_PATH),
rewardsDestination: parseTxDestination(network, params.rewardsDestination),
rewardsDestination: parseTxDestination(network, params.rewardsDestination, false),
nonce: parseUint64_str(params.nonce, {}, InvalidDataReason.GOVERNANCE_VOTING_REGISTRATION_INVALID_NONCE),
votingPurpose,
}
Expand Down
79 changes: 1 addition & 78 deletions test/integration/__fixtures__/signTx.ts
@@ -1,11 +1,9 @@
import type { Transaction } from "../../../src/Ada"
import { TxAuxiliaryDataSupplementType } from "../../../src/Ada"
import { CertificateType, Networks, TxAuxiliaryDataType } from "../../../src/Ada"
import type { BIP32Path, SignedTransactionData} from '../../../src/types/public'
import { GovernanceVotingRegistrationFormat} from '../../../src/types/public'
import { StakeCredentialParamsType, TransactionSigningMode } from '../../../src/types/public'
import { str_to_path } from "../../../src/utils/address"
import { destinations, inputs, mainnetFeeTtl, mints, outputs, shelleyBase, testnetFeeTtl } from "./txElements"
import { inputs, mainnetFeeTtl, mints, outputs, shelleyBase, testnetFeeTtl } from "./txElements"

export type SignTxTestcase = {
testname: string;
Expand Down Expand Up @@ -977,81 +975,6 @@ export const testsMary: SignTxTestcase[] = [
]


export const testsCatalystRegistration: SignTxTestcase[] = [
{
testname: "Sign tx with Catalyst voting key registration metadata with base address",
tx: {
...mainnetFeeTtl,
inputs: [inputs.utxoShelley],
outputs: [outputs.internalBaseWithStakingPath],
validityIntervalStart: 7,
auxiliaryData: {
type: TxAuxiliaryDataType.GOVERNANCE_VOTING_REGISTRATION,
params: {
format: GovernanceVotingRegistrationFormat.CIP_15,
votingPublicKeyHex: "4b19e27ffc006ace16592311c4d2f0cafc255eaa47a6178ff540c0a46d07027c",
stakingPath: str_to_path("1852'/1815'/0'/2/0"),
nonce: 1454448,
rewardsDestination: destinations.internalBaseWithStakingPath,
},
},
},
signingMode: TransactionSigningMode.ORDINARY_TRANSACTION,
txBody: "a600818258203b40265111d8bb3c3c608d95b3a0bf83461ace32d79336579a1939b3aad1c0b70001818258390114c16d7f43243bd81478e68b9db53a8528fd4fb1078d58d54a7f11241d227aefa4b773149170885aadba30aab3127cc611ddbc4999def61c1a006ca79302182a030a075820e9141b460aea0abb69ce113c7302c7c03690267736d6a382ee62d2a53c2ec9260807",
txAuxiliaryData: "82a219ef64a40158204b19e27ffc006ace16592311c4d2f0cafc255eaa47a6178ff540c0a46d07027c02582066610efd336e1137c525937b76511fbcf2a0e6bcf0d340a67bcb39bc870d85e80358390114c16d7f43243bd81478e68b9db53a8528fd4fb1078d58d54a7f11241d227aefa4b773149170885aadba30aab3127cc611ddbc4999def61c041a0016317019ef65a10158400ca3bb69cad5f471ddd32097a8501e3956e4ae0c2bf523625d1686b123dcc04af240630eb93bf1069c607b59bbe7d521fb8dd14a4312788bc0b72b7473ee160e80",
expectedResult: {
txHashHex: "9941060a76f5702e72b43c382f77b143ed0e328ac3977a0791f08a5f0e0149cd",
witnesses: [
{
path: str_to_path("1852'/1815'/0'/0/0"),
witnessSignatureHex: "b0bc6b3ddc0ab65e5b2e83cfdedbbf76619c3a833705f634f1c8c335dc7c1c5372ec7ebb8199d6d18204da4a0168a172c41c6dd53f45235225f5e62b672ca709",
},
],
auxiliaryDataSupplement: {
type: TxAuxiliaryDataSupplementType.GOVERNANCE_VOTING_REGISTRATION,
auxiliaryDataHashHex: "e9141b460aea0abb69ce113c7302c7c03690267736d6a382ee62d2a53c2ec926",
governanceVotingRegistrationSignatureHex: "0ca3bb69cad5f471ddd32097a8501e3956e4ae0c2bf523625d1686b123dcc04af240630eb93bf1069c607b59bbe7d521fb8dd14a4312788bc0b72b7473ee160e",
},
},
},
{
testname: "Sign tx with Catalyst voting key registration metadata with stake address",
tx: {
...mainnetFeeTtl,
inputs: [inputs.utxoShelley],
outputs: [outputs.internalBaseWithStakingPath],
auxiliaryData: {
type: TxAuxiliaryDataType.GOVERNANCE_VOTING_REGISTRATION,
params: {
format: GovernanceVotingRegistrationFormat.CIP_15,
votingPublicKeyHex: "4b19e27ffc006ace16592311c4d2f0cafc255eaa47a6178ff540c0a46d07027c",
stakingPath: str_to_path("1852'/1815'/0'/2/0"),
nonce: 1454448,
rewardsDestination: destinations.rewardsKeyPath,
},
},
},
signingMode: TransactionSigningMode.ORDINARY_TRANSACTION,
txBody: "a500818258203b40265111d8bb3c3c608d95b3a0bf83461ace32d79336579a1939b3aad1c0b70001818258390114c16d7f43243bd81478e68b9db53a8528fd4fb1078d58d54a7f11241d227aefa4b773149170885aadba30aab3127cc611ddbc4999def61c1a006ca79302182a030a075820d19f7cb4d48a6ae8d370c64d2a42fca1f61d6b2cf3d0c0c02801541811338deb",
txAuxiliaryData: "82a219ef64a40158204b19e27ffc006ace16592311c4d2f0cafc255eaa47a6178ff540c0a46d07027c02582066610efd336e1137c525937b76511fbcf2a0e6bcf0d340a67bcb39bc870d85e803581de11d227aefa4b773149170885aadba30aab3127cc611ddbc4999def61c041a0016317019ef65a10158401514b6bbc582b33edcf5fa30ec04dcaa62128de8755c786768ae5922132c2aa50b9ba17be28072de979f45b0f429c7f5d489c549a1e22bc8e7d0b2445c10360980",
expectedResult: {
txHashHex: "90ab18ad3a25cb9f48470cb16a51e1fe04181b96f639d939c51ca81ab4c0fa23",
witnesses: [
{
path: str_to_path("1852'/1815'/0'/0/0"),
witnessSignatureHex: "138afde8640bd8d1a08309455f604d842d65a85e5ce2f584974f004e9043dea670ead5de3e4895a320f94033d5476d56ccf7147f327156cc30aef8304c66c006",
},
],
auxiliaryDataSupplement: {
type: TxAuxiliaryDataSupplementType.GOVERNANCE_VOTING_REGISTRATION,
auxiliaryDataHashHex: "d19f7cb4d48a6ae8d370c64d2a42fca1f61d6b2cf3d0c0c02801541811338deb",
governanceVotingRegistrationSignatureHex: "1514b6bbc582b33edcf5fa30ec04dcaa62128de8755c786768ae5922132c2aa50b9ba17be28072de979f45b0f429c7f5d489c549a1e22bc8e7d0b2445c103609",
},
},
},
]


export const testsAlonzoTrezorComparison: SignTxTestcase[] = [
{
testname: "Full test for trezor feature parity",
Expand Down
5 changes: 1 addition & 4 deletions test/integration/signTxGovernance.test.ts
@@ -1,8 +1,5 @@
import { describeSignTxPositiveTest,describeSignTxRejects } from "../test_utils"
import {
testsCatalystRegistration,
} from "./__fixtures__/signTx"
import { testsGovernanceVotingRegistrationCIP36, testsGovernanceVotingRegistrationRejects } from "./__fixtures__/signTxGovernance"
import { testsCatalystRegistration, testsGovernanceVotingRegistrationCIP36, testsGovernanceVotingRegistrationRejects } from "./__fixtures__/signTxGovernance"

describeSignTxPositiveTest("signTxCatalyst", testsCatalystRegistration)
describeSignTxPositiveTest("signTxGovernanceVotingRegistrationCIP36", testsGovernanceVotingRegistrationCIP36)
Expand Down
2 changes: 1 addition & 1 deletion test/test_utils.ts
Expand Up @@ -59,7 +59,7 @@ export function describeWithoutValidation(title: string, test: () => void) {

const ProtocolMagics = {
MAINNET: 764824073,
TESTNET: 42, // never used in an actual testnet, just for our integration tests
TESTNET: 42, // used in our integration tests
TESTNET_LEGACY: 1097911063,
TESTNET_PREPROD: 1,
TESTNET_PREVIEW: 2,
Expand Down

0 comments on commit 25b18bf

Please sign in to comment.