Skip to content

Commit

Permalink
add collaterals
Browse files Browse the repository at this point in the history
  • Loading branch information
mkv-vcm committed Oct 12, 2021
1 parent 438e55d commit 399f99d
Show file tree
Hide file tree
Showing 7 changed files with 63 additions and 3 deletions.
4 changes: 3 additions & 1 deletion src/errors/invalidDataReason.ts
Expand Up @@ -50,7 +50,7 @@ export enum InvalidDataReason {
TTL_INVALID = "invalid ttl",

CERTIFICATES_NOT_ARRAY = "certificates not an array",

CERTIFICATE_INVALID_PATH = "one of the certificates contains an invalid path",
CERTIFICATE_INVALID_SCRIPT_HASH = "one of the certificates contains an invalid script hash",
CERTIFICATE_INVALID_STAKE_CREDENTIAL = "one of the certificates contains both a path and a scripthash or neither",
Expand Down Expand Up @@ -134,6 +134,8 @@ export enum InvalidDataReason {

SCRIPT_DATA_HASH_WRONG_LENGTH = "script data hash not 32 long",

COLLATERALS_NOT_ARRAY = "collaterals not an array",

SIGN_MODE_UNKNOWN = "unknown signing mode",

SIGN_MODE_ORDINARY__POOL_REGISTRATION_NOT_ALLOWED =
Expand Down
4 changes: 4 additions & 0 deletions src/interactions/serialization/txInit.ts
Expand Up @@ -31,6 +31,9 @@ export function serializeTxInit(
const scriptDataHashBuffer = getCompatibility(version).supportsAlonso
? serializeOptionFlag(tx.scriptDataHashHex != null)
: Buffer.from([])
const collateralsBuffer = getCompatibility(version).supportsAlonso
? uint32_to_buf(tx.collaterals.length as Uint32_t)
: Buffer.from([])

return Buffer.concat([
uint8_to_buf(tx.network.networkId),
Expand All @@ -46,5 +49,6 @@ export function serializeTxInit(
uint32_to_buf(tx.certificates.length as Uint32_t),
uint32_to_buf(tx.withdrawals.length as Uint32_t),
uint32_to_buf(numWitnesses as Uint32_t),
collateralsBuffer,
])
}
26 changes: 24 additions & 2 deletions src/interactions/signTx.ts
Expand Up @@ -32,6 +32,7 @@ const enum P1 {
STAGE_CONFIRM = 0x0a,
STAGE_MINT = 0x0b,
STAGE_SCRIPT_DATA_HASH = 0x0c,
STAGE_COLLATERALS = 0x0d,
STAGE_WITNESSES = 0x0f,
}

Expand Down Expand Up @@ -497,6 +498,21 @@ function* signTx_setScriptDataHash(
})
}

function* signTx_addCollateral(
input: ParsedInput
): Interaction<void> {
const enum P2 {
UNUSED = 0x00,
}

yield send({
p1: P1.STAGE_COLLATERALS,
p2: P2.UNUSED,
data: serializeTxInput(input),
expectedResponseLength: 0,
})
}

function* signTx_awaitConfirm(
): Interaction<{ txHashHex: string; }> {
const enum P2 {
Expand Down Expand Up @@ -647,8 +663,10 @@ function ensureRequestSupportedByAppVersion(version: Version, request: ParsedSig
}
}

if (request?.tx?.scriptDataHashHex && !getCompatibility(version).supportsAlonso) {
throw new DeviceVersionUnsupported(`Alonso not supported by Ledger app version ${version}.`)
if (!getCompatibility(version).supportsAlonso) {
if (request?.tx?.scriptDataHashHex || request?.tx.collaterals.length != 0) {
throw new DeviceVersionUnsupported(`Alonso not supported by Ledger app version ${version}.`)
}
}
}

Expand Down Expand Up @@ -731,6 +749,10 @@ export function* signTransaction(version: Version, request: ParsedSigningRequest
yield* signTx_setScriptDataHash(tx.scriptDataHashHex)
}

for (const input of tx.collaterals) {
yield* signTx_addCollateral(input)
}

// confirm
const { txHashHex } = yield* signTx_awaitConfirm()

Expand Down
4 changes: 4 additions & 0 deletions src/parsing/transaction.ts
Expand Up @@ -141,6 +141,9 @@ export function parseTransaction(tx: Transaction): ParsedTransaction {
? null
: parseHexStringOfLength(tx.scriptDataHashHex, SCRIPT_DATA_HASH_LENGTH, InvalidDataReason.SCRIPT_DATA_HASH_WRONG_LENGTH)

validate(isArray(tx.collaterals ?? []), InvalidDataReason.COLLATERALS_NOT_ARRAY)
const collaterals = (tx.collaterals ?? []).map(inp => parseTxInput(inp))

return {
network,
inputs,
Expand All @@ -153,6 +156,7 @@ export function parseTransaction(tx: Transaction): ParsedTransaction {
fee,
mint,
scriptDataHashHex: scriptDataHash,
collaterals
}
}

Expand Down
1 change: 1 addition & 0 deletions src/types/internal.ts
Expand Up @@ -117,6 +117,7 @@ export type ParsedTransaction = {
validityIntervalStart: Uint64_str | null
mint: Array<ParsedAssetGroup<Int64_str>> | null
scriptDataHashHex: ScriptDataHash | null
collaterals: ParsedInput[]
}

export type ParsedSigningRequest = {
Expand Down
4 changes: 4 additions & 0 deletions src/types/public.ts
Expand Up @@ -1065,6 +1065,10 @@ export type Transaction = {
* Script Data hash (if any)
*/
scriptDataHashHex?: string | null,
/**
* Collaterals (if any)
*/
collaterals?: Array<TxInput> | null,
}

/**
Expand Down
23 changes: 23 additions & 0 deletions test/integration/__fixtures__/signTx.ts
Expand Up @@ -1051,6 +1051,29 @@ export const testsShelleyNoCertificates: TestcaseShelley[] = [
auxiliaryDataSupplement: null,
},
},
{
testname: "Sign tx with collaterals",
tx: {
...shelleyBase,
outputs: [],
collaterals: [inputs.utxoShelley],
},
signingMode: TransactionSigningMode.ORDINARY_TRANSACTION,
additionalWitnessPaths: [],
txBody: "a600818258203b40265111d8bb3c3c608d95b3a0bf83461ace32d79336579a1939b3aad1c0b700018002182a030a0d818258203b40265111d8bb3c3c608d95b3a0bf83461ace32d79336579a1939b3aad1c0b7000f01",
result: {
txHashHex:
"4e94b319a7e5a28f333932b0e2337b7c16da22f5eacae684edf2b2fbca2bf2f7",
witnesses: [
{
path: str_to_path("1852'/1815'/0'/0/0"),
witnessSignatureHex:
"8c1cbf88de350d9817aab84f878005342c677319d6c16210db28477eff9355a689a210ea14c449b455f7e77165055f4de4314efbab9a3c7aceae5288f8a73f0d",
},
],
auxiliaryDataSupplement: null,
},
},
]

export const testsShelleyWithCertificates: TestcaseShelley[] = [
Expand Down

0 comments on commit 399f99d

Please sign in to comment.