-
Notifications
You must be signed in to change notification settings - Fork 105
/
lib.go
59 lines (48 loc) · 1.9 KB
/
lib.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
// This file contains the part of the API that is exposed no matter if libwasmvm
// is available or not. Symbols from lib_libwasmvm.go are added conditionally.
package cosmwasm
import (
"bytes"
"crypto/sha256"
"fmt"
"github.com/CosmWasm/wasmvm/v2/types"
)
// Checksum represents a hash of the Wasm bytecode that serves as an ID. Must be generated from this library.
type Checksum = types.Checksum
// WasmCode is an alias for raw bytes of the wasm compiled code
type WasmCode []byte
// KVStore is a reference to some sub-kvstore that is valid for one instance of a code
type KVStore = types.KVStore
// GoAPI is a reference to some "precompiles", go callbacks
type GoAPI = types.GoAPI
// Querier lets us make read-only queries on other modules
type Querier = types.Querier
// GasMeter is a read-only version of the sdk gas meter
type GasMeter = types.GasMeter
// LibwasmvmVersion returns the version of the loaded library
// at runtime. This can be used for debugging to verify the loaded version
// matches the expected version.
//
// When cgo is disabled at build time, this returns an error at runtime.
func LibwasmvmVersion() (string, error) {
return libwasmvmVersionImpl()
}
// CreateChecksum performs the hashing of Wasm bytes to obtain the CosmWasm checksum.
//
// Ony Wasm blobs are allowed as inputs and a magic byte check will be performed
// to avoid accidental misusage.
func CreateChecksum(wasm []byte) (Checksum, error) {
if len(wasm) == 0 {
return Checksum{}, fmt.Errorf("Wasm bytes nil or empty")
}
if len(wasm) < 4 {
return Checksum{}, fmt.Errorf("Wasm bytes shorter than 4 bytes")
}
// magic number for Wasm is "\0asm"
// See https://webassembly.github.io/spec/core/binary/modules.html#binary-module
if !bytes.Equal(wasm[:4], []byte("\x00\x61\x73\x6D")) {
return Checksum{}, fmt.Errorf("Wasm bytes do not start with Wasm magic number")
}
hash := sha256.Sum256(wasm)
return Checksum(hash[:]), nil
}