Commit 312fdf06 authored by zokyo-secured's avatar zokyo-secured
Browse files

finished with simple pay logic

parent 3ef5a047
{
"publicKeySet":[
{
"X": 16018165732954277624897009972361810774938160124376715463403951618377382386994,
"Y": 102395631593934565578467625282269208733157262581137750446478192491796004274979
},
{
"X": 30321070545248488399817466362260044602937803709853919585846660296849009833522,
"Y": 584349551486137423528370505680565301008648566037903166922050018102495266109
}
]
}
\ No newline at end of file
{
"L": 92111008636566153212032464762143563073542001160751363761712155655010845645112,
"aggregatedPublicKey": {
"X": 53273021753290677733958181036785342793909515636172542173452266111914559687918,
"Y": 80438816962432312406855120871023622040514830899308207189257144571508235291798
},
"R": {
"X": 39187551096853091697318624048264502756276174131861726900667247414843938291671,
"Y": 104858173701843540448383142892396261186943819363060132734804369366770619715086
},
"sigList":[
105689801978300580997942871504732446698008366627471895897196456699423211941014,
74443022546988139061598211812603205866830863595660865133631292810888798763966
]
}
\ No newline at end of file
{
"riSet":[
{
"X": 32138742690242662502241199693237900335204239496008641016955400178363039706333,
"Y": 98206281677669063585309234705818540728303839018966295776521001268753556134537
},
{
"X": 72664875838982340636391200513545812992442341844955961733021880193736610927495,
"Y": 72911874978211977551112918690838955657286613708826885971426767283825381825555
}
]
}
\ No newline at end of file
This diff is collapsed.
{
"sender": "0x1b17552bE3192810B80AE14B6CF8769D5dF9FF9e",
"senderPrivateKey": "88893ec39cb7725a46ac5bc44ee6e9642c5518ff507df5ca6ce37e622ed86d92",
"commitment": {
"X": 80896827534506630690547520492020251026855161557744096096567524235125637166228,
"Y": 35340021902447857874733116252574618597035402700515652153758858212804173617786
},
"amount": 10000,
"message": "message",
"publicKey": {
"X": 105012878946854625478151668840096389084932252989398520013599206687016371737084,
"Y": 109332001306514958330197135056171475144957829515243433050908948584468608042267
},
"R": {
"X": 50076431034714051257113545201217887633355089690556915100431086728944568565495,
"Y": 100534334696491181083477625115660637245645677532597488342701629369138874590038
},
"S":"45407579404865038949469822443889530360183727020693714560400763628627243928653"
}
\ No newline at end of file
{
"amount": 10000,
"recipientPubKey": {
"X": 53273021753290677733958181036785342793909515636172542173452266111914559687918,
"Y": 80438816962432312406855120871023622040514830899308207189257144571508235291798
},
"L": 92111008636566153212032464762143563073542001160751363761712155655010845645112
}
\ No newline at end of file
{
"publicKeySet":[
{
"X": 36930100774794457220439558941655221242025365988148096033938984509752240947363,
"Y": 107355372938895231341476848707268475852702849693587111228193612229669247067113
},
{
"X": 64803262188850657644951808823807838997767581964440567505420615647899043300678,
"Y": 63971737191177299002910363586526159730847480888556046747838445847595257452665
}
],
"markBool":[true, false]
}
\ No newline at end of file
{
"L": 6599458253638430530030989081222295280179915486349680107289106653719043473595,
"aggregatedPublicKey": {
"X": 36294734091650334299015255590564730774169557592558761594180532540875164254701,
"Y": 90715836293579651850115939187136704678019587655103823367190473516129207889293
},
"R": {
"X": 110192382215796476901070453284669594272297384093706425731628707323167171732594,
"Y": 54054991267547387766525693130050883083009923797212175854694435405586777560884
},
"sigList":[
2884036274461637530019643735204421459772558748675878266832983716997011408890,
108771513214745855611404821661821789198068705568541276521438575780169651853158
],
"markBool":[true, false]
}
\ No newline at end of file
{
"senderPrivateKey": "88893ec39cb7725a46ac5bc44ee6e9642c5518ff507df5ca6ce37e622ed86d92",
"spender": "0xC83476667bDcE8c859A444f4430cCBA14A786d1A",
"amount": 100000000000000000000
}
{
"riSet":[
{
"X": 55047947001688850541206122139597538792173466287048643696201173818919050753248,
"Y": 48271349426637424411339597823612687579527130691947287301526172630742354071418
},
{
"X": 30597522040258475983643942921995173199617490107718684840225655162066944250598,
"Y": 109679892184507445583508052942072381880458734989997944539754656652907671908836
}
],
"markBool":[true, false]
}
\ No newline at end of file
{
"senderPrivateKey": "88893ec39cb7725a46ac5bc44ee6e9642c5518ff507df5ca6ce37e622ed86d92",
"commitment": {
"X": 30827625733040073096485335692492917589533718913918351627457957153885537129254,
"Y": 108126380256976004157050699991932453929873564986017810392507488857532410091051
},
"amount": 500000000000000000,
"message": "1234",
"publicKey": {
"X": 36930100774794457220439558941655221242025365988148096033938984509752240947363,
"Y": 107355372938895231341476848707268475852702849693587111228193612229669247067113
},
"R": {
"X": 99032024504366114089069064720559526954716207773392460061061508156961667557771,
"Y": 89139262429803275016956186088593858266879957029022117416205588637118906870181
},
"S":"29250001288494152486434678654766083121865114917755429556989368331635088161184"
}
\ No newline at end of file
{
"amount": 500000000000000000,
"recipientPubKey": {
"X": 36930100774794457220439558941655221242025365988148096033938984509752240947363,
"Y": 107355372938895231341476848707268475852702849693587111228193612229669247067113
}
}
\ No newline at end of file
{
"recipient": "0xF23708F1a53C5d2904358EA910377Ef1bad235a5"
}
{
"message":"1234",
"Ri": {
"X": 30597522040258475983643942921995173199617490107718684840225655162066944250598,
"Y": 109679892184507445583508052942072381880458734989997944539754656652907671908836
},
"ri": 114415355204464994731195838790575673966672547434206849360810641937879654378709,
"L": 6599458253638430530030989081222295280179915486349680107289106653719043473595,
"aggregatedPublicKey": {
"X": 36294734091650334299015255590564730774169557592558761594180532540875164254701,
"Y": 90715836293579651850115939187136704678019587655103823367190473516129207889293
},
"R": {
"X": 110192382215796476901070453284669594272297384093706425731628707323167171732594,
"Y": 54054991267547387766525693130050883083009923797212175854694435405586777560884
},
"pubKey":{
"X": 64803262188850657644951808823807838997767581964440567505420615647899043300678,
"Y": 63971737191177299002910363586526159730847480888556046747838445847595257452665
},
"privateKey":100285118598107525106152990397846761103060579829613726849138124742137825740949
}s
\ No newline at end of file
{
"Message":"1234",
"PubKey": {
"X": 64803262188850657644951808823807838997767581964440567505420615647899043300678,
"Y": 63971737191177299002910363586526159730847480888556046747838445847595257452665
},
"PrivateKey":100285118598107525106152990397846761103060579829613726849138124742137825740949
}
\ No newline at end of file
{
"senderPrivateKey": "88893ec39cb7725a46ac5bc44ee6e9642c5518ff507df5ca6ce37e622ed86d92",
"recipient": "0xF23708F1a53C5d2904358EA910377Ef1bad235a5",
"amount": 500000000000000000,
"senderPubKey": {
"X": 36930100774794457220439558941655221242025365988148096033938984509752240947363,
"Y": 107355372938895231341476848707268475852702849693587111228193612229669247067113
},
"recipientPubKey": {
"X": 64803262188850657644951808823807838997767581964440567505420615647899043300678,
"Y": 63971737191177299002910363586526159730847480888556046747838445847595257452665
},
"message": "1234",
"aggregatedPublicKey": {
"X": 36294734091650334299015255590564730774169557592558761594180532540875164254701,
"Y": 90715836293579651850115939187136704678019587655103823367190473516129207889293
},
"R": {
"X": 110192382215796476901070453284669594272297384093706425731628707323167171732594,
"Y": 54054991267547387766525693130050883083009923797212175854694435405586777560884
},
"S":"9904612297031977342185807082070540114541417459209506127999571078345521050069",
"commitmentId": 1
}
\ No newline at end of file
{
"senderPrivateKey": "88893ec39cb7725a46ac5bc44ee6e9642c5518ff507df5ca6ce37e622ed86d92",
"commitmentOldId": 1,
"amount": 500000000000000000,
"message": "1234",
"publicKey": {
"X": 64803262188850657644951808823807838997767581964440567505420615647899043300678,
"Y": 63971737191177299002910363586526159730847480888556046747838445847595257452665
},
"R": {
"X": 16190646668120730616383452097178100316515350545587170331426936124946939574795,
"Y": 114236180707621018426675826209788138941571874939242416379465218812523599803372
},
"S":"59460034229263320819118940043948675236521976542967825283899967021331653630627"
}
\ No newline at end of file
......@@ -15,15 +15,16 @@ import (
"net/http"
"offshift/bulletproof"
commC "offshift/commitment"
"offshift/hash"
"offshift/secp256k1/ecc_math"
"offshift/secp256k1/keys"
"offshift/secp256k1/signatures/schnorr_musig_sha256"
"offshift/secp256k1/signatures/schnorr_single_sha256"
"offshift/token"
"time"
)
const tokenContractAddress = "0x1Ce09C22f5899407993f51219A416CEa520146Ac"
var balanceContractAddress = common.HexToAddress("0x09947b76f32Af21b1eeDe363feA19E412aBd93d4")
var tokenContractAddress = common.HexToAddress("0xfF594515B319826Cbab656458c24a77731D95dC7")
var balanceContractAddress = common.HexToAddress("0xC83476667bDcE8c859A444f4430cCBA14A786d1A")
var conn *ethclient.Client
func respondWithJSON(w http.ResponseWriter, code int, payload interface{}) {
......@@ -204,7 +205,6 @@ func aggregatedSignature(w http.ResponseWriter, r *http.Request) {
type genCommParams struct {
Amount *big.Int `json:"amount"`
RecipientPubKey ecc_math.ECPoint `json:"recipientPubKey"`
L big.Int `json:"L"`
}
func generateCommitment(w http.ResponseWriter, r *http.Request) {
......@@ -216,11 +216,10 @@ func generateCommitment(w http.ResponseWriter, r *http.Request) {
}
defer r.Body.Close()
// (r*G+v*H) = (pubKeyBob * H(L,pubKeyBob) + amount * H)
Hash := new(big.Int)
Hash.SetString(fmt.Sprintf("%X", hash.SHA256StringToString(commParams.L.String()+ecc_math.ECPointToString(commParams.RecipientPubKey))), 16)
// (r*G+v*H) = (privateKey * G + amount * H)
// (pubKey + amount * H)
left := ecc_math.ScalarMult(commParams.RecipientPubKey, *Hash)
left := commParams.RecipientPubKey
right := bulletproof.EC.H.Mult(commParams.Amount)
X, Y := bulletproof.EC.C.Add(left.X, left.Y, right.X, right.Y)
......@@ -230,16 +229,166 @@ func generateCommitment(w http.ResponseWriter, r *http.Request) {
respondWithJSON(w, http.StatusCreated, comm)
}
var transfers = make(map[common.Address]int64)
type getTestTokensParams struct {
Recipient common.Address `json:"recipient"`
}
func getTokens(w http.ResponseWriter, r *http.Request) {
var gParams getTestTokensParams
var hashTransaction transactionHash
decoder := json.NewDecoder(r.Body)
if err := decoder.Decode(&gParams); err != nil {
respondWithError(w, http.StatusBadRequest, "Invalid request payload")
return
}
defer r.Body.Close()
h, _ := time.ParseDuration("12h")
fmt.Printf("Convert 12 hours to Seconds: %.2f\n", h.Seconds())
if transfers[gParams.Recipient] + int64(h.Seconds()) > time.Now().Unix() {
respondWithJSON(w, http.StatusBadRequest, "You can take tokens every 12 hours")
} else {
privateKey, err := crypto.HexToECDSA("88893ec39cb7725a46ac5bc44ee6e9642c5518ff507df5ca6ce37e622ed86d92")
if err != nil {
log.Fatal(err)
}
publicKey := privateKey.Public()
publicKeyECDSA, ok := publicKey.(*ecdsa.PublicKey)
if !ok {
log.Fatal("error casting public key to ECDSA")
}
fromAddress := crypto.PubkeyToAddress(*publicKeyECDSA)
fmt.Println("fromAddress: ", fromAddress)
nonce, err := conn.PendingNonceAt(context.Background(), fromAddress)
if err != nil {
log.Fatal(err)
}
fmt.Println("nonce: ", nonce)
gasPrice, err := conn.SuggestGasPrice(context.Background())
if err != nil {
log.Fatal(err)
}
fmt.Println("gasPrice: ", gasPrice)
auth := bind.NewKeyedTransactor(privateKey)
auth.Nonce = big.NewInt(int64(nonce))
auth.Value = big.NewInt(0) // in wei
auth.GasLimit = uint64(3000000) // in units
auth.GasPrice = gasPrice
auth.From = fromAddress
fmt.Println("auth: ", auth)
contract, err := token.NewToken(tokenContractAddress, conn)
if err != nil {
log.Fatalf("Failed to instantiate contract: %v", err)
}
stringAmount := "10000000000000000000"
amount, _ := new(big.Int).SetString(stringAmount, 10)
fmt.Println("amount: ", amount)
tx, err := contract.Transfer(
auth,
gParams.Recipient,
amount)
fmt.Println("transfer tx: ",tx.Hash().Hex())
hashTransaction.TxHash = tx.Hash().Hex()
if err != nil {
log.Fatal(err)
respondWithJSON(w, http.StatusBadRequest, err)
} else {
transfers[gParams.Recipient] = time.Now().Unix()
fmt.Println("transfers: ", transfers[gParams.Recipient])
respondWithJSON(w, http.StatusCreated, hashTransaction)
}
}
}
type approveParams struct {
SenderPrivateKey string `json:"senderPrivateKey"`
Spender common.Address `json:"spender"`
Amount *big.Int `json:"amount"`
}
func approve(w http.ResponseWriter, r *http.Request) {
var aParams approveParams
var hashTransaction transactionHash
decoder := json.NewDecoder(r.Body)
if err := decoder.Decode(&aParams); err != nil {
respondWithError(w, http.StatusBadRequest, "Invalid request payload")
return
}
defer r.Body.Close()
privateKey, err := crypto.HexToECDSA(aParams.SenderPrivateKey)
if err != nil {
log.Fatal(err)
}
publicKey := privateKey.Public()
publicKeyECDSA, ok := publicKey.(*ecdsa.PublicKey)
if !ok {
log.Fatal("error casting public key to ECDSA")
}
fromAddress := crypto.PubkeyToAddress(*publicKeyECDSA)
fmt.Println("fromAddress: ", fromAddress)
nonce, err := conn.PendingNonceAt(context.Background(), fromAddress)
if err != nil {
log.Fatal(err)
}
fmt.Println("nonce: ", nonce)
gasPrice, err := conn.SuggestGasPrice(context.Background())
if err != nil {
log.Fatal(err)
}
fmt.Println("gasPrice: ", gasPrice)
auth := bind.NewKeyedTransactor(privateKey)
auth.Nonce = big.NewInt(int64(nonce))
auth.Value = big.NewInt(0) // in wei
auth.GasLimit = uint64(3000000) // in units
auth.GasPrice = gasPrice
auth.From = fromAddress
fmt.Println("auth: ", auth)
contract, err := token.NewToken(tokenContractAddress, conn)
if err != nil {
log.Fatalf("Failed to instantiate contract: %v", err)
}
tx, err := contract.Approve(
auth,
aParams.Spender,
aParams.Amount)
fmt.Println("approve tx: ",tx.Hash().Hex())
hashTransaction.TxHash = tx.Hash().Hex()
if err != nil {
log.Fatal(err)
respondWithJSON(w, http.StatusBadRequest, err)
}
respondWithJSON(w, http.StatusCreated, hashTransaction)
}
type transferParams struct {
Sender common.Address `json:"sender"`
SenderPrivateKey string `json:"senderPrivateKey"`
Recipient common.Address `json:"recipient"`
Amount *big.Int `json:"amount"`
SenderPubKey ecc_math.ECPoint `json:"senderPubKey"` // todo: delete before deploy
SenderPubKey ecc_math.ECPoint `json:"senderPubKey"`
RecipientPubKey ecc_math.ECPoint `json:"recipientPubKey"`
Message string `json:"message"`
AggregatedPublicKey ecc_math.ECPoint `json:"aggregatedPublicKey"`
L big.Int `json:"L"`
R ecc_math.ECPoint `json:"R"`
S string `json:"S"`
CommitmentId *big.Int `json:"commitmentId"` // todo: temporary, till key storing logic will complete
......@@ -247,6 +396,7 @@ type transferParams struct {
func transferCommitment(w http.ResponseWriter, r *http.Request) {
var tParams transferParams
var hashTransaction transactionHash
decoder := json.NewDecoder(r.Body)
if err := decoder.Decode(&tParams); err != nil {
respondWithError(w, http.StatusBadRequest, "Invalid request payload")
......@@ -254,28 +404,23 @@ func transferCommitment(w http.ResponseWriter, r *http.Request) {
}
defer r.Body.Close()
// (r*G+v*H) = (pubKeyBob * H(L,pubKeyBob) + amount * H)
Hash := new(big.Int)
Hash.SetString(fmt.Sprintf("%X", hash.SHA256StringToString(tParams.L.String()+ecc_math.ECPointToString(tParams.RecipientPubKey))), 16)
// generation recipient commitment
// r - recipient private key
// G - base point G
// v - amount
// H - base point H
// (r*G+v*H) = (pubKeyAli + amount * H)
// (r*G+v*H) = (pubKeyBob + amount * H)
left := ecc_math.ScalarMult(tParams.RecipientPubKey, *Hash)
left := tParams.RecipientPubKey
right := bulletproof.EC.H.Mult(tParams.Amount)
X, Y := bulletproof.EC.C.Add(left.X, left.Y, right.X, right.Y)
comm := ecc_math.ECPoint{X, Y}
fmt.Println("rec comm: ", comm)
// (r*G+v*H) = (pubKeyBob * H(L,pubKeyBob) + amount * H)
val1 := tParams.SenderPubKey.X
val2 := tParams.SenderPubKey.Y
pk := ecc_math.ECPoint{
val1,
val2,
}
Hash2 := new(big.Int)
Hash2.SetString(fmt.Sprintf("%X", hash.SHA256StringToString(tParams.L.String()+ecc_math.ECPointToString(pk))), 16)
left2 := ecc_math.ScalarMult(pk, *Hash2)
left2 := tParams.SenderPubKey
right2 := bulletproof.EC.H.Mult(tParams.Amount)
X2, Y2 := bulletproof.EC.C.Add(left2.X, left2.Y, right2.X, right2.Y)
......@@ -286,7 +431,6 @@ func transferCommitment(w http.ResponseWriter, r *http.Request) {
if err != nil {
log.Fatal(err)
}
publicKey := privateKey.Public()
publicKeyECDSA, ok := publicKey.(*ecdsa.PublicKey)
if !ok {
......@@ -312,17 +456,14 @@ func transferCommitment(w http.ResponseWriter, r *http.Request) {
auth.Value = big.NewInt(0) // in wei
auth.GasLimit = uint64(3000000) // in units
auth.GasPrice = gasPrice
auth.From = tParams.Sender
auth.From = fromAddress
fmt.Println("auth: ", auth)
contract, err := commC.NewCommitment(common.HexToAddress("0x09947b76f32Af21b1eeDe363feA19E412aBd93d4"), conn)
contract, err := commC.NewCommitment(balanceContractAddress, conn)
if err != nil {
log.Fatalf("Failed to instantiate contract: %v", err)
}
amt, _ := contract.Bb(&bind.CallOpts{})
fmt.Println("amt", amt)
S, _ := new(big.Int).SetString(tParams.S, 10)
fmt.Println(schnorr_single_sha256.SchnorrSignatureVerify(
......@@ -330,10 +471,7 @@ func transferCommitment(w http.ResponseWriter, r *http.Request) {
tParams.AggregatedPublicKey,
schnorr_single_sha256.SchnorrSignature{tParams.R, *S}))
fmt.Println("comm start to eth: ", comm)
// todo: at first create keys and commitments storing
amt1, err := contract.TransferCommitment(
tx, err := contract.TransferCommitment(
auth,
tParams.Recipient,
tParams.CommitmentId,
......@@ -341,6 +479,14 @@ func transferCommitment(w http.ResponseWriter, r *http.Request) {
comm.X,
comm.Y,
},
commC.RangeProofMathPointEC{
tParams.SenderPubKey.X,
tParams.SenderPubKey.Y,
},
commC.RangeProofMathPointEC{
tParams.RecipientPubKey.X,
tParams.RecipientPubKey.Y,
},
tParams.Message,
commC.RangeProofMathPointEC{
tParams.AggregatedPublicKey.X,
......@@ -352,12 +498,17 @@ func transferCommitment(w http.ResponseWriter, r *http.Request) {
},
S)
fmt.Println("amt1: ", amt1)
//respondWithJSON(w, http.StatusCreated, aggSignature)
fmt.Println("tx: ",tx.Hash().Hex())
hashTransaction.TxHash = tx.Hash().Hex()
if err != nil {
log.Fatal(err)
respondWithJSON(w, http.StatusBadRequest, err)
}
respondWithJSON(w, http.StatusCreated, hashTransaction)
}
type depositParams struct {
Sender common.Address `json:"sender"`
SenderPrivateKey string `json:"senderPrivateKey"`
Commitment ecc_math.ECPoint `json:"commitment"`
Amount *big.Int `json:"amount"`
......@@ -367,8 +518,13 @@ type depositParams struct {
S string `json:"S"`
}
type transactionHash struct {
TxHash string `json:"tx"`
}
func depositCommitment(w http.ResponseWriter, r *http.Request) {
var dParams depositParams
var hashTransaction transactionHash
decoder := json.NewDecoder(r.Body)
if err := decoder.Decode(&dParams); err != nil {
respondWithError(w, http.StatusBadRequest, "Invalid request payload")
......@@ -406,10 +562,10 @@ func depositCommitment(w http.ResponseWriter, r *http.Request) {
auth.Value = big.NewInt(0) // in wei
auth.GasLimit = uint64(3000000) // in units
auth.GasPrice = gasPrice
auth.From = dParams.Sender
auth.From = fromAddress
fmt.Println("auth: ", auth)
contract, err := commC.NewCommitment(common.HexToAddress("0x09947b76f32Af21b1eeDe363feA19E412aBd93d4"), conn)
contract, err := commC.NewCommitment(balanceContractAddress, conn)
if err != nil {
log.Fatalf("Failed to instantiate contract: %v", err)
}
......@@ -439,18 +595,19 @@ func depositCommitment(w http.ResponseWriter, r *http.Request) {
S,
dParams.Message)
fmt.Println("deposit tx: ",tx.Hash().Hex())
hashTransaction.