Example #1
0
func (k *Key) UnmarshalJSON(j []byte) (err error) {
	keyJSON := new(plainKeyJSON)
	err = json.Unmarshal(j, &keyJSON)
	if err != nil {
		return err
	}

	u := new(uuid.UUID)
	*u = uuid.Parse(keyJSON.Id)
	k.Id = *u
	addr, err := hex.DecodeString(keyJSON.Address)
	if err != nil {
		return err
	}

	privkey, err := hex.DecodeString(keyJSON.PrivateKey)
	if err != nil {
		return err
	}

	k.Address = common.BytesToAddress(addr)
	k.PrivateKey = ToECDSA(privkey)

	return nil
}
Example #2
0
func getKeyAddresses(keysDirPath string) (addresses []common.Address, err error) {
	fileInfos, err := ioutil.ReadDir(keysDirPath)
	if err != nil {
		return nil, err
	}
	for _, fileInfo := range fileInfos {
		filename := fileInfo.Name()
		if len(filename) >= 40 {
			addr := filename[len(filename)-40 : len(filename)]
			address, err := hex.DecodeString(addr)
			if err == nil {
				addresses = append(addresses, common.BytesToAddress(address))
			}
		}
	}
	return addresses, err
}
Example #3
0
// 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
}
Example #4
0
func (self *StateDB) RawDump() World {
	world := World{
		Root:     common.Bytes2Hex(self.trie.Root()),
		Accounts: make(map[string]Account),
	}

	it := self.trie.Iterator()
	for it.Next() {
		addr := self.trie.GetKey(it.Key)
		stateObject, _ := DecodeObject(common.BytesToAddress(addr), self.db, it.Value)

		account := Account{Balance: stateObject.balance.String(), Nonce: stateObject.nonce, Root: common.Bytes2Hex(stateObject.Root()), CodeHash: common.Bytes2Hex(stateObject.codeHash)}
		account.Storage = make(map[string]string)

		storageIt := stateObject.trie.Iterator()
		for storageIt.Next() {
			account.Storage[common.Bytes2Hex(self.trie.GetKey(storageIt.Key))] = common.Bytes2Hex(storageIt.Value)
		}
		world.Accounts[common.Bytes2Hex(addr)] = account
	}
	return world
}
Example #5
0
func (args *NewFilterArgs) UnmarshalJSON(data []byte) error {
	type input struct {
		From      *rpc.BlockNumber `json:"fromBlock"`
		ToBlock   *rpc.BlockNumber `json:"toBlock"`
		Addresses interface{}      `json:"address"`
		Topics    interface{}      `json:"topics"`
	}

	var raw input
	if err := json.Unmarshal(data, &raw); err != nil {
		return err
	}

	if raw.From == nil {
		args.FromBlock = rpc.LatestBlockNumber
	} else {
		args.FromBlock = *raw.From
	}

	if raw.ToBlock == nil {
		args.ToBlock = rpc.LatestBlockNumber
	} else {
		args.ToBlock = *raw.ToBlock
	}

	args.Addresses = []common.Address{}

	if raw.Addresses != nil {
		// raw.Address can contain a single address or an array of addresses
		var addresses []common.Address

		if strAddrs, ok := raw.Addresses.([]interface{}); ok {
			for i, addr := range strAddrs {
				if strAddr, ok := addr.(string); ok {
					if len(strAddr) >= 2 && strAddr[0] == '0' && (strAddr[1] == 'x' || strAddr[1] == 'X') {
						strAddr = strAddr[2:]
					}
					if decAddr, err := hex.DecodeString(strAddr); err == nil {
						addresses = append(addresses, common.BytesToAddress(decAddr))
					} else {
						fmt.Errorf("invalid address given")
					}
				} else {
					return fmt.Errorf("invalid address on index %d", i)
				}
			}
		} else if singleAddr, ok := raw.Addresses.(string); ok {
			if len(singleAddr) >= 2 && singleAddr[0] == '0' && (singleAddr[1] == 'x' || singleAddr[1] == 'X') {
				singleAddr = singleAddr[2:]
			}
			if decAddr, err := hex.DecodeString(singleAddr); err == nil {
				addresses = append(addresses, common.BytesToAddress(decAddr))
			} else {
				fmt.Errorf("invalid address given")
			}
		} else {
			errors.New("invalid address(es) given")
		}
		args.Addresses = addresses
	}

	topicConverter := func(raw string) (common.Hash, error) {
		if len(raw) == 0 {
			return common.Hash{}, nil
		}

		if len(raw) >= 2 && raw[0] == '0' && (raw[1] == 'x' || raw[1] == 'X') {
			raw = raw[2:]
		}

		if decAddr, err := hex.DecodeString(raw); err == nil {
			return common.BytesToHash(decAddr), nil
		}

		return common.Hash{}, errors.New("invalid topic given")
	}

	// topics is an array consisting of strings or arrays of strings
	if raw.Topics != nil {
		topics, ok := raw.Topics.([]interface{})
		if ok {
			parsedTopics := make([][]common.Hash, len(topics))
			for i, topic := range topics {
				if topic == nil {
					parsedTopics[i] = []common.Hash{common.StringToHash("")}
				} else if strTopic, ok := topic.(string); ok {
					if t, err := topicConverter(strTopic); err != nil {
						return fmt.Errorf("invalid topic on index %d", i)
					} else {
						parsedTopics[i] = []common.Hash{t}
					}
				} else if arrTopic, ok := topic.([]interface{}); ok {
					parsedTopics[i] = make([]common.Hash, len(arrTopic))
					for j := 0; j < len(parsedTopics[i]); i++ {
						if arrTopic[j] == nil {
							parsedTopics[i][j] = common.StringToHash("")
						} else if str, ok := arrTopic[j].(string); ok {
							if t, err := topicConverter(str); err != nil {
								return fmt.Errorf("invalid topic on index %d", i)
							} else {
								parsedTopics[i] = []common.Hash{t}
							}
						} else {
							fmt.Errorf("topic[%d][%d] not a string", i, j)
						}
					}
				} else {
					return fmt.Errorf("topic[%d] invalid", i)
				}
			}
			args.Topics = parsedTopics
		}
	}

	return nil
}
Example #6
0
// Creates an ethereum address given the bytes and the nonce
func CreateAddress(b common.Address, nonce uint64) common.Address {
	data, _ := rlp.EncodeToBytes([]interface{}{b, nonce})
	return common.BytesToAddress(Sha3(data)[12:])
	//return Sha3(common.NewValue([]interface{}{b, nonce}).Encode())[12:]
}
Example #7
0
func PubkeyToAddress(p ecdsa.PublicKey) common.Address {
	pubBytes := FromECDSAPub(&p)
	return common.BytesToAddress(Sha3(pubBytes[1:])[12:])
}