Skip to content

Commit

Permalink
[SQUASH] review comments for address derivation
Browse files Browse the repository at this point in the history
  • Loading branch information
mkv-vcm authored and janmazak committed Jul 28, 2021
1 parent 2509ac1 commit 33a2493
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 64 deletions.
30 changes: 15 additions & 15 deletions src/interactions/serialization/addressParams.ts
Original file line number Diff line number Diff line change
@@ -1,47 +1,47 @@
import { ParsedAddressParams, SpendingChoice, SpendingChoiceType, StakingChoice, Uint8_t } from "../../types/internal"
import { AddressType, StakingChoiceType } from "../../types/internal"
import { ParsedAddressParams, SpendingChoice, SpendingDataSourceType, StakingChoice, Uint8_t } from "../../types/internal"
import { AddressType, StakingDataSource } from "../../types/internal"
import { hex_to_buf, path_to_buf, uint8_to_buf, uint32_to_buf } from "../../utils/serialize"

function serializeSpendingChoice(spendingChoice: SpendingChoice): Buffer {
switch (spendingChoice.type) {
case SpendingChoiceType.PATH:
case SpendingDataSourceType.PATH:
return path_to_buf(spendingChoice.path)
case SpendingChoiceType.SCRIPT_HASH:
case SpendingDataSourceType.SCRIPT_HASH:
return hex_to_buf(spendingChoice.scriptHash)
case SpendingChoiceType.NONE:
case SpendingDataSourceType.NONE:
return Buffer.alloc(0)
}
}

