Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature(sdk): Add support for signing v2 transactions #873

Merged
merged 1 commit into from
Jan 19, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 11 additions & 10 deletions sdk/encode.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,29 @@ import (
"bytes"
"syscall/js"

"go.sia.tech/core/rhp/v4"
proto4 "go.sia.tech/core/rhp/v4"
"go.sia.tech/core/types"
)

func encodeRPCRequest(data js.Value, req rhp.Request) result {
func encodeRPCRequest(rpcID types.Specifier, data js.Value, req proto4.Object) result {
if data.Type() != js.TypeUndefined {
if err := unmarshalStruct(data, &req); err != nil {
return resultErr(err)
}
}
buf := bytes.NewBuffer(nil)
if err := rhp.WriteRequest(buf, req); err != nil {
if err := proto4.WriteRequest(buf, rpcID, req); err != nil {
return resultErr(err)
}
return resultRPC(marshalUint8Array(buf.Bytes()))
}

func decodeRPCRequest(rpcJsData js.Value, res rhp.Request) result {
func decodeRPCRequest(rpcJsData js.Value, res proto4.Object) result {
rpcData, err := unmarshalUint8Array(rpcJsData)
if err != nil {
return resultErr(err)
}
err = rhp.ReadRequest(bytes.NewReader(rpcData), res)
err = proto4.ReadRequest(bytes.NewReader(rpcData), res)
if err != nil {
return resultErr(err)
}
Expand All @@ -36,25 +37,25 @@ func decodeRPCRequest(rpcJsData js.Value, res rhp.Request) result {
return resultData(d)
}

func encodeRPCResponse(data js.Value, req rhp.Object) result {
func encodeRPCResponse(data js.Value, obj proto4.Object) result {
if data.Type() != js.TypeUndefined {
if err := unmarshalStruct(data, &req); err != nil {
if err := unmarshalStruct(data, &obj); err != nil {
return resultErr(err)
}
}
buf := bytes.NewBuffer(nil)
if err := rhp.WriteResponse(buf, req); err != nil {
if err := proto4.WriteResponse(buf, obj); err != nil {
return resultErr(err)
}
return resultRPC(marshalUint8Array(buf.Bytes()))
}

func decodeRPCResponse(rpcJsData js.Value, res rhp.Object) result {
func decodeRPCResponse(rpcJsData js.Value, res proto4.Object) result {
rpcData, err := unmarshalUint8Array(rpcJsData)
if err != nil {
return resultErr(err)
}
err = rhp.ReadResponse(bytes.NewReader(rpcData), res)
err = proto4.ReadResponse(bytes.NewReader(rpcData), res)
if err != nil {
return resultErr(err)
}
Expand Down
19 changes: 10 additions & 9 deletions sdk/go.mod
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
module go.sia.tech/web/sdk

go 1.23.0
go 1.23.1

toolchain go1.23.3

require (
go.sia.tech/core v0.2.2-0.20240229154321-d97c1d5b2172
go.sia.tech/coreutils v0.0.3
go.sia.tech/core v0.9.1-0.20250106163018-8ec4de35d644
go.sia.tech/coreutils v0.9.0
)

require (
github.com/aead/chacha20 v0.0.0-20180709150244-8b13a72661da // indirect
go.uber.org/multierr v1.10.0 // indirect
go.uber.org/zap v1.26.0 // indirect
golang.org/x/crypto v0.0.0-20220507011949-2cf3adece122 // indirect
golang.org/x/sys v0.5.0 // indirect
lukechampine.com/frand v1.4.2 // indirect
go.uber.org/multierr v1.11.0 // indirect
go.uber.org/zap v1.27.0 // indirect
golang.org/x/crypto v0.31.0 // indirect
golang.org/x/sys v0.29.0 // indirect
lukechampine.com/frand v1.5.1 // indirect
)
39 changes: 20 additions & 19 deletions sdk/go.sum
Original file line number Diff line number Diff line change
@@ -1,27 +1,28 @@
github.com/aead/chacha20 v0.0.0-20180709150244-8b13a72661da h1:KjTM2ks9d14ZYCvmHS9iAKVt9AyzRSqNU1qabPih5BY=
github.com/aead/chacha20 v0.0.0-20180709150244-8b13a72661da/go.mod h1:eHEWzANqSiWQsof+nXEI9bUVUyV6F53Fp89EuCh2EAA=
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/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/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk=
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
go.sia.tech/core v0.2.2-0.20240229154321-d97c1d5b2172 h1:uET7VyK5mz02bsicyNeoEut/RarvmQXAvXgUqZ1YJoE=
go.sia.tech/core v0.2.2-0.20240229154321-d97c1d5b2172/go.mod h1:3EoY+rR78w1/uGoXXVqcYdwSjSJKuEMI5bL7WROA27Q=
go.sia.tech/coreutils v0.0.3 h1:ZxuzovRpQMvfy/pCOV4om1cPF6sE15GyJyK36kIrF1Y=
go.sia.tech/coreutils v0.0.3/go.mod h1:UBFc77wXiE//eyilO5HLOncIEj7F69j0Nv2OkFujtP0=
go.uber.org/goleak v1.2.0 h1:xqgm/S+aQvhWFTtR0XK3Jvg7z8kGV8P4X14IzwN3Eqk=
go.uber.org/goleak v1.2.0/go.mod h1:XJYK+MuIchqpmGmUSAzotztawfKvYLUIgg7guXrwVUo=
go.uber.org/multierr v1.10.0 h1:S0h4aNzvfcFsC3dRF1jLoaov7oRaKqRGC/pUEJ2yvPQ=
go.uber.org/multierr v1.10.0/go.mod h1:20+QtiLqy0Nd6FdQB9TLXag12DsQkrbs3htMFfDN80Y=
go.uber.org/zap v1.26.0 h1:sI7k6L95XOKS281NhVKOFCUNIvv9e0w4BF8N3u+tCRo=
go.uber.org/zap v1.26.0/go.mod h1:dtElttAiwGvoJ/vj4IwHBS/gXsEu/pZ50mUIRWuG0so=
golang.org/x/crypto v0.0.0-20220507011949-2cf3adece122 h1:NvGWuYG8dkDHFSKksI1P9faiVJ9rayE6l0+ouWVIDs8=
golang.org/x/crypto v0.0.0-20220507011949-2cf3adece122/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4=
golang.org/x/sys v0.0.0-20190626221950-04f50cda93cb/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.5.0 h1:MUK/U/4lj1t1oPg0HfuXDN/Z1wv31ZJ/YcPiGccS4DU=
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
go.etcd.io/bbolt v1.3.11 h1:yGEzV1wPz2yVCLsD8ZAiGHhHVlczyC9d1rP43/VCRJ0=
go.etcd.io/bbolt v1.3.11/go.mod h1:dksAq7YMXoljX0xu6VF5DMZGbhYYoLUalEiSySYAS4I=
go.sia.tech/core v0.9.1-0.20250106163018-8ec4de35d644 h1:UXDMLhwPZzJJFGqG0pzACaOex9LZs432TcsuoIp6wpU=
go.sia.tech/core v0.9.1-0.20250106163018-8ec4de35d644/go.mod h1:BV9XKOoP8Ux2ctiyoDExvdWc21rxSJAuFQ0Yr5Baiqk=
go.sia.tech/coreutils v0.9.0 h1:5cnK0RtHOyErGhcmNkmCdEKeuj1tECwO9PYbErEbpDQ=
go.sia.tech/coreutils v0.9.0/go.mod h1:KFq1q5/YbPH6ZSWtXCxA1bRhBF5Zgcj8G3Wvu0jr/BA=
go.sia.tech/mux v1.3.0 h1:hgR34IEkqvfBKUJkAzGi31OADeW2y7D6Bmy/Jcbop9c=
go.sia.tech/mux v1.3.0/go.mod h1:I46++RD4beqA3cW9Xm9SwXbezwPqLvHhVs9HLpDtt58=
go.uber.org/goleak v1.3.0 h1:2K3zAYmnTNqV73imy9J1T3WC+gmCePx2hEGkimedGto=
go.uber.org/goleak v1.3.0/go.mod h1:CoHD4mav9JJNrW/WLlf7HGZPjdw8EucARQHekz1X6bE=
go.uber.org/multierr v1.11.0 h1:blXXJkSxSSfBVBlC76pxqeO+LN3aDfLQo+309xJstO0=
go.uber.org/multierr v1.11.0/go.mod h1:20+QtiLqy0Nd6FdQB9TLXag12DsQkrbs3htMFfDN80Y=
go.uber.org/zap v1.27.0 h1:aJMhYGrd5QSmlpLMr2MftRKl7t8J8PTZPA732ud/XR8=
go.uber.org/zap v1.27.0/go.mod h1:GB2qFLM7cTU87MWRP2mPIjqfIDnGu+VIO4V/SdhGo2E=
golang.org/x/crypto v0.31.0 h1:ihbySMvVjLAeSH1IbfcRTkD/iNscyz8rGzjF/E5hV6U=
golang.org/x/crypto v0.31.0/go.mod h1:kDsLvtWBEx7MV9tJOj9bnXsPbxwJQ6csT/x4KIN4Ssk=
golang.org/x/sys v0.29.0 h1:TPYlXGxvx1MGTn2GiZDhnjPA9wZzZeGKHHmKhHYvgaU=
golang.org/x/sys v0.29.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
lukechampine.com/frand v1.4.2 h1:RzFIpOvkMXuPMBb9maa4ND4wjBn71E1Jpf8BzJHMaVw=
lukechampine.com/frand v1.4.2/go.mod h1:4S/TM2ZgrKejMcKMbeLjISpJMO+/eZ1zu3vYX9dtj3s=
lukechampine.com/frand v1.5.1 h1:fg0eRtdmGFIxhP5zQJzM1lFDbD6CUfu/f+7WgAZd5/w=
lukechampine.com/frand v1.5.1/go.mod h1:4VstaWc2plN4Mjr10chUD46RAVGWhpkZ5Nja8+Azp0Q=
2 changes: 2 additions & 0 deletions sdk/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ func main() {
"encodeTransaction": jsFunc(encodeTransaction),
"transactionId": jsFunc(transactionID),
"signTransactionV1": jsFunc(signTransactionV1),
"v2TransactionInputSigHash": jsFunc(v2TransactionInputSigHash),
"signHash": jsFunc(signHash),
},
})
c := make(chan bool, 1)
Expand Down
34 changes: 17 additions & 17 deletions sdk/rhp.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@ import (
"encoding/hex"
"syscall/js"

"go.sia.tech/core/rhp/v4"
proto4 "go.sia.tech/core/rhp/v4"
)

func generateAccount(this js.Value, args []js.Value) result {
if err := checkArgs(args); err != nil {
return resultErr(err)
}

pk, a := rhp.GenerateAccount()
pk, a := proto4.GenerateAccount()

privateKey := hex.EncodeToString(pk)
account := hex.EncodeToString(a[:])
Expand All @@ -29,31 +29,31 @@ func encodeSettingsRequest(this js.Value, args []js.Value) result {
if err := checkArgs(args); err != nil {
return resultErr(err)
}
var r rhp.RPCSettingsRequest
return encodeRPCRequest(js.Undefined(), &r)
var r proto4.RPCSettingsRequest
return encodeRPCRequest(proto4.RPCSettingsID, js.Undefined(), &r)
}

func decodeSettingsRequest(this js.Value, args []js.Value) result {
if err := checkArgs(args, js.TypeObject); err != nil {
return resultErr(err)
}
var r rhp.RPCSettingsRequest
var r proto4.RPCSettingsRequest
return decodeRPCRequest(args[0], &r)
}

func encodeSettingsResponse(this js.Value, args []js.Value) result {
if err := checkArgs(args, js.TypeObject); err != nil {
return resultErr(err)
}
var r rhp.RPCSettingsResponse
var r proto4.RPCSettingsResponse
return encodeRPCResponse(args[0], &r)
}

func decodeSettingsResponse(this js.Value, args []js.Value) result {
if err := checkArgs(args, js.TypeObject); err != nil {
return resultErr(err)
}
var r rhp.RPCSettingsResponse
var r proto4.RPCSettingsResponse
return decodeRPCResponse(args[0], &r)
}

Expand All @@ -64,31 +64,31 @@ func encodeReadSectorRequest(this js.Value, args []js.Value) result {
return resultErr(err)
}

var r rhp.RPCReadSectorRequest
return encodeRPCRequest(args[0], &r)
var r proto4.RPCReadSectorRequest
return encodeRPCRequest(proto4.RPCReadSectorID, args[0], &r)
}

func decodeReadSectorRequest(this js.Value, args []js.Value) result {
if err := checkArgs(args, js.TypeObject); err != nil {
return resultErr(err)
}
var r rhp.RPCReadSectorRequest
var r proto4.RPCReadSectorRequest
return decodeRPCRequest(args[0], &r)
}

func encodeReadSectorResponse(this js.Value, args []js.Value) result {
if err := checkArgs(args, js.TypeObject); err != nil {
return resultErr(err)
}
var r rhp.RPCReadSectorResponse
var r proto4.RPCReadSectorResponse
return encodeRPCResponse(args[0], &r)
}

func decodeReadSectorResponse(this js.Value, args []js.Value) result {
if err := checkArgs(args, js.TypeObject); err != nil {
return resultErr(err)
}
var r rhp.RPCReadSectorResponse
var r proto4.RPCReadSectorResponse
return decodeRPCResponse(args[0], &r)
}

Expand All @@ -98,15 +98,15 @@ func encodeWriteSectorRequest(this js.Value, args []js.Value) result {
if err := checkArgs(args, js.TypeObject); err != nil {
return resultErr(err)
}
var r rhp.RPCWriteSectorRequest
return encodeRPCRequest(args[0], &r)
var r proto4.RPCWriteSectorRequest
return encodeRPCRequest(proto4.RPCWriteSectorID, args[0], &r)
}

func decodeWriteSectorRequest(this js.Value, args []js.Value) result {
if err := checkArgs(args, js.TypeObject); err != nil {
return resultErr(err)
}
var r rhp.RPCWriteSectorRequest
var r proto4.RPCWriteSectorRequest
return decodeRPCRequest(args[0], &r)
}

Expand All @@ -115,14 +115,14 @@ func encodeWriteSectorResponse(this js.Value, args []js.Value) result {
return resultErr(err)
}

var r rhp.RPCWriteSectorResponse
var r proto4.RPCWriteSectorResponse
return encodeRPCResponse(args[0], &r)
}

func decodeWriteSectorResponse(this js.Value, args []js.Value) result {
if err := checkArgs(args, js.TypeObject); err != nil {
return resultErr(err)
}
var r rhp.RPCWriteSectorResponse
var r proto4.RPCWriteSectorResponse
return decodeRPCResponse(args[0], &r)
}
Loading
Loading