func VerifyOIP041Transfer(o Oip041) (Oip041, error) { oip_t := o.Transfer if len(oip_t.Reference) != 64 { return o, ErrInvalidReference } if !utility.CheckAddress(oip_t.To) { return o, ErrInvalidAddress } preImage := oip_t.Reference + "-" + oip_t.To + "-" + oip_t.From + "-" + strconv.FormatInt(oip_t.Timestamp, 10) valid, err := utility.CheckSignature(oip_t.From, o.Signature, preImage) if !valid { return o, err } return o, nil }
func HandleOIP041Transfer(o Oip041, txid string, processingBlock int, dbtx *sql.Tx) { // ToDo: Verify rights to transfer // leedle leedle leedle lee oip_t := o.Transfer if len(oip_t.Reference) != 64 { return //oip_t, ErrInvalidReference } if !utility.CheckAddress(oip_t.To) { return //oip_t, ErrInvalidAddress } preImage := oip_t.Reference + "-" + oip_t.To + "-" + oip_t.From + "-" + strconv.FormatInt(oip_t.Timestamp, 10) valid, err := utility.CheckSignature(oip_t.From, o.Signature, preImage) if !valid { return //oip_t, err } stmt, err := dbtx.Prepare(`SELECT publisher FROM media WHERE txid = ? LIMIT 1;`) if err != nil { log.Fatal(err) } row := stmt.QueryRow(oip_t.Reference) var publisher string err = row.Scan(&publisher) if err != nil { log.Fatal(err) } stmt.Close() fmt.Println(publisher) if publisher == oip_t.From { fmt.Println("Transfered") StoreOIPTransfer(oip_t, dbtx) } else { fmt.Println("Denied") } fmt.Println() }
func VerifyMediaMultipartSingle(s string, txid string, block int) (MediaMultipartSingle, error) { var ret MediaMultipartSingle prefix := "alexandria-media-multipart(" // check prefix checkPrefix := strings.HasPrefix(s, prefix) if !checkPrefix { return ret, ErrWrongPrefix } // trim prefix off s = strings.TrimPrefix(s, prefix) // check length if len(s) < 108 { return ret, errors.New("not enough data in mutlipart string") } // check part and max part, err := strconv.Atoi(string(s[0])) if err != nil { fmt.Println("cannot convert part to int") return ret, errors.New("cannot convert part to int") } max, err2 := strconv.Atoi(string(s[2])) if err2 != nil { fmt.Println("cannot convert max to int") return ret, errors.New("cannot convert max to int") } // get and check address address := s[4:38] if !utility.CheckAddress(address) { // fmt.Println("address doesn't check out: \"" + address + "\"") return ret, ErrInvalidAddress } // get reference txid reference := s[39:103] // get and check signature sigEndIndex := strings.Index(s, "):") if sigEndIndex < 105 { fmt.Println("no end of signature found, malformed tx-comment") return ret, ErrNoSignatureEnd } signature := s[104:sigEndIndex] if signature[len(signature)-1] == ',' { // strip erroneous comma added by fluffy-enigma signature = signature[:len(signature)-1] } data := s[sigEndIndex+2:] // fmt.Println("data: \"" + data + "\"") // signature pre-image is <part>-<max>-<address>-<txid>-<data> // in the case of multipart[0], txid is 64 zeros // in the case of multipart[n], where n != 0, txid is the reference txid (from multipart[0]) preimage := string(s[0]) + "-" + string(s[2]) + "-" + address + "-" + reference + "-" + data // fmt.Printf("preimage: %v\n", preimage) val, _ := utility.CheckSignature(address, signature, preimage) if !val { // fmt.Println("signature didn't pass checksignature test") return ret, ErrBadSignature } // if part == 0, reference should be submitted in the tx-comment as a string of 64 zeros // the local DB will store reference = txid for this transaction after it's submitted // in case of a reorg, the publisher must re-publish this multipart message (sorry) if part == 0 { if reference != "0000000000000000000000000000000000000000000000000000000000000000" { // fmt.Println("reference txid should be 64 zeros for part 0 of a multipart message") return ret, errors.New("reference txid should be 64 zeros for part 0") } reference = txid } // all checks passed, verified! //fmt.Printf("data: %v\n", data) // fmt.Printf("=== VERIFIED ===\n") //fmt.Printf("part: %v\nmax: %v\nreference: %v\naddress: %v\nsignature: %v\ntxid: %v\nblock: %v\n", part, max, reference, address, signature, txid, block) ret = MediaMultipartSingle{ Part: part, Max: max, Reference: reference, Address: address, Signature: signature, Data: data, Txid: txid, Block: block, } return ret, nil }