コード例 #1
0
ファイル: add.go プロジェクト: travisperson/ipfs-api-test
func PostFile(t *testing.T) {
	assert := assert.New(t)

	fs := []files.File{
		{"file.txt", "Hello", "QmNULL"},
		{"file2.txt", "Hello2", "QmNULL"},
	}

	request := AddFiles(t, fs)

	// Perform the request
	resp, err := http.DefaultClient.Do(request)

	assert.NoError(err)
	assert.Equal(http.StatusOK, resp.StatusCode)

	// Verify our return values
	var file files.File
	dec := json.NewDecoder(resp.Body)

	// TODO: The API does not have to return files in order
	// go-ipfs just happens to do so as of right now
	for _, f := range fs {
		err = dec.Decode(&file)
		assert.NoError(err)

		// Verify the name
		assert.Equal(f.Name, file.Name)

		// Verify hash
		_, err = mh.FromB58String(file.Hash)
		assert.NoError(err)
	}
}
コード例 #2
0
ファイル: peer.go プロジェクト: tilgovi/go-libp2p
// IDB58Decode returns a b58-decoded Peer
func IDB58Decode(s string) (ID, error) {
	m, err := mh.FromB58String(s)
	if err != nil {
		return "", err
	}
	return ID(m), err
}
コード例 #3
0
ファイル: ipld.go プロジェクト: mildred/go-ipld
// Hash returns the multihash value of the link.
func (l Link) Hash() (mh.Multihash, error) {
	s := l.LinkStr()
	if s == "" {
		return nil, errors.New("no hash in link")
	}
	return mh.FromB58String(s)
}
コード例 #4
0
ファイル: ipld_test.go プロジェクト: mildred/go-ipld
func mmh(b58 string) mh.Multihash {
	h, err := mh.FromB58String(b58)
	if err != nil {
		panic("failed to decode multihash")
	}
	return h
}
コード例 #5
0
ファイル: codec.go プロジェクト: whyrusleeping/go-multiaddr
func addressStringToBytes(p Protocol, s string) ([]byte, error) {
	switch p.Code {

	case P_IP4: // ipv4
		i := net.ParseIP(s).To4()
		if i == nil {
			return nil, fmt.Errorf("failed to parse ip4 addr: %s", s)
		}
		return i, nil

	case P_IP6: // ipv6
		i := net.ParseIP(s).To16()
		if i == nil {
			return nil, fmt.Errorf("failed to parse ip6 addr: %s", s)
		}
		return i, nil

	// tcp udp dccp sctp
	case P_TCP, P_UDP, P_DCCP, P_SCTP:
		i, err := strconv.Atoi(s)
		if err != nil {
			return nil, fmt.Errorf("failed to parse %s addr: %s", p.Name, err)
		}
		if i >= 65536 {
			return nil, fmt.Errorf("failed to parse %s addr: %s", p.Name, "greater than 65536")
		}
		b := make([]byte, 2)
		binary.BigEndian.PutUint16(b, uint16(i))
		return b, nil

	case P_IPFS: // ipfs
		// the address is a varint prefixed multihash string representation
		m, err := mh.FromB58String(s)
		if err != nil {
			return nil, fmt.Errorf("failed to parse ipfs addr: %s %s", s, err)
		}
		size := CodeToVarint(len(m))
		b := append(size, m...)
		return b, nil
	}

	return []byte{}, fmt.Errorf("failed to parse %s addr: unknown", p.Name)
}
コード例 #6
0
ファイル: pm.go プロジェクト: kalmi/gx
// ResolveDepName resolves a given package name to a hash
// using configured repos as a mapping.
func (pm *PM) ResolveDepName(name string) (string, error) {
	_, err := mh.FromB58String(name)
	if err == nil {
		return name, nil
	}

	if strings.Contains(name, "/") {
		parts := strings.Split(name, "/")
		rpath, ok := pm.cfg.GetRepos()[parts[0]]
		if !ok {
			return "", fmt.Errorf("unknown repo: '%s'", parts[0])
		}

		pkgs, err := pm.FetchRepo(rpath, true)
		if err != nil {
			return "", err
		}

		val, ok := pkgs[parts[1]]
		if !ok {
			return "", fmt.Errorf("package %s not found in repo %s", parts[1], parts[0])
		}

		return val, nil
	}

	out, err := pm.QueryRepos(name)
	if err != nil {
		return "", err
	}

	if len(out) == 0 {
		return "", fmt.Errorf("could not find package by name: %s", name)
	}

	if len(out) == 1 {
		for _, v := range out {
			return v, nil
		}
	}

	return "", fmt.Errorf("ambiguous ref, appears in multiple repos")
}
コード例 #7
0
ファイル: block.go プロジェクト: disorganizer/brig
// AddBlock creates a new block with `data`.
// The hash of the data is returned.
// It is no error if the block already exists.
func AddBlock(node *Node, data []byte) (gmh.Multihash, error) {
	nd, err := node.proc()
	if err != nil {
		log.Warningf("ipfs block-add: %v", err)
		return nil, err
	}

	block := blocks.NewBlock(data)
	k, err := nd.Blocks.AddBlock(block)

	if err != nil {
		return nil, err
	}

	mh, err := gmh.FromB58String(k.B58String())
	if err != nil {
		return nil, err
	}

	return mh, nil
}
コード例 #8
0
ファイル: index.go プロジェクト: disorganizer/brig
// Owner returns the owner of the store (name + hash)
func (st *Store) Owner() (*Author, error) {
	bid, err := st.fs.MetadataGet("id")
	if err != nil {
		return nil, err
	}

	bhash, err := st.fs.MetadataGet("hash")
	if err != nil {
		return nil, err
	}

	ident, err := id.Cast(string(bid))
	if err != nil {
		return nil, err
	}

	hash, err := multihash.FromB58String(string(bhash))
	if err != nil {
		return nil, err
	}

	return &Author{ident, &Hash{hash}}, nil
}
コード例 #9
0
ファイル: codec.go プロジェクト: Gaboose/go-pubsub
func addressStringToBytes(p Protocol, s string) ([]byte, error) {
	switch p.Code {

	case P_IP4: // ipv4
		i := net.ParseIP(s).To4()
		if i == nil {
			return nil, fmt.Errorf("failed to parse ip4 addr: %s", s)
		}
		return i, nil

	case P_IP6: // ipv6
		i := net.ParseIP(s).To16()
		if i == nil {
			return nil, fmt.Errorf("failed to parse ip6 addr: %s", s)
		}
		return i, nil

	// tcp udp dccp sctp
	case P_TCP, P_UDP, P_DCCP, P_SCTP:
		i, err := strconv.Atoi(s)
		if err != nil {
			return nil, fmt.Errorf("failed to parse %s addr: %s", p.Name, err)
		}
		if i >= 65536 {
			return nil, fmt.Errorf("failed to parse %s addr: %s", p.Name, "greater than 65536")
		}
		b := make([]byte, 2)
		binary.BigEndian.PutUint16(b, uint16(i))
		return b, nil

	case P_ONION:
		addr := strings.Split(s, ":")
		if len(addr) != 2 {
			return nil, fmt.Errorf("failed to parse %s addr: %s does not contain a port number.", p.Name, s)
		}

		// onion address without the ".onion" substring
		if len(addr[0]) != 16 {
			return nil, fmt.Errorf("failed to parse %s addr: %s not a Tor onion address.", p.Name, s)
		}
		onionHostBytes, err := base32.StdEncoding.DecodeString(strings.ToUpper(addr[0]))
		if err != nil {
			return nil, fmt.Errorf("failed to decode base32 %s addr: %s %s", p.Name, s, err)
		}

		// onion port number
		i, err := strconv.Atoi(addr[1])
		if err != nil {
			return nil, fmt.Errorf("failed to parse %s addr: %s", p.Name, err)
		}
		if i >= 65536 {
			return nil, fmt.Errorf("failed to parse %s addr: %s", p.Name, "port greater than 65536")
		}
		if i < 1 {
			return nil, fmt.Errorf("failed to parse %s addr: %s", p.Name, "port less than 1")
		}

		onionPortBytes := make([]byte, 2)
		binary.BigEndian.PutUint16(onionPortBytes, uint16(i))
		bytes := []byte{}
		bytes = append(bytes, onionHostBytes...)
		bytes = append(bytes, onionPortBytes...)
		return bytes, nil

	case P_IPFS: // ipfs
		// the address is a varint prefixed multihash string representation
		m, err := mh.FromB58String(s)
		if err != nil {
			return nil, fmt.Errorf("failed to parse ipfs addr: %s %s", s, err)
		}
		size := CodeToVarint(len(m))
		b := append(size, m...)
		return b, nil
	}

	return []byte{}, fmt.Errorf("failed to parse %s addr: unknown", p.Name)
}