func ecrecoverFunc(in []byte) []byte { in = common.RightPadBytes(in, 128) // "in" is (hash, v, r, s), each 32 bytes // but for ecrecover we want (r, s, v) r := common.BytesToBig(in[64:96]) s := common.BytesToBig(in[96:128]) // Treat V as a 256bit integer vbig := common.Bytes2Big(in[32:64]) v := byte(vbig.Uint64()) if !crypto.ValidateSignatureValues(v, r, s) { glog.V(logger.Debug).Infof("EC RECOVER FAIL: v, r or s value invalid") return nil } // v needs to be at the end and normalized for libsecp256k1 vbignormal := new(big.Int).Sub(vbig, big.NewInt(27)) vnormal := byte(vbignormal.Uint64()) rsv := append(in[64:128], vnormal) pubKey, err := crypto.Ecrecover(in[:32], rsv) // make sure the public key is a valid one if err != nil { glog.V(logger.Error).Infof("EC RECOVER FAIL: ", err) return nil } // the first byte of pubkey is bitcoin heritage return common.LeftPadBytes(crypto.Sha3(pubKey[1:])[12:], 32) }
// toGoType parses the input and casts it to the proper type defined by the ABI // argument in t. func toGoType(t Argument, input []byte) interface{} { switch t.Type.T { case IntTy: return common.BytesToBig(input) case UintTy: return common.BytesToBig(input) case BoolTy: return common.BytesToBig(input).Uint64() > 0 case AddressTy: return common.BytesToAddress(input) case HashTy: return common.BytesToHash(input) case BytesTy: return input } return nil }
// Start Go API. Not important for this version func (c *Channels) NewChannel(key *ecdsa.PrivateKey, to common.Address, amount, price *big.Int, cb func(*Channel)) (*types.Transaction, error) { from := crypto.PubkeyToAddress(key.PublicKey) data, err := c.abi.Pack("createChannel", to, price) if err != nil { return nil, err } statedb, err := c.blockchain.State() if err != nil { return nil, err } transaction, err := types.NewTransaction(statedb.GetNonce(from), contractAddress, amount, big.NewInt(250000), big.NewInt(50000000000), data).SignECDSA(key) if err != nil { return nil, err } evId := c.abi.Events["NewChannel"].Id() filter := filters.New(c.db) filter.SetAddresses([]common.Address{contractAddress}) filter.SetTopics([][]common.Hash{ // TODO refactor, helper []common.Hash{evId}, []common.Hash{from.Hash()}, []common.Hash{to.Hash()}, }) filter.SetBeginBlock(0) filter.SetEndBlock(-1) filter.LogsCallback = func(logs vm.Logs) { // tere should really be only one log. TODO this part log := logs[0] // TODO: do to and from validation here /* from := log.Topics[1] to := log.Topics[2] */ channelId := common.BytesToHash(log.Data[0:31]) nonce := common.BytesToBig(log.Data[31:]) c.channelMu.Lock() defer c.channelMu.Unlock() channel, exist := c.channels[channelId] if !exist { channel = NewChannel(c, channelId, from, to, nonce) c.channels[channelId] = channel } cb(channel) } c.filters.Add(filter) return transaction, nil }
func opSha3(instr instruction, pc *uint64, env Environment, contract *Contract, memory *Memory, stack *stack) { offset, size := stack.pop(), stack.pop() hash := crypto.Sha3(memory.Get(offset.Int64(), size.Int64())) stack.push(common.BytesToBig(hash)) }
func (b Bloom) TestBytes(test []byte) bool { return b.Test(common.BytesToBig(test)) }