-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathchannelid.go
42 lines (34 loc) · 1.07 KB
/
channelid.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
package filclient
import (
"fmt"
"strconv"
"strings"
datatransfer "github.com/filecoin-project/go-data-transfer"
"github.com/libp2p/go-libp2p/core/peer"
)
func ChannelIDFromString(id string) (*datatransfer.ChannelID, error) {
if id == "" {
return nil, fmt.Errorf("cannot parse empty string as channel id")
}
parts := strings.Split(id, "-")
if len(parts) != 3 {
return nil, fmt.Errorf("cannot parse channel id '%s': expected format 'initiator-responder-transferid'", id)
}
initiator, err := peer.Decode(parts[0])
if err != nil {
return nil, fmt.Errorf("parsing initiator peer id '%s' in channel id '%s'", parts[0], id)
}
responder, err := peer.Decode(parts[1])
if err != nil {
return nil, fmt.Errorf("parsing responder peer id '%s' in channel id '%s'", parts[1], id)
}
xferid, err := strconv.ParseUint(parts[2], 10, 64)
if err != nil {
return nil, fmt.Errorf("parsing transfer id '%s' in channel id '%s'", parts[2], id)
}
return &datatransfer.ChannelID{
Initiator: initiator,
Responder: responder,
ID: datatransfer.TransferID(xferid),
}, nil
}