Skip to content

Commit

Permalink
[lib] refs #992 - Update SKY_cli_CreateTxFromWallet to deal with wall…
Browse files Browse the repository at this point in the history
…et encryption
  • Loading branch information
olemis committed May 6, 2018
1 parent faad646 commit 50e60bd
Show file tree
Hide file tree
Showing 6 changed files with 92 additions and 28 deletions.
6 changes: 6 additions & 0 deletions include/skytypes.h
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,12 @@ typedef struct {
*/
typedef GoInt64_ Handle;

/**
* Memory handle for internal object retrieving password to read
* encrypted wallets.
*/
typedef Handle PasswordReader__Handle;

#include "cipher.hash.go.h"
#include "cipher.crypto.go.h"
#include "cipher.address.go.h"
Expand Down
20 changes: 20 additions & 0 deletions lib/cgo/api.cli.cli.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package main

/*
#include <string.h>
#include <stdlib.h>
#include "skytypes.h"
*/
import "C"

import (
cli "github.com/skycoin/skycoin/src/api/cli"
)

//export SKY_cli_NewPasswordReader
func SKY_cli_NewPasswordReader(_p []byte) C.PasswordReader__Handle {
pr := cli.NewPasswordReader(_p)
return registerPasswordReaderHandle(pr)
}
14 changes: 9 additions & 5 deletions lib/cgo/api.cli.create_rawtx.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,21 +21,25 @@ import (
*/

//export SKY_cli_CreateRawTxFromWallet
func SKY_cli_CreateRawTxFromWallet(_ctx C.Handle, _walletFile, _chgAddr string, _toAddrs []C.cli__SendAmount, _tx *C.coin__Transaction) uint32 {
func SKY_cli_CreateRawTxFromWallet(_ctx C.Handle, _walletFile, _chgAddr string, _toAddrs []C.cli__SendAmount, _pr C.PasswordReader__Handle, _tx *C.coin__Transaction) uint32 {
// TODO: Instantiate _ctx . Not used in cli function
toAddrs := (*[]cli.SendAmount)(unsafe.Pointer(&_toAddrs))
tx, err := cli.CreateRawTxFromWallet(nil, _walletFile, _chgAddr, *toAddrs)
pr, isOk := lookupPasswordReaderHandle(_pr)
if !isOk {
return SKY_ERROR
}
tx, err := cli.CreateRawTxFromWallet(nil, _walletFile, _chgAddr, *toAddrs, pr)
*_tx = *(*C.coin__Transaction)(unsafe.Pointer(&tx))
if err != nil {
return 1
return SKY_ERROR
}
return 0
return SKY_OK
}

//export SKY_cli_CreateRawTxFromAddress
func SKY_cli_CreateRawTxFromAddress(_ctx C.Handle, _addr, _walletFile, _chgAddr string, _toAddrs []C.cli__SendAmount, _tx *C.coin__Transaction) uint32 {
// TODO: Implement
return 0
return SKY_ERROR
}

//export SKY_cli_CreateRawTx
Expand Down
2 changes: 1 addition & 1 deletion lib/cgo/libsky_error.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package main

const (
SKY_OK = 0
SKY_ERROR = 0xFFFFFFFF
SKY_OK = 0
)

func libErrorCode(err error) uint32 {
Expand Down
56 changes: 56 additions & 0 deletions lib/cgo/libsky_handle.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package main

/*
#include <string.h>
#include <stdlib.h>
#include "skytypes.h"
*/
import "C"

import (
cli "github.com/skycoin/skycoin/src/api/cli"
"unsafe"
)

type Handle uint64

var (
handleMap = make(map[Handle]interface{})
)

func registerHandle(obj interface{}) Handle {
ptr := &obj
handle := *(*Handle)(unsafe.Pointer(&ptr))
handleMap[handle] = obj
return handle
}

func lookupHandleObj(handle Handle) (interface{}, bool) {
obj, ok := handleMap[handle]
return obj, ok
}

func registerPasswordReaderHandle(obj cli.PasswordReader) C.PasswordReader__Handle {
return (C.PasswordReader__Handle)(registerHandle(obj))
}

func lookupPasswordReaderHandle(handle C.PasswordReader__Handle) (cli.PasswordReader, bool) {
obj, ok := lookupHandleObj(Handle(handle))
if ok {
if obj, isOK := (obj).(cli.PasswordReader); isOK {
return obj, true
}
}
return nil, false
}

func closeHandle(handle Handle) {
delete(handleMap, handle)
}

//export SKY_handle_close
func SKY_handle_close(handle C.Handle) {
closeHandle(Handle(handle))
}
22 changes: 0 additions & 22 deletions lib/cgo/libsky_mem.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,28 +36,6 @@ const (
SizeofUxBalance = unsafe.Sizeof(C.wallet__UxBalance{})
)

type Handle uint64

var (
handleMap = make(map[Handle]interface{})
)

func openHandle(obj interface{}) Handle {
ptr := &obj
handle := *(*Handle)(unsafe.Pointer(&ptr))
handleMap[handle] = obj
return handle
}

func lookupHandleObj(handle Handle) (interface{}, bool) {
obj, ok := handleMap[handle]
return obj, ok
}

func closeHandle(handle Handle) {
delete(handleMap, handle)
}

/**
* Inplace memory references
*/
Expand Down

0 comments on commit 50e60bd

Please sign in to comment.