Skip to content

Commit e221f83

Browse files
authored
Add BIP68 encoding/decoding (#2)
* add BIP68 func * bip68_test.go: use require assertions * add DecodeBIP68 func
1 parent d761068 commit e221f83

File tree

6 files changed

+184
-1
lines changed

6 files changed

+184
-1
lines changed

.gitignore

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,7 @@ DS_Store
1818
._.DS_Store
1919
**/.DS_Store
2020
**/._.DS_Store
21-
21+
22+
23+
go.work
24+
go.work.sum

pkg/ark-sdk/bip68.go

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package sdk
2+
3+
import (
4+
"encoding/hex"
5+
"fmt"
6+
)
7+
8+
const (
9+
SEQUENCE_LOCKTIME_MASK = 0x0000ffff
10+
SEQUENCE_LOCKTIME_TYPE_FLAG = 1 << 22
11+
SEQUENCE_LOCKTIME_GRANULARITY = 9
12+
SECONDS_MOD = 1 << SEQUENCE_LOCKTIME_GRANULARITY
13+
SECONDS_MAX = SEQUENCE_LOCKTIME_MASK << SEQUENCE_LOCKTIME_GRANULARITY
14+
SEQUENCE_LOCKTIME_DISABLE_FLAG = 1 << 31
15+
)
16+
17+
func closerToModulo512(x uint) uint {
18+
return x - (x % 512)
19+
}
20+
21+
// BIP68 returns the encoded sequence locktime for the given number of seconds.
22+
func BIP68(seconds uint) ([]byte, error) {
23+
seconds = closerToModulo512(seconds)
24+
if seconds > SECONDS_MAX {
25+
return nil, fmt.Errorf("seconds too large, max is %d", SECONDS_MAX)
26+
}
27+
if seconds%SECONDS_MOD != 0 {
28+
return nil, fmt.Errorf("seconds must be a multiple of %d", SECONDS_MOD)
29+
}
30+
31+
asNumber := SEQUENCE_LOCKTIME_TYPE_FLAG | (seconds >> SEQUENCE_LOCKTIME_GRANULARITY)
32+
hexString := fmt.Sprintf("%x", asNumber)
33+
reversed, err := hex.DecodeString(hexString)
34+
if err != nil {
35+
return nil, err
36+
}
37+
for i, j := 0, len(reversed)-1; i < j; i, j = i+1, j-1 {
38+
reversed[i], reversed[j] = reversed[j], reversed[i]
39+
}
40+
return reversed, nil
41+
}
42+
43+
func DecodeBIP68(sequence []byte) (uint, error) {
44+
// sequence to int
45+
var asNumber int64
46+
for i := len(sequence) - 1; i >= 0; i-- {
47+
asNumber = asNumber<<8 | int64(sequence[i])
48+
}
49+
50+
if asNumber&SEQUENCE_LOCKTIME_DISABLE_FLAG != 0 {
51+
return 0, fmt.Errorf("sequence is disabled")
52+
}
53+
if asNumber&SEQUENCE_LOCKTIME_TYPE_FLAG != 0 {
54+
seconds := asNumber & SEQUENCE_LOCKTIME_MASK << SEQUENCE_LOCKTIME_GRANULARITY
55+
return uint(seconds), nil
56+
}
57+
return 0, fmt.Errorf("sequence is encoded as block number")
58+
}

pkg/ark-sdk/bip68_test.go

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package sdk_test
2+
3+
import (
4+
"encoding/json"
5+
"os"
6+
"testing"
7+
8+
sdk "github.com/ark-network/ark-sdk"
9+
"github.com/stretchr/testify/require"
10+
)
11+
12+
func TestBIP68(t *testing.T) {
13+
data, err := os.ReadFile("fixtures/bip68.json")
14+
require.NoError(t, err)
15+
16+
var testCases []struct {
17+
Input uint `json:"seconds"`
18+
Expected int64 `json:"sequence"`
19+
Desc string `json:"description"`
20+
}
21+
err = json.Unmarshal(data, &testCases)
22+
require.NoError(t, err)
23+
require.NotEmpty(t, testCases)
24+
25+
for _, tc := range testCases {
26+
t.Run(tc.Desc, func(t *testing.T) {
27+
actual, err := sdk.BIP68(tc.Input)
28+
require.NoError(t, err)
29+
30+
var asNumber int64
31+
for i := len(actual) - 1; i >= 0; i-- {
32+
asNumber = asNumber<<8 | int64(actual[i])
33+
}
34+
35+
require.Equal(t, tc.Expected, asNumber)
36+
37+
decoded, err := sdk.DecodeBIP68(actual)
38+
require.NoError(t, err)
39+
40+
require.Equal(t, tc.Input, decoded)
41+
})
42+
}
43+
}

pkg/ark-sdk/fixtures/bip68.json

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
[
2+
{
3+
"description": "0x00400000 (00000000010000000000000000000000)",
4+
"seconds": 0,
5+
"sequence": 4194304
6+
},
7+
{
8+
"description": "0x00400001 (00000000010000000000000000000001)",
9+
"seconds": 512,
10+
"sequence": 4194305
11+
},
12+
{
13+
"description": "0x00400002 (00000000010000000000000000000010)",
14+
"seconds": 1024,
15+
"sequence": 4194306
16+
},
17+
{
18+
"description": "0x00400003 (00000000010000000000000000000011)",
19+
"seconds": 1536,
20+
"sequence": 4194307
21+
},
22+
{
23+
"description": "0x00400004 (00000000010000000000000000000100)",
24+
"seconds": 2048,
25+
"sequence": 4194308
26+
},
27+
{
28+
"description": "0x00400005 (00000000010000000000000000000101)",
29+
"seconds": 2560,
30+
"sequence": 4194309
31+
},
32+
{
33+
"description": "0x00400006 (00000000010000000000000000000110)",
34+
"seconds": 3072,
35+
"sequence": 4194310
36+
},
37+
{
38+
"description": "0x00400007 (00000000010000000000000000000111)",
39+
"seconds": 3584,
40+
"sequence": 4194311
41+
},
42+
{
43+
"description": "0x00400008 (00000000010000000000000000001000)",
44+
"seconds": 4096,
45+
"sequence": 4194312
46+
},
47+
{
48+
"description": "0x00400009 (00000000010000000000000000001001)",
49+
"seconds": 4608,
50+
"sequence": 4194313
51+
},
52+
{
53+
"description": "0x0040000a (00000000010000000000000000001010)",
54+
"seconds": 5120,
55+
"sequence": 4194314
56+
},
57+
{
58+
"description": "0x0040000b (00000000010000000000000000001011)",
59+
"seconds": 5632,
60+
"sequence": 4194315
61+
},
62+
{
63+
"description": "0x0040000c (00000000010000000000000000001100)",
64+
"seconds": 6144,
65+
"sequence": 4194316
66+
}
67+
]

pkg/ark-sdk/go.mod

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,11 @@ go 1.21.0
55
require (
66
github.com/btcsuite/btcd/btcutil v1.1.3
77
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.2.0
8+
github.com/stretchr/testify v1.7.0
9+
)
10+
11+
require (
12+
github.com/davecgh/go-spew v1.1.1 // indirect
13+
github.com/pmezard/go-difflib v1.0.0 // indirect
14+
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c // indirect
815
)

pkg/ark-sdk/go.sum

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ github.com/btcsuite/websocket v0.0.0-20150119174127-31079b680792/go.mod h1:ghJtE
2121
github.com/btcsuite/winsvc v1.0.0/go.mod h1:jsenWakMcC0zFBFurPLEAyrnc/teJEM1O46fmI40EZs=
2222
github.com/davecgh/go-spew v0.0.0-20171005155431-ecdeabc65495/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
2323
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
24+
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
2425
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
2526
github.com/decred/dcrd/crypto/blake256 v1.0.0/go.mod h1:sQl2p6Y26YV+ZOcSTP6thNdn47hh8kt6rqSlvmrXFAc=
2627
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
5455
github.com/onsi/gomega v1.4.3/go.mod h1:ex+gbHU/CVuBBDIJjb2X0qEXbFg53c61hWP/1CpauHY=
5556
github.com/onsi/gomega v1.7.1/go.mod h1:XdKZgCCFLUoM/7CFJVPcG8C1xQ1AJ0vpAezJrB7JYyY=
5657
github.com/onsi/gomega v1.10.1/go.mod h1:iN09h71vgCQne3DLsj+A5owkum+a2tYe+TOCB1ybHNo=
58+
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
5759
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
5860
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
61+
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
5962
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
6063
github.com/syndtr/goleveldb v1.0.1-0.20210819022825-2ae1ddf74ef7/go.mod h1:q4W45IWZaF22tdD+VEXcAWRA037jwmWEB5VWYORlTpc=
6164
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
8891
google.golang.org/protobuf v1.20.1-0.20200309200217-e05f789c0967/go.mod h1:A+miEFZTKqfCUM6K7xSMQL9OKL/b6hQv+e19PK+JZNE=
8992
google.golang.org/protobuf v1.21.0/go.mod h1:47Nbq4nVaFHyn7ilMalzfO3qCViNmqZ2kzikPIcrTAo=
9093
google.golang.org/protobuf v1.23.0/go.mod h1:EGpADcykh3NcUnDUJcl1+ZksZNG86OlYog2l/sGQquU=
94+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
9195
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
9296
gopkg.in/fsnotify.v1 v1.4.7/go.mod h1:Tz8NjZHkW78fSQdbUxIjBTcgA1z1m8ZHf0WmKUhAMys=
9397
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7/go.mod h1:dt/ZhP58zS4L8KSrWDmTeBkI65Dw0HsyUHuEVlX15mw=
9498
gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
9599
gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
96100
gopkg.in/yaml.v2 v2.3.0/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
101+
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
97102
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

0 commit comments

Comments
 (0)