mirror of
https://github.com/aljazceru/ark.git
synced 2026-01-31 00:54:48 +01:00
Add BIP68 encoding/decoding (#2)
* add BIP68 func * bip68_test.go: use require assertions * add DecodeBIP68 func
This commit is contained in:
58
pkg/ark-sdk/bip68.go
Normal file
58
pkg/ark-sdk/bip68.go
Normal file
@@ -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")
|
||||
}
|
||||
43
pkg/ark-sdk/bip68_test.go
Normal file
43
pkg/ark-sdk/bip68_test.go
Normal file
@@ -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)
|
||||
})
|
||||
}
|
||||
}
|
||||
67
pkg/ark-sdk/fixtures/bip68.json
Normal file
67
pkg/ark-sdk/fixtures/bip68.json
Normal file
@@ -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
|
||||
}
|
||||
]
|
||||
@@ -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
|
||||
)
|
||||
|
||||
@@ -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=
|
||||
|
||||
Reference in New Issue
Block a user