function serializeStakingChoice(stakingChoice: StakingChoice): Buffer {
const stakingChoicesEncoding = {
[StakingChoiceType.NO_STAKING]: 0x11,
[StakingChoiceType.STAKING_KEY_PATH]: 0x22,
[StakingChoiceType.STAKING_KEY_HASH]: 0x33,
[StakingChoiceType.BLOCKCHAIN_POINTER]: 0x44,
[StakingChoiceType.STAKING_SCRIPT_HASH]: 0x55,
[StakingDataSource.NONE]: 0x11,
[StakingDataSource.KEY_PATH]: 0x22,
[StakingDataSource.KEY_HASH]: 0x33,
[StakingDataSource.BLOCKCHAIN_POINTER]: 0x44,
[StakingDataSource.SCRIPT_HASH]: 0x55,
} as const

switch (stakingChoice.type) {
case StakingChoiceType.NO_STAKING: {
case StakingDataSource.NONE: {
return Buffer.concat([
uint8_to_buf(stakingChoicesEncoding[stakingChoice.type] as Uint8_t),
])
}
case StakingChoiceType.STAKING_KEY_HASH:
case StakingChoiceType.STAKING_SCRIPT_HASH: {
case StakingDataSource.KEY_HASH:
case StakingDataSource.SCRIPT_HASH: {
return Buffer.concat([
uint8_to_buf(stakingChoicesEncoding[stakingChoice.type] as Uint8_t),
hex_to_buf(stakingChoice.hashHex),
])
}
case StakingChoiceType.STAKING_KEY_PATH: {
case StakingDataSource.KEY_PATH: {
return Buffer.concat([
uint8_to_buf(stakingChoicesEncoding[stakingChoice.type] as Uint8_t),
path_to_buf(stakingChoice.path),
])
}
case StakingChoiceType.BLOCKCHAIN_POINTER: {
case StakingDataSource.BLOCKCHAIN_POINTER: {
return Buffer.concat([
uint8_to_buf(stakingChoicesEncoding[stakingChoice.type] as Uint8_t),
uint32_to_buf(stakingChoice.pointer.blockIndex),
Expand Down
2 changes: 1 addition & 1 deletion src/interactions/serialization/txInit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const _serializeSigningMode = (
[TransactionSigningMode.ORDINARY_TRANSACTION]: 3 as Uint8_t,
[TransactionSigningMode.POOL_REGISTRATION_AS_OWNER]: 4 as Uint8_t,
[TransactionSigningMode.POOL_REGISTRATION_AS_OPERATOR]: 5 as Uint8_t,
[TransactionSigningMode.MULTISIGN_TRANSACTION]: 6 as Uint8_t, //TODO not sure if it's part of some specification
[TransactionSigningMode.MULTISIG_TRANSACTION]: 6 as Uint8_t,
}[mode]

assert(value !== undefined, 'Invalid signing mode')
Expand Down
18 changes: 9 additions & 9 deletions src/parsing/address.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { InvalidData } from "../errors"
import { InvalidDataReason } from "../errors/invalidDataReason"
import type { ParsedAddressParams } from "../types/internal"
import { AddressType, KEY_HASH_LENGTH, SCRIPT_HASH_LENGTH, SpendingChoice,
StakingChoice, SpendingChoiceType, StakingChoiceType } from "../types/internal"
StakingChoice, SpendingDataSourceType, StakingDataSource } from "../types/internal"
import type { BIP32Path, BlockchainPointer, DeviceOwnedAddress, Network } from "../types/public"
import { parseBIP32Path, parseHexStringOfLength, parseUint32_t, validate } from "../utils/parse"
import { parseNetwork } from "./network"
Expand All @@ -21,7 +21,7 @@ function extractSpendingChoice(
validate(spendingPath != null, InvalidDataReason.ADDRESS_INVALID_SPENDING_KEY_PATH)
validate(spendingScriptHash == null, InvalidDataReason.ADDRESS_INVALID_SPENDING_SCRIPT_HASH)
return {
type: SpendingChoiceType.PATH,
type: SpendingDataSourceType.PATH,
path: parseBIP32Path(spendingPath, InvalidDataReason.ADDRESS_INVALID_SPENDING_KEY_PATH),
}
case AddressType.BASE_PAYMENT_SCRIPT_STAKE_KEY:
Expand All @@ -31,15 +31,15 @@ function extractSpendingChoice(
validate(spendingPath == null, InvalidDataReason.ADDRESS_INVALID_SPENDING_KEY_PATH)
validate(spendingScriptHash != null, InvalidDataReason.ADDRESS_INVALID_SPENDING_SCRIPT_HASH)
return {
type: SpendingChoiceType.SCRIPT_HASH,
type: SpendingDataSourceType.SCRIPT_HASH,
scriptHash: parseHexStringOfLength(spendingScriptHash, SCRIPT_HASH_LENGTH, InvalidDataReason.ADDRESS_INVALID_SPENDING_SCRIPT_HASH),
}
case AddressType.REWARD_KEY:
case AddressType.REWARD_SCRIPT:
validate(spendingPath == null, InvalidDataReason.ADDRESS_INVALID_SPENDING_KEY_PATH)
validate(spendingScriptHash == null, InvalidDataReason.ADDRESS_INVALID_SPENDING_SCRIPT_HASH)
return {
type: SpendingChoiceType.NONE,
type: SpendingDataSourceType.NONE,
}
break;
default:
Expand All @@ -66,13 +66,13 @@ function extractStakingChoice(
if (stakingHashPresent) {
const hashHex = parseHexStringOfLength(stakingKeyHashHex!, KEY_HASH_LENGTH, InvalidDataReason.ADDRESS_INVALID_STAKING_KEY_HASH)
return {
type: StakingChoiceType.STAKING_KEY_HASH,
type: StakingDataSource.KEY_HASH,
hashHex,
}
}
const codedStakingPath = parseBIP32Path(stakingPath, InvalidDataReason.ADDRESS_INVALID_SPENDING_KEY_PATH)
return {
type: StakingChoiceType.STAKING_KEY_PATH,
type: StakingDataSource.KEY_PATH,
path: codedStakingPath,
}
case AddressType.BASE_PAYMENT_SCRIPT_STAKE_SCRIPT:
Expand All @@ -84,7 +84,7 @@ function extractStakingChoice(
validate(stakingScriptHash != null, InvalidDataReason.ADDRESS_INVALID_STAKING_INFO)
const stakingHash = parseHexStringOfLength(stakingScriptHash, SCRIPT_HASH_LENGTH, InvalidDataReason.ADDRESS_INVALID_STAKING_SCRIPT_HASH)
return {
type: StakingChoiceType.STAKING_SCRIPT_HASH,
type: StakingDataSource.SCRIPT_HASH,
hashHex: stakingHash,
}
case AddressType.POINTER_KEY:
Expand All @@ -95,7 +95,7 @@ function extractStakingChoice(
validate(stakingScriptHash == null, InvalidDataReason.ADDRESS_INVALID_STAKING_INFO)
const pointer = stakingBlockchainPointer!
return {
type: StakingChoiceType.BLOCKCHAIN_POINTER,
type: StakingDataSource.BLOCKCHAIN_POINTER,
pointer: {
blockIndex: parseUint32_t(pointer.blockIndex, InvalidDataReason.ADDRESS_INVALID_BLOCKCHAIN_POINTER),
txIndex: parseUint32_t(pointer.txIndex, InvalidDataReason.ADDRESS_INVALID_BLOCKCHAIN_POINTER),
Expand All @@ -110,7 +110,7 @@ function extractStakingChoice(
validate(stakingBlockchainPointer == null, InvalidDataReason.ADDRESS_INVALID_BLOCKCHAIN_POINTER)
validate(stakingScriptHash == null, InvalidDataReason.ADDRESS_INVALID_STAKING_INFO)
return {
type: StakingChoiceType.NO_STAKING,
type: StakingDataSource.NONE,
}
default:
throw new InvalidData(InvalidDataReason.ADDRESS_UNKNOWN_TYPE)
Expand Down
4 changes: 2 additions & 2 deletions src/parsing/transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ export function parseSigningMode(mode: TransactionSigningMode): TransactionSigni
case TransactionSigningMode.ORDINARY_TRANSACTION:
case TransactionSigningMode.POOL_REGISTRATION_AS_OWNER:
case TransactionSigningMode.POOL_REGISTRATION_AS_OPERATOR:
case TransactionSigningMode.MULTISIGN_TRANSACTION:
case TransactionSigningMode.MULTISIG_TRANSACTION:
return mode
default:
throw new InvalidData(InvalidDataReason.SIGN_MODE_UNKNOWN)
Expand Down Expand Up @@ -304,7 +304,7 @@ export function parseSignTransactionRequest(request: SignTransactionRequest): Pa
)
break
}
case TransactionSigningMode.MULTISIGN_TRANSACTION: {
case TransactionSigningMode.MULTISIG_TRANSACTION: {
//TODO ???
break;
}
Expand Down
67 changes: 33 additions & 34 deletions src/types/internal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -187,31 +187,30 @@ export type ParsedPoolMetadata = {
hashHex: FixlenHexString<32>,
} & { __brand: 'pool_metadata' }

//TODO no spending for REWARD
export const enum SpendingChoiceType {
export const enum SpendingDataSourceType {
NONE = "no_spending",
PATH = "spending_path",
SCRIPT_HASH = "spending_script_hash",
}

type SpendingChoiceNone = {
type: SpendingChoiceType.NONE,
type SpendingDataSourceNone = {
type: SpendingDataSourceType.NONE,
}
type SpendingChoicePath = {
type: SpendingChoiceType.PATH,
type SpendingDataSourcePath = {
type: SpendingDataSourceType.PATH,
path: ValidBIP32Path,
}
type SpendingChoiceScriptHash = {
type: SpendingChoiceType.SCRIPT_HASH,
type SpendingDataSourceScriptHash = {
type: SpendingDataSourceType.SCRIPT_HASH,
scriptHash: FixlenHexString<typeof SCRIPT_HASH_LENGTH>,
}

export const enum StakingChoiceType {
NO_STAKING = 'no_staking',
STAKING_KEY_PATH = 'staking_key_path',
STAKING_KEY_HASH = 'staking_key_hash',
export const enum StakingDataSource {
NONE = 'no_staking',
KEY_PATH = 'staking_key_path',
KEY_HASH = 'staking_key_hash',
BLOCKCHAIN_POINTER = 'blockchain_pointer',
STAKING_SCRIPT_HASH = 'staking_script_hash',
SCRIPT_HASH = 'staking_script_hash',
}

type ParsedBlockchainPointer = {
Expand All @@ -220,34 +219,34 @@ type ParsedBlockchainPointer = {
certificateIndex: Uint32_t,
}

type StakingChoiceNone = {
type: StakingChoiceType.NO_STAKING
type StakingDataSourceNone = {
type: StakingDataSource.NONE
}
type StakingChoicePath = {
type: StakingChoiceType.STAKING_KEY_PATH,
type StakingDataSourcePath = {
type: StakingDataSource.KEY_PATH,
path: ValidBIP32Path
}
type StakingChoiceHash = {
type: StakingChoiceType.STAKING_KEY_HASH,
type StakingDataSourceKeyHash = {
type: StakingDataSource.KEY_HASH,
hashHex: FixlenHexString<typeof KEY_HASH_LENGTH>
}
type StakingChoicePointer = {
type: StakingChoiceType.BLOCKCHAIN_POINTER,
type StakingDataSourcePointer = {
type: StakingDataSource.BLOCKCHAIN_POINTER,
pointer: ParsedBlockchainPointer
}
type StakingChoiceScriptHash = {
type: StakingChoiceType.STAKING_SCRIPT_HASH,
type StakingDataSourceScriptHash = {
type: StakingDataSource.SCRIPT_HASH,
hashHex: FixlenHexString<typeof SCRIPT_HASH_LENGTH>
}

export type SpendingChoice = SpendingChoicePath | SpendingChoiceScriptHash | SpendingChoiceNone
export type StakingChoice = StakingChoiceNone | StakingChoicePath | StakingChoiceHash | StakingChoicePointer | StakingChoiceScriptHash
export type SpendingChoice = SpendingDataSourcePath | SpendingDataSourceScriptHash | SpendingDataSourceNone
export type StakingChoice = StakingDataSourceNone | StakingDataSourcePath | StakingDataSourceKeyHash | StakingDataSourcePointer | StakingDataSourceScriptHash

export type ByronAddressParams = {
type: AddressType.BYRON,
protocolMagic: Uint32_t
spendingChoice: SpendingChoicePath,
stakingChoice: StakingChoiceNone,
spendingChoice: SpendingDataSourcePath,
stakingChoice: StakingDataSourceNone,
}

export type ShelleyAddressParams = {
Expand All @@ -268,34 +267,34 @@ export type ShelleyAddressParams = {
AddressType.BASE_PAYMENT_KEY_STAKE_SCRIPT |
AddressType.ENTERPRISE_KEY |
AddressType.POINTER_KEY
spendingChoice: SpendingChoicePath
spendingChoice: SpendingDataSourcePath
} | {
type: AddressType.BASE_PAYMENT_SCRIPT_STAKE_KEY |
AddressType.BASE_PAYMENT_SCRIPT_STAKE_SCRIPT |
AddressType.ENTERPRISE_SCRIPT |
AddressType.POINTER_SCRIPT
spendingChoice: SpendingChoiceScriptHash
spendingChoice: SpendingDataSourceScriptHash
} | {
type: AddressType.REWARD_KEY | AddressType.REWARD_SCRIPT
spendingChoice: SpendingChoiceNone
spendingChoice: SpendingDataSourceNone
}
) & (
{
type: AddressType.BASE_PAYMENT_KEY_STAKE_KEY |
AddressType.BASE_PAYMENT_SCRIPT_STAKE_KEY |
AddressType.REWARD_KEY
stakingChoice: StakingChoicePath | StakingChoiceHash
stakingChoice: StakingDataSourcePath | StakingDataSourceKeyHash
} | {
type: AddressType.BASE_PAYMENT_KEY_STAKE_SCRIPT |
AddressType.BASE_PAYMENT_SCRIPT_STAKE_SCRIPT |
AddressType.REWARD_SCRIPT
stakingChoice: StakingChoiceScriptHash
stakingChoice: StakingDataSourceScriptHash
} | {
type: AddressType.ENTERPRISE_KEY | AddressType.ENTERPRISE_SCRIPT
stakingChoice: StakingChoiceNone
stakingChoice: StakingDataSourceNone
} | {
type: AddressType.POINTER_KEY | AddressType.POINTER_SCRIPT
stakingChoice: StakingChoicePointer
stakingChoice: StakingDataSourcePointer
}
)

Expand Down
6 changes: 3 additions & 3 deletions src/types/public.ts
Original file line number Diff line number Diff line change
Expand Up @@ -188,8 +188,8 @@ export type AddressParamsByron = {
* @see [[DeviceOwnedAddress]]
*/
export type AddressParamsBase =
| { spendingPath: BIP32Path} & AddressParamsBaseStaking
| { spendingScriptHash: string} & AddressParamsBaseStaking
({ spendingPath: BIP32Path} |
{ spendingScriptHash: string}) & AddressParamsBaseStaking

/**
* Shelley *base* address parameters staking choice.
Expand Down Expand Up @@ -1086,7 +1086,7 @@ export enum TransactionSigningMode {
/**
* TODO: no clue, but any amount of witnesses is fine
*/
MULTISIGN_TRANSACTION = "multisig_transaction",
MULTISIG_TRANSACTION = "multisig_transaction",
}

/**
Expand Down

0 comments on commit 33a2493

Please sign in to comment.