Skip to content

Commit

Permalink
add golang sign
Browse files Browse the repository at this point in the history
  • Loading branch information
ququzone committed Jan 16, 2023
1 parent 820eb57 commit 5a8ac57
Show file tree
Hide file tree
Showing 8 changed files with 82 additions and 4 deletions.
1 change: 1 addition & 0 deletions 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 ctss/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ version = "0.1.0"
edition = "2021"

[dependencies]
hex = "0.4.3"
tss-lib = { path = "../tss" }

[lib]
Expand Down
41 changes: 37 additions & 4 deletions ctss/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@

use std::os::raw::c_char;
use std::ffi::CStr;
use std::ffi::CString;
use std::os::raw::c_char;

use tss_lib::keygen;
use tss_lib::{keygen, sign};

pub fn parse_string(s: *const c_char) -> &'static str {
let s = unsafe {
Expand All @@ -27,8 +26,42 @@ pub extern "C" fn keygen(
index,
threshold,
number_of_parties,
).unwrap();
)
.unwrap();

let data = CString::new(data).unwrap();
data.into_raw()
}

#[no_mangle]
pub extern "C" fn sign(
server_url: *const c_char,
room: *const c_char,
local_share: *const c_char,
parties: *const c_char,
data: *const c_char,
) -> *const c_char {
let parties = parse_string(parties)
.split(",")
.map(|c| c.trim().parse::<u16>().unwrap())
.collect();

let signature = sign::run(
parse_string(server_url),
parse_string(room),
parse_string(local_share),
parties,
parse_string(data).as_bytes(),
)
.unwrap();

let signature = format!(
r#"{{ "r":"0x{}", "s":"0x{}", "v":"{}" }}"#,
hex::encode(signature.r.to_bytes().as_ref()),
hex::encode(signature.s.to_bytes().as_ref()),
signature.recid,
);

let signature = CString::new(signature).unwrap();
signature.into_raw()
}
1 change: 1 addition & 0 deletions examples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ ctss bind examples

```
go build -ldflags="-r lib" keygen.go
go build -ldflags="-r lib" sign.go
```
1 change: 1 addition & 0 deletions examples/golang/.gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
/keygen
/sign
/local-share*.json
6 changes: 6 additions & 0 deletions examples/golang/lib/ctss.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,9 @@ const char *keygen(const char *server_url,
uint16_t index,
uint16_t threshold,
uint16_t number_of_parties);

const char *sign(const char *server_url,
const char *room,
const char *local_share,
const char *parties,
const char *data);
Binary file modified examples/golang/lib/libctss.dylib
Binary file not shown.
35 changes: 35 additions & 0 deletions examples/golang/sign.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package main

/*
#cgo LDFLAGS: -L./lib -lctss
#include "./lib/ctss.h"
*/
import "C"
import (
"fmt"
"log"
"strconv"
)

func main() {
fmt.Println("input index:")
var indexStr string
_, err := fmt.Scanln(&indexStr)
if err != nil {
log.Fatalln(err)
}

index, err := strconv.Atoi(indexStr)
if err != nil {
log.Fatalln(err)
}
signature := C.sign(
C.CString("http://localhost:8000/"),
C.CString("default-keygen"),
C.CString(fmt.Sprintf("local-share%d.json", index)),
C.CString("1,2"),
C.CString("hello"),
)

fmt.Println(C.GoString(signature))
}

0 comments on commit 5a8ac57

Please sign in to comment.