|
| 1 | +use aiken/cbor.{serialise} |
| 2 | +use aiken/collection/list |
| 3 | +use aiken/primitive/bytearray |
| 4 | +use cardano/address.{Address, Inline, Script, VerificationKey} |
| 5 | +use cardano/assets.{Lovelace, Value} |
| 6 | +use cardano/transaction.{ |
| 7 | + Datum, DatumHash, InlineDatum, Input, NoDatum, Output, OutputReference, |
| 8 | +} |
| 9 | +use types/cosponsor.{CosponsoredProposalProcedure} |
| 10 | + |
| 11 | +pub fn convert_inputs(inputs: List<Input>) -> ByteArray { |
| 12 | + let (length, bytes) = |
| 13 | + list.foldr( |
| 14 | + inputs, |
| 15 | + (0, ""), |
| 16 | + fn(input, acc) { |
| 17 | + let OutputReference { transaction_id: hash, output_index: index } = |
| 18 | + input.output_reference |
| 19 | + |
| 20 | + ( |
| 21 | + acc.1st + 1, |
| 22 | + bytearray.concat(#"82", hash) |
| 23 | + |> bytearray.concat(serialise(index)) |
| 24 | + |> bytearray.concat(acc.2nd), |
| 25 | + ) |
| 26 | + }, |
| 27 | + ) |
| 28 | + |
| 29 | + if length <= 23 { |
| 30 | + bytearray.from_int_big_endian(128 + length, 1) |> bytearray.concat(bytes) |
| 31 | + } else { |
| 32 | + bytearray.concat(#"9f", bytes) |> bytearray.concat(#"ff") |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +pub fn convert_mint(mint: Value) -> ByteArray { |
| 37 | + mint |> serialise |
| 38 | +} |
| 39 | + |
| 40 | +pub fn convert_outputs(outputs: List<Output>) -> ByteArray { |
| 41 | + let (length, bytes) = |
| 42 | + list.foldr( |
| 43 | + outputs, |
| 44 | + (0, ""), |
| 45 | + fn(output, acc) { |
| 46 | + let Output { address, value, datum, .. } = output |
| 47 | + |
| 48 | + let (add_datum, datum_bytes) = convert_datum(datum) |
| 49 | + ( |
| 50 | + acc.1st + 1, |
| 51 | + bytearray.from_int_big_endian(130 + add_datum, 1) |
| 52 | + |> bytearray.concat(convert_address(address)) |
| 53 | + |> bytearray.concat(convert_value(value)) |
| 54 | + |> bytearray.concat(datum_bytes) |
| 55 | + |> bytearray.concat(acc.2nd), |
| 56 | + ) |
| 57 | + }, |
| 58 | + ) |
| 59 | + |
| 60 | + if length <= 23 { |
| 61 | + bytearray.from_int_big_endian(128 + length, 1) |> bytearray.concat(bytes) |
| 62 | + } else { |
| 63 | + bytearray.concat(#"9f", bytes) |> bytearray.concat(#"ff") |
| 64 | + } |
| 65 | +} |
| 66 | + |
| 67 | +pub fn convert_fee(fee: Lovelace) -> ByteArray { |
| 68 | + fee |> serialise |
| 69 | +} |
| 70 | + |
| 71 | +pub fn convert_extra_signatories(signatures: List<ByteArray>) -> ByteArray { |
| 72 | + let (length, bytes) = |
| 73 | + list.foldr( |
| 74 | + signatures, |
| 75 | + (0, ""), |
| 76 | + fn(signature, acc) { |
| 77 | + (acc.1st + 1, bytearray.concat(serialise(signature), acc.2nd)) |
| 78 | + }, |
| 79 | + ) |
| 80 | + if length <= 23 { |
| 81 | + bytearray.from_int_big_endian(128 + length, 1) |> bytearray.concat(bytes) |
| 82 | + } else { |
| 83 | + bytearray.concat(#"9f", bytes) |> bytearray.concat(#"ff") |
| 84 | + } |
| 85 | +} |
| 86 | + |
| 87 | +fn convert_datum(datum: Datum) -> (Int, ByteArray) { |
| 88 | + when datum is { |
| 89 | + NoDatum -> (0, "") |
| 90 | + DatumHash(datum_hash) -> (1, datum_hash) |
| 91 | + InlineDatum(datum_bytes) -> (1, serialise(datum_bytes)) |
| 92 | + } |
| 93 | +} |
| 94 | + |
| 95 | +fn convert_value(assets: Value) -> ByteArray { |
| 96 | + let lovelace = assets.lovelace_of(assets) |> serialise |
| 97 | + let multi_asset = assets.without_lovelace(assets) |> serialise |
| 98 | + |
| 99 | + if multi_asset == #"a0" { |
| 100 | + lovelace |
| 101 | + } else { |
| 102 | + #"82" |> bytearray.concat(lovelace) |> bytearray.concat(multi_asset) |
| 103 | + } |
| 104 | +} |
| 105 | + |
| 106 | +fn convert_address(address: Address) -> ByteArray { |
| 107 | + //; bit 4: payment cred is keyhash/scripthash |
| 108 | + let (header_byte_pc, credential_p) = |
| 109 | + when address.payment_credential is { |
| 110 | + Script(cred) -> (16, cred) |
| 111 | + VerificationKey(cred) -> (0, cred) |
| 112 | + } |
| 113 | + let (header_byte_sc, credential_s) = |
| 114 | + when address.stake_credential is { |
| 115 | + None -> (96, "") |
| 116 | + Some(Inline(VerificationKey(cred))) -> (0, cred) |
| 117 | + Some(Inline(Script(cred))) -> (32, cred) |
| 118 | + _ -> fail @"no way" |
| 119 | + } |
| 120 | + bytearray.from_int_big_endian(header_byte_pc + header_byte_sc, 1) |
| 121 | + |> bytearray.concat(bytearray.concat(credential_p, credential_s)) |
| 122 | + |> serialise |
| 123 | +} |
| 124 | + |
| 125 | +pub fn convert_proposals( |
| 126 | + proposals: List<CosponsoredProposalProcedure>, |
| 127 | +) -> ByteArray { |
| 128 | + let (length, bytes) = |
| 129 | + list.foldr( |
| 130 | + proposals, |
| 131 | + (0, ""), |
| 132 | + fn(proposal, acc) { |
| 133 | + (acc.1st + 1, bytearray.concat(convert_proposal(proposal), acc.2nd)) |
| 134 | + }, |
| 135 | + ) |
| 136 | + if length <= 23 { |
| 137 | + bytearray.from_int_big_endian(128 + length, 1) |> bytearray.concat(bytes) |
| 138 | + } else { |
| 139 | + bytearray.concat(#"9f", bytes) |> bytearray.concat(#"ff") |
| 140 | + } |
| 141 | +} |
| 142 | + |
| 143 | +fn convert_proposal(proposal: CosponsoredProposalProcedure) -> ByteArray { |
| 144 | + #"84" |
| 145 | + |> bytearray.concat(serialise(proposal.procedure.deposit)) |
| 146 | + |> bytearray.concat(serialise(proposal.procedure.return_address)) |
| 147 | + |> bytearray.concat(serialise(proposal.procedure.governance_action)) |
| 148 | + |> bytearray.concat(serialise(proposal.anchor)) |
| 149 | +} |
0 commit comments