|
| 1 | +// Copyright 2025 Blink Labs Software |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package script |
| 16 | + |
| 17 | +import ( |
| 18 | + "bytes" |
| 19 | + "slices" |
| 20 | + |
| 21 | + lcommon "github.com/blinklabs-io/gouroboros/ledger/common" |
| 22 | + "github.com/blinklabs-io/plutigo/data" |
| 23 | +) |
| 24 | + |
| 25 | +type ScriptInfo interface { |
| 26 | + isScriptInfo() |
| 27 | + ScriptHash() lcommon.ScriptHash |
| 28 | + ToPlutusData |
| 29 | +} |
| 30 | + |
| 31 | +type ScriptInfoMinting struct { |
| 32 | + PolicyId lcommon.Blake2b224 |
| 33 | +} |
| 34 | + |
| 35 | +func (ScriptInfoMinting) isScriptInfo() {} |
| 36 | + |
| 37 | +func (s ScriptInfoMinting) ScriptHash() lcommon.ScriptHash { |
| 38 | + return s.PolicyId |
| 39 | +} |
| 40 | + |
| 41 | +func (s ScriptInfoMinting) ToPlutusData() data.PlutusData { |
| 42 | + return data.NewConstr( |
| 43 | + 0, |
| 44 | + data.NewByteString(s.PolicyId.Bytes()), |
| 45 | + ) |
| 46 | +} |
| 47 | + |
| 48 | +type ScriptInfoSpending struct { |
| 49 | + Input lcommon.Utxo |
| 50 | + Datum data.PlutusData |
| 51 | +} |
| 52 | + |
| 53 | +func (ScriptInfoSpending) isScriptInfo() {} |
| 54 | + |
| 55 | +func (s ScriptInfoSpending) ScriptHash() lcommon.ScriptHash { |
| 56 | + tmpAddr := s.Input.Output.Address() |
| 57 | + return tmpAddr.PaymentKeyHash() |
| 58 | +} |
| 59 | + |
| 60 | +func (s ScriptInfoSpending) ToPlutusData() data.PlutusData { |
| 61 | + if s.Datum == nil { |
| 62 | + return data.NewConstr( |
| 63 | + 1, |
| 64 | + s.Input.Id.ToPlutusData(), |
| 65 | + ) |
| 66 | + } |
| 67 | + return data.NewConstr( |
| 68 | + 1, |
| 69 | + s.Input.Id.ToPlutusData(), |
| 70 | + data.NewConstr( |
| 71 | + 0, |
| 72 | + s.Datum, |
| 73 | + ), |
| 74 | + ) |
| 75 | +} |
| 76 | + |
| 77 | +type ScriptInfoRewarding struct { |
| 78 | + StakeCredential lcommon.Credential |
| 79 | +} |
| 80 | + |
| 81 | +func (ScriptInfoRewarding) isScriptInfo() {} |
| 82 | + |
| 83 | +func (s ScriptInfoRewarding) ScriptHash() lcommon.ScriptHash { |
| 84 | + // TODO |
| 85 | + return lcommon.ScriptHash{} |
| 86 | +} |
| 87 | + |
| 88 | +func (s ScriptInfoRewarding) ToPlutusData() data.PlutusData { |
| 89 | + // TODO |
| 90 | + return nil |
| 91 | +} |
| 92 | + |
| 93 | +type ScriptInfoCertifying struct { |
| 94 | + Size uint64 |
| 95 | + Certificate lcommon.Certificate |
| 96 | +} |
| 97 | + |
| 98 | +func (ScriptInfoCertifying) isScriptInfo() {} |
| 99 | + |
| 100 | +func (s ScriptInfoCertifying) ScriptHash() lcommon.ScriptHash { |
| 101 | + // TODO |
| 102 | + return lcommon.ScriptHash{} |
| 103 | +} |
| 104 | + |
| 105 | +func (s ScriptInfoCertifying) ToPlutusData() data.PlutusData { |
| 106 | + // TODO |
| 107 | + return nil |
| 108 | +} |
| 109 | + |
| 110 | +type ScriptInfoVoting struct { |
| 111 | + Voter lcommon.Voter |
| 112 | +} |
| 113 | + |
| 114 | +func (ScriptInfoVoting) isScriptInfo() {} |
| 115 | + |
| 116 | +func (s ScriptInfoVoting) ScriptHash() lcommon.ScriptHash { |
| 117 | + // TODO |
| 118 | + return lcommon.ScriptHash{} |
| 119 | +} |
| 120 | + |
| 121 | +func (s ScriptInfoVoting) ToPlutusData() data.PlutusData { |
| 122 | + // TODO |
| 123 | + return nil |
| 124 | +} |
| 125 | + |
| 126 | +type ScriptInfoProposing struct { |
| 127 | + Size uint64 |
| 128 | + ProposalProcedure lcommon.ProposalProcedure |
| 129 | +} |
| 130 | + |
| 131 | +func (ScriptInfoProposing) isScriptInfo() {} |
| 132 | + |
| 133 | +func (s ScriptInfoProposing) ScriptHash() lcommon.ScriptHash { |
| 134 | + // TODO |
| 135 | + return lcommon.ScriptHash{} |
| 136 | +} |
| 137 | + |
| 138 | +func (s ScriptInfoProposing) ToPlutusData() data.PlutusData { |
| 139 | + // TODO |
| 140 | + return nil |
| 141 | +} |
| 142 | + |
| 143 | +type toScriptPurposeFunc func(lcommon.RedeemerKey) ScriptInfo |
| 144 | + |
| 145 | +// scriptPurposeBuilder creates a reusable function preloaded with information about a particular transaction |
| 146 | +func scriptPurposeBuilder( |
| 147 | + resolvedInputs []lcommon.Utxo, |
| 148 | + inputs []lcommon.TransactionInput, |
| 149 | + mint lcommon.MultiAsset[lcommon.MultiAssetTypeMint], |
| 150 | + // TODO: certificates |
| 151 | + withdrawals map[*lcommon.Address]uint64, |
| 152 | + // TODO: proposal procedures |
| 153 | + // TODO: votes |
| 154 | +) toScriptPurposeFunc { |
| 155 | + return func(redeemerKey lcommon.RedeemerKey) ScriptInfo { |
| 156 | + // TODO: implement additional redeemer tags |
| 157 | + // https://github.com/aiken-lang/aiken/blob/af4e04b91e54dbba3340de03fc9e65a90f24a93b/crates/uplc/src/tx/script_context.rs#L771-L826 |
| 158 | + switch redeemerKey.Tag { |
| 159 | + case lcommon.RedeemerTagSpend: |
| 160 | + var datum data.PlutusData |
| 161 | + tmpInput := inputs[redeemerKey.Index] |
| 162 | + var resolvedInput lcommon.Utxo |
| 163 | + for _, tmpResolvedInput := range resolvedInputs { |
| 164 | + if tmpResolvedInput.Id.String() == tmpInput.String() { |
| 165 | + resolvedInput = tmpResolvedInput |
| 166 | + if tmpDatum := resolvedInput.Output.Datum(); tmpDatum != nil { |
| 167 | + datum = tmpDatum.Data |
| 168 | + } |
| 169 | + break |
| 170 | + } |
| 171 | + } |
| 172 | + return ScriptInfoSpending{ |
| 173 | + Input: resolvedInput, |
| 174 | + Datum: datum, |
| 175 | + } |
| 176 | + case lcommon.RedeemerTagMint: |
| 177 | + // TODO: fix this to work for more than one minted policy |
| 178 | + mintPolicies := mint.Policies() |
| 179 | + slices.SortFunc( |
| 180 | + mintPolicies, |
| 181 | + func(a, b lcommon.Blake2b224) int { return bytes.Compare(a.Bytes(), b.Bytes()) }, |
| 182 | + ) |
| 183 | + return ScriptInfoMinting{ |
| 184 | + PolicyId: mintPolicies[redeemerKey.Index], |
| 185 | + } |
| 186 | + case lcommon.RedeemerTagCert: |
| 187 | + return nil |
| 188 | + case lcommon.RedeemerTagReward: |
| 189 | + return nil |
| 190 | + case lcommon.RedeemerTagVoting: |
| 191 | + return nil |
| 192 | + case lcommon.RedeemerTagProposing: |
| 193 | + return nil |
| 194 | + } |
| 195 | + return nil |
| 196 | + } |
| 197 | +} |
| 198 | + |
| 199 | +func scriptPurposeStripDatum(purpose ScriptInfo) ScriptInfo { |
| 200 | + switch p := purpose.(type) { |
| 201 | + case ScriptInfoSpending: |
| 202 | + p.Datum = nil |
| 203 | + return p |
| 204 | + } |
| 205 | + return purpose |
| 206 | +} |
0 commit comments