|
| 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 common |
| 16 | + |
| 17 | +import ( |
| 18 | + "fmt" |
| 19 | + |
| 20 | + "github.com/blinklabs-io/gouroboros/cbor" |
| 21 | +) |
| 22 | + |
| 23 | +type NativeScript struct { |
| 24 | + item any |
| 25 | +} |
| 26 | + |
| 27 | +func (n *NativeScript) Item() any { |
| 28 | + return n.item |
| 29 | +} |
| 30 | + |
| 31 | +func (n *NativeScript) UnmarshalCBOR(data []byte) error { |
| 32 | + id, err := cbor.DecodeIdFromList(data) |
| 33 | + if err != nil { |
| 34 | + return err |
| 35 | + } |
| 36 | + var tmpData any |
| 37 | + switch id { |
| 38 | + case 0: |
| 39 | + tmpData = &NativeScriptPubkey{} |
| 40 | + case 1: |
| 41 | + tmpData = &NativeScriptAll{} |
| 42 | + case 2: |
| 43 | + tmpData = &NativeScriptAny{} |
| 44 | + case 3: |
| 45 | + tmpData = &NativeScriptNofK{} |
| 46 | + case 4: |
| 47 | + tmpData = &NativeScriptInvalidBefore{} |
| 48 | + case 5: |
| 49 | + tmpData = &NativeScriptInvalidHereafter{} |
| 50 | + default: |
| 51 | + return fmt.Errorf("unknown native script type %d", id) |
| 52 | + } |
| 53 | + if _, err := cbor.Decode(data, tmpData); err != nil { |
| 54 | + return err |
| 55 | + } |
| 56 | + return nil |
| 57 | +} |
| 58 | + |
| 59 | +type NativeScriptPubkey struct { |
| 60 | + cbor.StructAsArray |
| 61 | + Type uint |
| 62 | + Hash []byte |
| 63 | +} |
| 64 | + |
| 65 | +type NativeScriptAll struct { |
| 66 | + cbor.StructAsArray |
| 67 | + Type uint |
| 68 | + Scripts []NativeScript |
| 69 | +} |
| 70 | + |
| 71 | +type NativeScriptAny struct { |
| 72 | + cbor.StructAsArray |
| 73 | + Type uint |
| 74 | + Scripts []NativeScript |
| 75 | +} |
| 76 | + |
| 77 | +type NativeScriptNofK struct { |
| 78 | + cbor.StructAsArray |
| 79 | + Type uint |
| 80 | + N uint |
| 81 | + Scripts []NativeScript |
| 82 | +} |
| 83 | + |
| 84 | +type NativeScriptInvalidBefore struct { |
| 85 | + cbor.StructAsArray |
| 86 | + Type uint |
| 87 | + Slot uint64 |
| 88 | +} |
| 89 | + |
| 90 | +type NativeScriptInvalidHereafter struct { |
| 91 | + cbor.StructAsArray |
| 92 | + Type uint |
| 93 | + Slot uint64 |
| 94 | +} |
0 commit comments