Skip to content

Commit 8b877fe

Browse files
Merge pull request #86 from Peersyst/feat/project-reference
[TA-3821]: Project reference
2 parents b0905ba + eca490e commit 8b877fe

File tree

49 files changed

+461
-478
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

49 files changed

+461
-478
lines changed

address-codec/base58check.go

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,6 @@ package addresscodec
22

33
import (
44
"crypto/sha256"
5-
"errors"
6-
)
7-
8-
// ErrChecksum indicates that the checksum of a check-encoded string does not verify against
9-
// the checksum.
10-
// ErrInvalidFormat indicates that the check-encoded string has an invalid format.
11-
var (
12-
ErrChecksum = errors.New("checksum error")
13-
ErrInvalidFormat = errors.New("invalid format: version and/or checksum bytes missing")
145
)
156

167
// checksum: first four bytes of sha256^2

address-codec/codec.go

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"bytes"
55
"encoding/hex"
66
"errors"
7-
"fmt"
87

98
"github.com/Peersyst/xrpl-go/address-codec/interfaces"
109
"github.com/Peersyst/xrpl-go/pkg/crypto"
@@ -27,24 +26,6 @@ const (
2726
NodePublicKeyPrefix = 0x1C
2827
)
2928

30-
var (
31-
// Errors
32-
33-
// Invalid classic address
34-
ErrInvalidClassicAddress = errors.New("invalid classic address")
35-
ErrInvalidSeed = errors.New("invalid seed; could not determine encoding algorithm")
36-
)
37-
38-
type EncodeLengthError struct {
39-
Instance string
40-
Input int
41-
Expected int
42-
}
43-
44-
func (e *EncodeLengthError) Error() string {
45-
return fmt.Sprintf("`%v` length should be %v not %v", e.Instance, e.Expected, e.Input)
46-
}
47-
4829
// Returns the base58 encoding of byte slice, with the given type prefix, whilst ensuring that the byte slice is the expected length.
4930
func Encode(b []byte, typePrefix []byte, expectedLength int) (string, error) {
5031

address-codec/errors.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package addresscodec
2+
3+
import (
4+
"errors"
5+
"fmt"
6+
)
7+
8+
var (
9+
// Static errors
10+
11+
// Invalid classic address
12+
ErrInvalidClassicAddress = errors.New("invalid classic address")
13+
// Invalid seed
14+
ErrInvalidSeed = errors.New("invalid seed; could not determine encoding algorithm")
15+
16+
// Invalid x-address
17+
ErrInvalidXAddress = errors.New("invalid x-address")
18+
// Invalid tag
19+
ErrInvalidTag = errors.New("invalid tag")
20+
// Invalid accountId
21+
ErrInvalidAccountID = errors.New("invalid accountId")
22+
23+
// ErrChecksum indicates that the checksum of a check-encoded string does not verify against
24+
// the checksum.
25+
ErrChecksum = errors.New("checksum error")
26+
// ErrInvalidFormat indicates that the check-encoded string has an invalid format.
27+
ErrInvalidFormat = errors.New("invalid format: version and/or checksum bytes missing")
28+
)
29+
30+
// Dynamic errors
31+
32+
// EncodeLengthError is an error that occurs when the length of the input does not match the expected length.
33+
type EncodeLengthError struct {
34+
Instance string
35+
Input int
36+
Expected int
37+
}
38+
39+
// Error implements the error interface.
40+
func (e *EncodeLengthError) Error() string {
41+
return fmt.Sprintf("`%v` length should be %v not %v", e.Instance, e.Expected, e.Input)
42+
}

address-codec/interfaces/crypto.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package interfaces
22

3+
// CryptoImplementation defines an interface for implementing cryptographic operations
4+
// required by address codec.
35
type CryptoImplementation interface {
46
DeriveKeypair(decodedSeed []byte, validator bool) (string, string, error)
57
Sign(msg, privKey string) (string, error)

address-codec/x_codec.go

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,13 @@ package addresscodec
22

33
import (
44
"bytes"
5-
"errors"
65
)
76

87
var (
98
MainnetXAddressPrefix = []byte{0x05, 0x44}
109
TestnetXAddressPrefix = []byte{0x04, 0x93}
1110
// X-address length - value is 35
1211
XAddressLength = 35
13-
14-
// Errors
15-
16-
// Invalid x-address
17-
ErrInvalidXAddress = errors.New("invalid x-address")
18-
// Invalid tag
19-
ErrInvalidTag = errors.New("invalid tag")
20-
// Invalid accountId
21-
ErrInvalidAccountID = errors.New("invalid accountId")
2212
)
2313

2414
// IsValidXAddress returns true if the x-address is valid. Otherwise, it returns false.
Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,10 @@ import (
1313
)
1414

1515
var (
16-
errSigningClaimFieldNotFound = errors.New("'Channel' & 'Amount' fields are both required, but were not found")
16+
// Static errors
17+
18+
// ErrSigningClaimFieldNotFound is returned when the 'Channel' & 'Amount' fields are both required, but were not found.
19+
ErrSigningClaimFieldNotFound = errors.New("'Channel' & 'Amount' fields are both required, but were not found")
1720
)
1821

1922
const (
@@ -89,7 +92,7 @@ func EncodeForSigning(json map[string]any) (string, error) {
8992
func EncodeForSigningClaim(json map[string]any) (string, error) {
9093

9194
if json["Channel"] == nil || json["Amount"] == nil {
92-
return "", errSigningClaimFieldNotFound
95+
return "", ErrSigningClaimFieldNotFound
9396
}
9497

9598
channel, err := types.NewHash256().FromJSON(json["Channel"])
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -565,15 +565,15 @@ func TestEncodeForSigningClaim(t *testing.T) {
565565
"Amount": "1000",
566566
},
567567
output: "",
568-
expectedErr: errSigningClaimFieldNotFound,
568+
expectedErr: ErrSigningClaimFieldNotFound,
569569
},
570570
{
571571
description: "fail to encode claim - no amount",
572572
input: map[string]any{
573573
"Channel": "43904CBFCDCEC530B4037871F86EE90BF799DF8D2E0EA564BC8A3F332E4F5FB1",
574574
},
575575
output: "",
576-
expectedErr: errSigningClaimFieldNotFound,
576+
expectedErr: ErrSigningClaimFieldNotFound,
577577
},
578578
}
579579

binary-codec/definitions/definitions.go

Lines changed: 8 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -2,56 +2,18 @@ package definitions
22

33
import (
44
_ "embed"
5-
"errors"
6-
"fmt"
75

86
"github.com/ugorji/go/codec"
97
)
108

119
var (
10+
//go:embed definitions.json
11+
docBytes []byte
12+
1213
// definitions is the singleton instance of the Definitions struct.
1314
definitions *Definitions
14-
15-
// Errors
16-
17-
// ErrUnableToCastFieldInfo is returned when the field info cannot be cast.
18-
ErrUnableToCastFieldInfo = errors.New("unable to cast to field info")
1915
)
2016

21-
//go:embed definitions.json
22-
var docBytes []byte
23-
24-
func Get() *Definitions {
25-
return definitions
26-
}
27-
28-
type NotFoundError struct {
29-
Instance string
30-
Input string
31-
}
32-
33-
func (e *NotFoundError) Error() string {
34-
return fmt.Sprintf("%v %v not found", e.Instance, e.Input)
35-
}
36-
37-
type NotFoundErrorInt struct {
38-
Instance string
39-
Input int32
40-
}
41-
42-
func (e *NotFoundErrorInt) Error() string {
43-
return fmt.Sprintf("%v %v not found", e.Instance, e.Input)
44-
}
45-
46-
type NotFoundErrorFieldHeader struct {
47-
Instance string
48-
Input FieldHeader
49-
}
50-
51-
func (e *NotFoundErrorFieldHeader) Error() string {
52-
return fmt.Sprintf("%v %v not found", e.Instance, e.Input)
53-
}
54-
5517
type Definitions struct {
5618
Types map[string]int32
5719
LedgerEntryTypes map[string]int32
@@ -60,6 +22,11 @@ type Definitions struct {
6022
TransactionTypes map[string]int32
6123
FieldIDNameMap map[FieldHeader]string
6224
}
25+
26+
func Get() *Definitions {
27+
return definitions
28+
}
29+
6330
type definitionsDoc struct {
6431
Types map[string]int32 `json:"TYPES"`
6532
LedgerEntryTypes map[string]int32 `json:"LEDGER_ENTRY_TYPES"`

binary-codec/definitions/errors.go

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package definitions
2+
3+
import (
4+
"errors"
5+
"fmt"
6+
)
7+
8+
var (
9+
// Static errors
10+
11+
// ErrUnableToCastFieldInfo is returned when the field info cannot be cast.
12+
ErrUnableToCastFieldInfo = errors.New("unable to cast to field info")
13+
)
14+
15+
// Dynamic errors
16+
17+
// NotFoundError is an error that occurs when a value is not found.
18+
type NotFoundError struct {
19+
Instance string
20+
Input string
21+
}
22+
23+
// Error implements the error interface.
24+
func (e *NotFoundError) Error() string {
25+
return fmt.Sprintf("%v %v not found", e.Instance, e.Input)
26+
}
27+
28+
// NotFoundErrorInt is an error that occurs when a value is not found.
29+
type NotFoundErrorInt struct {
30+
Instance string
31+
Input int32
32+
}
33+
34+
// Error implements the error interface.
35+
func (e *NotFoundErrorInt) Error() string {
36+
return fmt.Sprintf("%v %v not found", e.Instance, e.Input)
37+
}
38+
39+
// NotFoundErrorFieldHeader is an error that occurs when a value is not found.
40+
type NotFoundErrorFieldHeader struct {
41+
Instance string
42+
Input FieldHeader
43+
}
44+
45+
// Error implements the error interface.
46+
func (e *NotFoundErrorFieldHeader) Error() string {
47+
return fmt.Sprintf("%v %v not found", e.Instance, e.Input)
48+
}

binary-codec/definitions/field_instance.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,15 @@ package definitions
22

33
import "github.com/ugorji/go/codec"
44

5+
// FieldInstance is a struct that represents a field instance.
56
type FieldInstance struct {
67
FieldName string
78
*FieldInfo
89
FieldHeader *FieldHeader
910
Ordinal int32
1011
}
1112

13+
// FieldInfo is a struct that represents the field info.
1214
type FieldInfo struct {
1315
Nth int32
1416
IsVLEncoded bool
@@ -17,11 +19,13 @@ type FieldInfo struct {
1719
Type string
1820
}
1921

22+
// FieldHeader is a struct that represents the field header.
2023
type FieldHeader struct {
2124
TypeCode int32
2225
FieldCode int32
2326
}
2427

28+
// CreateFieldHeader creates a new field header.
2529
func (d *Definitions) CreateFieldHeader(tc, fc int32) FieldHeader {
2630
return FieldHeader{
2731
TypeCode: tc,

0 commit comments

Comments
 (0)