Skip to content
Open
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
7 changes: 7 additions & 0 deletions contracts/bidding_contract/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions contracts/bidding_contract/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ crate-type = ["cdylib", "rlib"]
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
rubixwasm-std = { path = "../../packages/std" }
hex = "0.4"
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
afford belt west ripple help receive example scatter prefer caught planet until shy helmet below seven cage draw current faint mix minimum screen enrich
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
-----BEGIN PUBLIC KEY-----
BCsMqnOyw0v4UtYE6ii6TdbA3lwpGKlQWAkcliAGnsYiiTLqk5DDJLPzfxKSpcsr
WFnL9l0fg5lO8bWdcfzxZ/8=
-----END PUBLIC KEY-----
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
-----BEGIN ENCRYPTED PRIVATE KEY-----
eXBS4SjJK2KeZ76kzq+guSPkPlOF4JIafHcsHbEZtdu/BVytdt+ZBTh03hH5evXt
XMleSkMWe8iDndWb
-----END ENCRYPTED PRIVATE KEY-----
13 changes: 11 additions & 2 deletions contracts/bidding_contract/dapp/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,15 @@ module bidding-contract

go 1.21

require github.com/bytecodealliance/wasmtime-go v1.0.0
require (
github.com/bytecodealliance/wasmtime-go v1.0.0
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.3.0
github.com/ecies/go/v2 v2.0.9
github.com/rubixchain/rubixgoplatform v0.1.0
)

require github.com/rubixchain/rubix-wasm/go-wasm-bridge v0.0.0-20241021011146-a8b29487213e // indirect
require (
github.com/ethereum/go-ethereum v1.13.5 // indirect
github.com/rubixchain/rubix-wasm/go-wasm-bridge v0.1.2
golang.org/x/crypto v0.16.0 // indirect
)
2,354 changes: 2,353 additions & 1 deletion contracts/bidding_contract/dapp/go.sum

Large diffs are not rendered by default.

104 changes: 104 additions & 0 deletions contracts/bidding_contract/dapp/host/encrypt.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
package host

import (
"encoding/pem"
"fmt"
"log"
"math/big"
"os"
ecies "github.com/ecies/go/v2"
seal "github.com/rubixchain/rubixgoplatform/crypto"
secp256k1 "github.com/decred/dcrd/dcrec/secp256k1/v4"
)

func ConvertpubkeySecp256k1ToEcies(pubKey *secp256k1.PublicKey) (*ecies.PublicKey, error) {
// Extract the X and Y coordinates by calling the functions
x := pubKey.X()
y := pubKey.Y()

// Create an ECIES public key from the X and Y coordinates
eciesPubKey := &ecies.PublicKey{
X: x,
Y: y,
Curve: secp256k1.S256(),
}

return eciesPubKey, nil
}

// ConvertSecp256k1ToEcies converts a secp256k1 private key to an ECIES private key.
func ConvertSecp256k1privkeyToEcies(privKey *secp256k1.PrivateKey) (*ecies.PrivateKey, error) {
// Serialize the private key to get the private scalar bytes
privKeyBytes := privKey.Serialize()

// Convert the private scalar bytes to a big.Int
d := new(big.Int).SetBytes(privKeyBytes)
// Create an ECIES public key from the secp256k1 public key
pubKey := privKey.PubKey()
eciesPubKey := &ecies.PublicKey{
X: pubKey.X(),
Y: pubKey.Y(),
Curve: secp256k1.S256(),
}

// Create an ECIES private key from the D value and the ECIES public key
eciesPrivKey := &ecies.PrivateKey{
PublicKey: eciesPubKey,
D: d,
}

return eciesPrivKey, nil
}

func EciesEncryption(pubkey_path string, data []byte) (ciphertext []byte) {
read_pubKey, err := os.ReadFile(pubkey_path)
if err != nil {
log.Fatal(err)
}

pemdecoded_pubkey, _ := pem.Decode(read_pubKey)
pubkeyback, _ := secp256k1.ParsePubKey(pemdecoded_pubkey.Bytes)

eciesPubKey, err := ConvertpubkeySecp256k1ToEcies(pubkeyback)
if err != nil {
fmt.Println("Error converting public key:", err)
return
}

ciphertext, err = ecies.Encrypt(eciesPubKey, data)
if err != nil {
panic(err)
}
fmt.Println("ciphertext is ", ciphertext)
return ciphertext
}
func EciesDecryption(privkey_path string, encrypted_data []byte) (plaintext string, err error) {
read_encodedprivkey, err := os.ReadFile(privkey_path)
if err != nil {
return "", err
}

pemdecoded_privkey, _ := pem.Decode(read_encodedprivkey)
password := "mypassword"
unsealedprivkey, err := seal.UnSeal(password, (pemdecoded_privkey).Bytes)
if err != nil {
return "", fmt.Errorf("unable to decrypt the private key: %v", err)
}
fmt.Println("Decrypted Private key is ", unsealedprivkey)
parsedprivkey := secp256k1.PrivKeyFromBytes(unsealedprivkey)

ecies_privkey, err := ConvertSecp256k1privkeyToEcies(parsedprivkey)
if err != nil {
return "", fmt.Errorf("ConvertSecp256k1privkeyToEcies func failed: %v", err)
}

plaintext_bytes, err := ecies.Decrypt(ecies_privkey, encrypted_data)
if err != nil {
return "", fmt.Errorf("decrypt func failed: %v", err)
}

plaintext_string := string(plaintext_bytes)
return plaintext_string, nil
}


