From e221f834ec451323f080275f50c5d5041ff859ec Mon Sep 17 00:00:00 2001 From: Louis Singer <41042567+louisinger@users.noreply.github.com> Date: Mon, 6 Nov 2023 16:07:02 +0100 Subject: [PATCH] Add BIP68 encoding/decoding (#2) * add BIP68 func * bip68_test.go: use require assertions * add DecodeBIP68 func --- .gitignore | 5 ++- pkg/ark-sdk/bip68.go | 58 ++++++++++++++++++++++++++++ pkg/ark-sdk/bip68_test.go | 43 +++++++++++++++++++++ pkg/ark-sdk/fixtures/bip68.json | 67 +++++++++++++++++++++++++++++++++ pkg/ark-sdk/go.mod | 7 ++++ pkg/ark-sdk/go.sum | 5 +++ 6 files changed, 184 insertions(+), 1 deletion(-) create mode 100644 pkg/ark-sdk/bip68.go create mode 100644 pkg/ark-sdk/bip68_test.go create mode 100644 pkg/ark-sdk/fixtures/bip68.json diff --git a/.gitignore b/.gitignore index 43e4731..757492d 100755 --- a/.gitignore +++ b/.gitignore @@ -18,4 +18,7 @@ DS_Store ._.DS_Store **/.DS_Store **/._.DS_Store - \ No newline at end of file + + +go.work +go.work.sum \ No newline at end of file diff --git a/pkg/ark-sdk/bip68.go b/pkg/ark-sdk/bip68.go new file mode 100644 index 0000000..33f1bec --- /dev/null +++ b/pkg/ark-sdk/bip68.go @@ -0,0 +1,58 @@ +package sdk + +import ( + "encoding/hex" + "fmt" +) + +const ( + SEQUENCE_LOCKTIME_MASK = 0x0000ffff + SEQUENCE_LOCKTIME_TYPE_FLAG = 1 << 22 + SEQUENCE_LOCKTIME_GRANULARITY = 9 + SECONDS_MOD = 1 << SEQUENCE_LOCKTIME_GRANULARITY + SECONDS_MAX = SEQUENCE_LOCKTIME_MASK << SEQUENCE_LOCKTIME_GRANULARITY + SEQUENCE_LOCKTIME_DISABLE_FLAG = 1 << 31 +) + +func closerToModulo512(x uint) uint { + return x - (x % 512) +} + +// BIP68 returns the encoded sequence locktime for the given number of seconds. +func BIP68(seconds uint) ([]byte, error) { + seconds = closerToModulo512(seconds) + if seconds > SECONDS_MAX { + return nil, fmt.Errorf("seconds too large, max is %d", SECONDS_MAX) + } + if seconds%SECONDS_MOD != 0 { + return nil, fmt.Errorf("seconds must be a multiple of %d", SECONDS_MOD) + } + + asNumber := SEQUENCE_LOCKTIME_TYPE_FLAG | (seconds >> SEQUENCE_LOCKTIME_GRANULARITY) + hexString := fmt.Sprintf("%x", asNumber) + reversed, err := hex.DecodeString(hexString) + if err != nil { + return nil, err + } + for i, j := 0, len(reversed)-1; i < j; i, j = i+1, j-1 { + reversed[i], reversed[j] = reversed[j], reversed[i] + } + return reversed, nil +} + +func DecodeBIP68(sequence []byte) (uint, error) { + // sequence to int + var asNumber int64 + for i := len(sequence) - 1; i >= 0; i-- { + asNumber = asNumber<<8 | int64(sequence[i]) + } + + if asNumber&SEQUENCE_LOCKTIME_DISABLE_FLAG != 0 { + return 0, fmt.Errorf("sequence is disabled") + } + if asNumber&SEQUENCE_LOCKTIME_TYPE_FLAG != 0 { + seconds := asNumber & SEQUENCE_LOCKTIME_MASK << SEQUENCE_LOCKTIME_GRANULARITY + return uint(seconds), nil + } + return 0, fmt.Errorf("sequence is encoded as block number") +} diff --git a/pkg/ark-sdk/bip68_test.go b/pkg/ark-sdk/bip68_test.go new file mode 100644 index 0000000..4680dee --- /dev/null +++ b/pkg/ark-sdk/bip68_test.go @@ -0,0 +1,43 @@ +package sdk_test + +import ( + "encoding/json" + "os" + "testing" + + sdk "github.com/ark-network/ark-sdk" + "github.com/stretchr/testify/require" +) + +func TestBIP68(t *testing.T) { + data, err := os.ReadFile("fixtures/bip68.json") + require.NoError(t, err) + + var testCases []struct { + Input uint `json:"seconds"` + Expected int64 `json:"sequence"` + Desc string `json:"description"` + } + err = json.Unmarshal(data, &testCases) + require.NoError(t, err) + require.NotEmpty(t, testCases) + + for _, tc := range testCases { + t.Run(tc.Desc, func(t *testing.T) { + actual, err := sdk.BIP68(tc.Input) + require.NoError(t, err) + + var asNumber int64 + for i := len(actual) - 1; i >= 0; i-- { + asNumber = asNumber<<8 | int64(actual[i]) + } + + require.Equal(t, tc.Expected, asNumber) + + decoded, err := sdk.DecodeBIP68(actual) + require.NoError(t, err) + + require.Equal(t, tc.Input, decoded) + }) + } +} diff --git a/pkg/ark-sdk/fixtures/bip68.json b/pkg/ark-sdk/fixtures/bip68.json new file mode 100644 index 0000000..fac4f86 --- /dev/null +++ b/pkg/ark-sdk/fixtures/bip68.json @@ -0,0 +1,67 @@ +[ + { + "description": "0x00400000 (00000000010000000000000000000000)", + "seconds": 0, + "sequence": 4194304 + }, + { + "description": "0x00400001 (00000000010000000000000000000001)", + "seconds": 512, + "sequence": 4194305 + }, + { + "description": "0x00400002 (00000000010000000000000000000010)", + "seconds": 1024, + "sequence": 4194306 + }, + { + "description": "0x00400003 (00000000010000000000000000000011)", + "seconds": 1536, + "sequence": 4194307 + }, + { + "description": "0x00400004 (00000000010000000000000000000100)", + "seconds": 2048, + "sequence": 4194308 + }, + { + "description": "0x00400005 (00000000010000000000000000000101)", + "seconds": 2560, + "sequence": 4194309 + }, + { + "description": "0x00400006 (00000000010000000000000000000110)", + "seconds": 3072, + "sequence": 4194310 + }, + { + "description": "0x00400007 (00000000010000000000000000000111)", + "seconds": 3584, + "sequence": 4194311 + }, + { + "description": "0x00400008 (00000000010000000000000000001000)", + "seconds": 4096, + "sequence": 4194312 + }, + { + "description": "0x00400009 (00000000010000000000000000001001)", + "seconds": 4608, + "sequence": 4194313 + }, + { + "description": "0x0040000a (00000000010000000000000000001010)", + "seconds": 5120, + "sequence": 4194314 + }, + { + "description": "0x0040000b (00000000010000000000000000001011)", + "seconds": 5632, + "sequence": 4194315 + }, + { + "description": "0x0040000c (00000000010000000000000000001100)", + "seconds": 6144, + "sequence": 4194316 + } +] \ No newline at end of file diff --git a/pkg/ark-sdk/go.mod b/pkg/ark-sdk/go.mod index fba7994..60866dd 100644 --- a/pkg/ark-sdk/go.mod +++ b/pkg/ark-sdk/go.mod @@ -5,4 +5,11 @@ go 1.21.0 require ( github.com/btcsuite/btcd/btcutil v1.1.3 github.com/decred/dcrd/dcrec/secp256k1/v4 v4.2.0 + github.com/stretchr/testify v1.7.0 +) + +require ( + github.com/davecgh/go-spew v1.1.1 // indirect + github.com/pmezard/go-difflib v1.0.0 // indirect + gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c // indirect ) diff --git a/pkg/ark-sdk/go.sum b/pkg/ark-sdk/go.sum index 0c90e22..bc4dd00 100644 --- a/pkg/ark-sdk/go.sum +++ b/pkg/ark-sdk/go.sum @@ -21,6 +21,7 @@ github.com/btcsuite/websocket v0.0.0-20150119174127-31079b680792/go.mod h1:ghJtE github.com/btcsuite/winsvc v1.0.0/go.mod h1:jsenWakMcC0zFBFurPLEAyrnc/teJEM1O46fmI40EZs= github.com/davecgh/go-spew v0.0.0-20171005155431-ecdeabc65495/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/decred/dcrd/crypto/blake256 v1.0.0/go.mod h1:sQl2p6Y26YV+ZOcSTP6thNdn47hh8kt6rqSlvmrXFAc= github.com/decred/dcrd/dcrec/secp256k1/v4 v4.0.1/go.mod h1:hyedUtir6IdtD/7lIxGeCxkaw7y45JueMRL4DIyJDKs= @@ -54,8 +55,10 @@ github.com/onsi/gomega v1.4.1/go.mod h1:C1qb7wdrVGGVU+Z6iS04AVkA3Q65CEZX59MT0QO5 github.com/onsi/gomega v1.4.3/go.mod h1:ex+gbHU/CVuBBDIJjb2X0qEXbFg53c61hWP/1CpauHY= github.com/onsi/gomega v1.7.1/go.mod h1:XdKZgCCFLUoM/7CFJVPcG8C1xQ1AJ0vpAezJrB7JYyY= github.com/onsi/gomega v1.10.1/go.mod h1:iN09h71vgCQne3DLsj+A5owkum+a2tYe+TOCB1ybHNo= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY= github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/syndtr/goleveldb v1.0.1-0.20210819022825-2ae1ddf74ef7/go.mod h1:q4W45IWZaF22tdD+VEXcAWRA037jwmWEB5VWYORlTpc= golang.org/x/crypto v0.0.0-20170930174604-9419663f5a44/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4= @@ -88,10 +91,12 @@ google.golang.org/protobuf v0.0.0-20200228230310-ab0ca4ff8a60/go.mod h1:cfTl7dwQ google.golang.org/protobuf v1.20.1-0.20200309200217-e05f789c0967/go.mod h1:A+miEFZTKqfCUM6K7xSMQL9OKL/b6hQv+e19PK+JZNE= google.golang.org/protobuf v1.21.0/go.mod h1:47Nbq4nVaFHyn7ilMalzfO3qCViNmqZ2kzikPIcrTAo= google.golang.org/protobuf v1.23.0/go.mod h1:EGpADcykh3NcUnDUJcl1+ZksZNG86OlYog2l/sGQquU= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/fsnotify.v1 v1.4.7/go.mod h1:Tz8NjZHkW78fSQdbUxIjBTcgA1z1m8ZHf0WmKUhAMys= gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7/go.mod h1:dt/ZhP58zS4L8KSrWDmTeBkI65Dw0HsyUHuEVlX15mw= gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.3.0/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=