-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #12 from blinklabs-io/feat/cardano-dns-datum
feat: add Cardano DNS domain datum
- Loading branch information
Showing
4 changed files
with
201 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
// Copyright 2023 Blink Labs Software | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package models | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/blinklabs-io/gouroboros/cbor" | ||
) | ||
|
||
type CardanoDnsDomain struct { | ||
Origin []byte | ||
Records []CardanoDnsDomainRecord | ||
AdditionalData any | ||
} | ||
|
||
func (c CardanoDnsDomain) String() string { | ||
ret := fmt.Sprintf( | ||
"CardanoDnsDomain { Origin = %s, Records = [ ", | ||
c.Origin, | ||
) | ||
for idx, record := range c.Records { | ||
ret += record.String() | ||
if idx == len(c.Records)-1 { | ||
ret += " " | ||
} else { | ||
ret += ", " | ||
} | ||
} | ||
ret += "] }" | ||
return ret | ||
} | ||
|
||
func (c *CardanoDnsDomain) UnmarshalCBOR(cborData []byte) error { | ||
var tmpData cbor.Constructor | ||
if _, err := cbor.Decode(cborData, &tmpData); err != nil { | ||
return err | ||
} | ||
if tmpData.Constructor() != 1 { | ||
return fmt.Errorf("unexpected constructor index: %d", tmpData.Constructor()) | ||
} | ||
tmpFields := tmpData.Fields() | ||
switch len(tmpFields) { | ||
case 2, 3: | ||
c.Origin = tmpFields[0].(cbor.ByteString).Bytes() | ||
for _, record := range tmpFields[1].([]any) { | ||
recordConstr := record.(cbor.Constructor) | ||
recordFields := recordConstr.Fields() | ||
if recordConstr.Constructor() != 1 { | ||
return fmt.Errorf("unexpected constructor index: %d", recordConstr.Constructor()) | ||
} | ||
var tmpRecord CardanoDnsDomainRecord | ||
switch len(recordFields) { | ||
case 3: | ||
tmpRecord.Lhs = recordFields[0].(cbor.ByteString).Bytes() | ||
tmpRecord.Type = recordFields[1].(cbor.ByteString).Bytes() | ||
tmpRecord.Rhs = recordFields[2].(cbor.ByteString).Bytes() | ||
case 4: | ||
tmpRecord.Lhs = recordFields[0].(cbor.ByteString).Bytes() | ||
tmpRecord.Ttl = uint(recordFields[1].(uint64)) | ||
tmpRecord.Type = recordFields[2].(cbor.ByteString).Bytes() | ||
tmpRecord.Rhs = recordFields[3].(cbor.ByteString).Bytes() | ||
default: | ||
return fmt.Errorf("unexpected constructor field length: %d", len(recordFields)) | ||
} | ||
c.Records = append(c.Records, tmpRecord) | ||
} | ||
if len(tmpData.Fields()) == 3 { | ||
c.AdditionalData = tmpData.Fields()[2] | ||
} | ||
default: | ||
return fmt.Errorf("unexpected constructor field length: %d", len(tmpData.Fields())) | ||
} | ||
return nil | ||
} | ||
|
||
type CardanoDnsDomainRecord struct { | ||
Lhs []byte | ||
Ttl uint | ||
Type []byte | ||
Rhs []byte | ||
} | ||
|
||
func (c CardanoDnsDomainRecord) String() string { | ||
return fmt.Sprintf( | ||
"CardanoDnsDomainRecord { Lhs = %s, Ttl = %d, Type = %s, Rhs = %s }", | ||
c.Lhs, | ||
c.Ttl, | ||
c.Type, | ||
c.Rhs, | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
// Copyright 2023 Blink Labs Software | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package models_test | ||
|
||
import ( | ||
"encoding/hex" | ||
"reflect" | ||
"testing" | ||
|
||
models "github.com/blinklabs-io/cardano-models" | ||
|
||
"github.com/blinklabs-io/gouroboros/cbor" | ||
) | ||
|
||
var cardanoDnsTestDefs = []struct { | ||
cborHex string | ||
expectedObj models.CardanoDnsDomain | ||
}{ | ||
{ | ||
cborHex: "d87a9f4b666f6f2e63617264616e6f9fd87a9f4b666f6f2e63617264616e6f426e734f6e73312e666f6f2e63617264616e6fffd87a9f4b666f6f2e63617264616e6f426e734f6e73322e666f6f2e63617264616e6fffd87a9f4f6e73312e666f6f2e63617264616e6f41614a3137322e32382e302e32ffd87a9f4f6e73322e666f6f2e63617264616e6f187b416147312e322e332e34ffffff", | ||
expectedObj: models.CardanoDnsDomain{ | ||
Origin: []byte("foo.cardano"), | ||
Records: []models.CardanoDnsDomainRecord{ | ||
{ | ||
Lhs: []byte("foo.cardano"), | ||
Type: []byte("ns"), | ||
Rhs: []byte("ns1.foo.cardano"), | ||
}, | ||
{ | ||
Lhs: []byte("foo.cardano"), | ||
Type: []byte("ns"), | ||
Rhs: []byte("ns2.foo.cardano"), | ||
}, | ||
{ | ||
Lhs: []byte("ns1.foo.cardano"), | ||
Type: []byte("a"), | ||
Rhs: []byte("172.28.0.2"), | ||
}, | ||
{ | ||
Lhs: []byte("ns2.foo.cardano"), | ||
Ttl: 123, | ||
Type: []byte("a"), | ||
Rhs: []byte("1.2.3.4"), | ||
}, | ||
}, | ||
}, | ||
}, | ||
} | ||
|
||
func TestCardanoDnsDecode(t *testing.T) { | ||
for _, testDef := range cardanoDnsTestDefs { | ||
testDatumBytes, err := hex.DecodeString(testDef.cborHex) | ||
if err != nil { | ||
t.Fatalf("unexpected error decoding test datum hex: %s", err) | ||
} | ||
// Decode CBOR into object | ||
var testObj models.CardanoDnsDomain | ||
if _, err := cbor.Decode(testDatumBytes, &testObj); err != nil { | ||
t.Fatalf("unexpected error decoding test datum CBOR: %s", err) | ||
} | ||
if !reflect.DeepEqual(testObj, testDef.expectedObj) { | ||
t.Fatalf( | ||
"CBOR did not decode to expected object\n got: %s\n wanted: %s", | ||
testObj.String(), | ||
testDef.expectedObj.String(), | ||
) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,11 @@ | ||
module github.com/blinklabs-io/cardano-models | ||
|
||
go 1.20 | ||
|
||
require github.com/blinklabs-io/gouroboros v0.64.0 | ||
|
||
require ( | ||
github.com/fxamacker/cbor/v2 v2.5.0 // indirect | ||
github.com/jinzhu/copier v0.4.0 // indirect | ||
github.com/x448/float16 v0.8.4 // indirect | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
github.com/blinklabs-io/gouroboros v0.64.0 h1:8wm1blxhE+qI/GElc3/pmi3YoI3jepEEPFJK6LkP/JE= | ||
github.com/blinklabs-io/gouroboros v0.64.0/go.mod h1:fph4LBNmSliMxt5ut40lXqqbZHWmXjT6p7o1hCyDEbQ= | ||
github.com/fxamacker/cbor/v2 v2.5.0 h1:oHsG0V/Q6E/wqTS2O1Cozzsy69nqCiguo5Q1a1ADivE= | ||
github.com/fxamacker/cbor/v2 v2.5.0/go.mod h1:TA1xS00nchWmaBnEIxPSE5oHLuJBAVvqrtAnWBwBCVo= | ||
github.com/jinzhu/copier v0.4.0 h1:w3ciUoD19shMCRargcpm0cm91ytaBhDvuRpz1ODO/U8= | ||
github.com/jinzhu/copier v0.4.0/go.mod h1:DfbEm0FYsaqBcKcFuvmOZb218JkPGtvSHsKg8S8hyyg= | ||
github.com/x448/float16 v0.8.4 h1:qLwI1I70+NjRFUR3zs1JPUCgaCXSh3SW62uAKT1mSBM= | ||
github.com/x448/float16 v0.8.4/go.mod h1:14CWIYCyZA/cWjXOioeEpHeN/83MdbZDRQHoFcYsOfg= |