103 changes: 103 additions & 0 deletions contracts/bidding_contract/dapp/host/host_ecies_decryption.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
package host

import (
"encoding/json"
"fmt"

"github.com/bytecodealliance/wasmtime-go"

wasmbridge "github.com/rubixchain/rubix-wasm/go-wasm-bridge/host"
"github.com/rubixchain/rubix-wasm/go-wasm-bridge/utils"
)

type DecryptBid struct {
allocFunc *wasmtime.Func
memory *wasmtime.Memory
}
type BidderData struct {
Did string `json:"Did"`
Bid []byte `json:"bid_amount"`
}

func NewDecryptBid() *DecryptBid {
return &DecryptBid{}
}

func (h *DecryptBid) Name() string {
return "ecies_decryption"
}

func (h *DecryptBid) FuncType() *wasmtime.FuncType {
return wasmtime.NewFuncType(
[]*wasmtime.ValType{
wasmtime.NewValType(wasmtime.KindI32), // input_ptr
wasmtime.NewValType(wasmtime.KindI32), // input_len
wasmtime.NewValType(wasmtime.KindI32), // resp_ptr_ptr
wasmtime.NewValType(wasmtime.KindI32), // resp_len_ptr
},
[]*wasmtime.ValType{wasmtime.NewValType(wasmtime.KindI32)}, // return i32
)
}

func (h *DecryptBid) Initialize(allocFunc, deallocFunc *wasmtime.Func, memory *wasmtime.Memory, nodeAddress string, quorumType int) {
h.allocFunc = allocFunc
h.memory = memory
}

func (h *DecryptBid) Callback() wasmbridge.HostFunctionCallBack {
return h.callback
}
func (h *DecryptBid) callback(
caller *wasmtime.Caller,
args []wasmtime.Val,
) ([]wasmtime.Val, *wasmtime.Trap) {

inputArgs, outputArgs := utils.HostFunctionParamExtraction(args, true, true)

// Extract input bytes and convert to string
inputBytes, memory, err := utils.ExtractDataFromWASM(caller, inputArgs)
if err != nil {
fmt.Println("Failed to extract data from WASM", err)
return utils.HandleError(err.Error())
}
h.memory = memory

type DecryptionInputData struct {
Privatekey_path string `json:"Privatekey_path"`
Data []byte `json:"data"`
}

var contractInputMap DecryptionInputData
//Unmarshaling the data which has been read from the wasm memory
err3 := json.Unmarshal(inputBytes, &contractInputMap)
if err3 != nil {
fmt.Println("Error unmarshaling response in callback function:", err3)
return []wasmtime.Val{wasmtime.ValI32(1)}, wasmtime.NewTrap(fmt.Sprintf("Error unmarshaling response in callback function: %v", err3))
}

encryptedBid := contractInputMap.Data

fmt.Println("Message: ", contractInputMap)

decryptedBid, err := EciesDecryption("/home/rubix/Sai-Rubix/rubix-wasm/contracts/bidding_contract/bafybmihkhzcczetx43gzuraoemydxntloct6qb4jkix6xo26fv5jdefq3a/pvtKey.pem", encryptedBid)
if err != nil {
fmt.Println("err")
return []wasmtime.Val{wasmtime.ValI32(1)}, wasmtime.NewTrap(fmt.Sprintf("unable to get decrypted string: %v", err))
}

if len(decryptedBid) == 0 {
fmt.Println("Unable to get the decrypted Bid")
return []wasmtime.Val{wasmtime.ValI32(1)}, wasmtime.NewTrap("Unable to get the decrypted Bid")
}

responseStr := decryptedBid

err = utils.UpdateDataToWASM(caller, h.allocFunc, responseStr, outputArgs)
if err != nil {
fmt.Println("Failed to update data to WASM", err)
return utils.HandleError(err.Error())
}

return utils.HandleOk() // Success

}
118 changes: 0 additions & 118 deletions contracts/bidding_contract/dapp/host/host_get_bid_state.go

This file was deleted.

Loading