-
-
Save christian-rogobete/1b8d2506f270938de1d4b3b0aefc7dce to your computer and use it in GitHub Desktop.
Soroban add meta and contract spec sections for add
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"bytes" | |
"fmt" | |
"log" | |
"os" | |
"github.com/tetratelabs/wabin/binary" | |
"github.com/tetratelabs/wabin/wasm" | |
"soneso.com/xdr" | |
) | |
const META_SECTION_NAME = "contractenvmetav0" | |
const SPEC_SECTION_NAME = "contractspecv0" | |
func main() { | |
if len(os.Args) < 2 { | |
log.Println("usage: wasmprepare <wasm file>") | |
return | |
} | |
filename := os.Args[1] | |
bin, err := os.ReadFile(filename) | |
if err != nil { | |
log.Panicln(err) | |
} | |
mod, err := binary.DecodeModule(bin, wasm.CoreFeaturesV2) | |
if err != nil { | |
log.Panicln(err) | |
} | |
xdrMeta := xdr.ScEnvMetaEntry{} | |
xdrMeta.Kind = xdr.ScEnvMetaKind(xdr.ScEnvMetaKindScEnvMetaKindInterfaceVersion) | |
var v xdr.Uint64 = 27 | |
xdrMeta.InterfaceVersion = &v | |
var metaBytes bytes.Buffer | |
_, err = xdr.Marshal(&metaBytes, xdrMeta) | |
if err != nil { | |
fmt.Println("error generating meta xdr") | |
} | |
xdrArg1 := xdr.ScSpecFunctionInputV0{} | |
xdrArg1.Name = "x" | |
xdrArg1.Type = xdr.ScSpecTypeDef{} | |
xdrArg1.Type.Type = xdr.ScSpecTypeScSpecTypeU32 | |
xdrArg2 := xdr.ScSpecFunctionInputV0{} | |
xdrArg2.Name = "y" | |
xdrArg2.Type = xdr.ScSpecTypeDef{} | |
xdrArg2.Type.Type = xdr.ScSpecTypeScSpecTypeU32 | |
xdrRet := xdr.ScSpecTypeDef{} | |
xdrRet.Type = xdr.ScSpecTypeScSpecTypeU32 | |
xdrFunc := xdr.ScSpecFunctionV0{} | |
xdrFunc.Name = xdr.ScSymbol("add") | |
xdrFunc.Inputs = []xdr.ScSpecFunctionInputV0{xdrArg1, xdrArg2} | |
xdrFunc.Outputs = []xdr.ScSpecTypeDef{xdrRet} | |
xdrFuncEntry := xdr.ScSpecEntry{} | |
xdrFuncEntry.Kind = xdr.ScSpecEntryKind(xdr.ScSpecEntryKindScSpecEntryFunctionV0) | |
xdrFuncEntry.FunctionV0 = &xdrFunc | |
var specBytes bytes.Buffer | |
_, err = xdr.Marshal(&specBytes, xdrFuncEntry) | |
if err != nil { | |
fmt.Println("error generating spec xdr") | |
} | |
var meta *wasm.CustomSection | |
var spec *wasm.CustomSection | |
for _, custom := range mod.CustomSections { | |
if custom.Name == META_SECTION_NAME { | |
meta = custom | |
} else if custom.Name == SPEC_SECTION_NAME { | |
spec = custom | |
} | |
} | |
changed := false | |
if meta == nil { | |
meta = &wasm.CustomSection{ | |
Name: META_SECTION_NAME, | |
Data: metaBytes.Bytes(), | |
} | |
mod.CustomSections = append(mod.CustomSections, meta) | |
changed = true | |
fmt.Printf("Added %s section\n", META_SECTION_NAME) | |
} else { | |
fmt.Println("meta already present") | |
} | |
if spec == nil { | |
spec = &wasm.CustomSection{ | |
Name: SPEC_SECTION_NAME, | |
Data: specBytes.Bytes(), | |
} | |
mod.CustomSections = append(mod.CustomSections, spec) | |
changed = true | |
fmt.Printf("Added %s section\n", SPEC_SECTION_NAME) | |
} else { | |
fmt.Println("spec already present") | |
} | |
if changed { | |
bin = binary.EncodeModule(mod) | |
if err = os.WriteFile(filename, bin, 0644); err != nil { | |
log.Panicln(err) | |
} | |
} | |
fmt.Println("") | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment