我是aries的新手,我正在运行代码示例,但我无法使用didexchange协议建立连接。
我正在使用go创建两个服务,一个在端口5000上运行,另一个在端口5005上运行。
下面是我的代码:
func main() {
client, _ := services.NewDidClient(port)
http.Handle("/invitation", &handlers.InvitationHandler{DidClient: *client})
http.Handle("/connection", &handlers.ConnectionHandler{DidClient: *client})
http.ListenAndServe(fmt.Sprintf(":%d", port), nil)
}
type DidClient struct {
didexchange.Client
}
func NewDidClient(port int32) (*DidClient, error) {
framework, _ := aries.New(
// aries.WithInboundTransport(newInboundTransport(port)),
aries.WithStoreProvider(mem.NewProvider()),
aries.WithProtocolStateStoreProvider(mem.NewProvider()),
)
ctx, _ := framework.Context()
didExchangeClient, _ := didexchange.New(ctx)
actions := make(chan service.DIDCommAction, 1)
didExchangeClient.RegisterActionEvent(actions)
go func() {
service.AutoExecuteActionEvent(actions)
}()
return &DidClient{Client: *didExchangeClient}, nil
}
type InvitationHandler struct {
DidClient services.DidClient
}
func (handler *InvitationHandler) ServeHTTP(writer http.ResponseWriter, request *http.Request) {
switch request.Method {
case http.MethodGet:
invitation, _ := handler.DidClient.CreateInvitation("5000 invitation")
response, _ := json.Marshal(invitation)
writer.Header().Set("Content-Type", "application/json")
writer.Write(response)
}
}
type ConnectionHandler struct {
DidClient services.DidClient
}
func (handler *ConnectionHandler) ServeHTTP(writer http.ResponseWriter, request *http.Request) {
switch request.Method {
case http.MethodGet:
connections, _ := handler.DidClient.QueryConnections(&didexchange.QueryConnectionsParams{})
response, _ := json.Marshal(connections)
writer.Header().Set("Content-Type", "application/json")
writer.Write(response)
case http.MethodPost:
var invitation didexchange.Invitation
bodyBytes, _ := ioutil.ReadAll(request.Body)
json.Unmarshal(bodyBytes, &invitation)
connectionID, _ := handler.DidClient.HandleInvitation(&invitation)
connection, _ := handler.DidClient.GetConnection(connectionID)
response, _ := json.Marshal(connection)
writer.Header().Set("Content-Type", "application/json")
writer.Write(response)
}
}
type inboundTransport struct {
port int32
}
func newInboundTransport(port int32) *inboundTransport {
return &inboundTransport{port: port + 1}
}
func (transport *inboundTransport) Start(prov transport.Provider) error {
return nil
}
func (transport *inboundTransport) Stop() error {
return nil
}
func (transport *inboundTransport) Endpoint() string {
return fmt.Sprintf("http://localhost:%d", transport.port)
}
我省略了这里的错误处理,但我有它在我的代码。
所以,在没有入站传输的情况下运行,它们都说服务端点是这样的:
我在端口5000上使用邀请处理程序创建一个邀请,如下所示:
curl --request GET --url http://localhost:5000/invitation
response:
{
"serviceEndpoint": "didcomm:transport/queue",
"recipientKeys": [
"did:key:z6MkojtSLJ4EtwAUJZsy7BDhhDDnNtfvwu3AH7di6o5XzaFz"
],
"@id": "ac56543b-526b-47e6-887a-0ee4a8fd888e",
"label": "5000 invitation",
"@type": "https://didcomm.org/didexchange/1.0/invitation"
}
在连接处理程序中使用post将其传递给端口5005的代理,如下所示:
curl --request POST
--url http://localhost:5005/connection
--header 'Content-Type: application/json'
--data '{
"serviceEndpoint": "didcomm:transport/queue",
"recipientKeys": [
"did:key:z6MkojtSLJ4EtwAUJZsy7BDhhDDnNtfvwu3AH7di6o5XzaFz"
],
"@id": "ac56543b-526b-47e6-887a-0ee4a8fd888e",
"label": "5000 invitation",
"@type": "https://didcomm.org/didexchange/1.0/invitation"
}'
response:
{
"ConnectionID": "808939bc-0e88-41cb-971b-e00e3aa2a631",
"State": "null",
"ThreadID": "3db73089-025d-4e6f-9703-d86881745494",
"ParentThreadID": "",
"TheirLabel": "5000 invitation",
"TheirDID": "",
"MyDID": "",
"ServiceEndPoint": "didcomm:transport/queue",
"RecipientKeys": [
"did:key:z6Mknaw7ANj5yqDrsFhkWgfdYM8XbGNKLLexhBmD1XoUKAAo"
],
"RoutingKeys": null,
"InvitationID": "3db73089-025d-4e6f-9703-d86881745494",
"InvitationDID": "",
"Implicit": false,
"Namespace": "my"
}
我期望在运行连接get的两个代理中都获得连接,但连接仅在5005代理上,如下所示:
curl --request GET --url http://localhost:5000/connection
response: nil
curl --request GET --url http://localhost:5005/connection
response:
[{
"ConnectionID": "808939bc-0e88-41cb-971b-e00e3aa2a631",
"State": "abandoned",
"ThreadID": "3db73089-025d-4e6f-9703-d86881745494",
"ParentThreadID": "",
"TheirLabel": "5000 invitation",
"TheirDID": "",
"MyDID": "did:peer:1zQmPBE2GKCCWTXoSA2EzUVbLGs3njJtqDmnN5LLP9UQpWLJ",
"ServiceEndPoint": "didcomm:transport/queue",
"RecipientKeys": [
"did:key:z6Mknaw7ANj5yqDrsFhkWgfdYM8XbGNKLLexhBmD1XoUKAAo"
],
"RoutingKeys": null,
"InvitationID": "3db73089-025d-4e6f-9703-d86881745494",
"InvitationDID": "",
"Implicit": false,
"Namespace": "my"
}]
但是我在控制台上得到这些错误:
[aries-framework/out-of-band/service] 2021/08/26 02:11:15 UTC - n/a -> ERROR command=[out-of-band] action=handleDIDEvent] [] errMsg=[ignored]
代理5005中的连接说它放弃了,我认为问题在于两个框架应该能够在没有我的干预的情况下相互连接,但他们看不到对方,所以我使用http://localhost与端口5001和5006添加了InboundTransport,但后来我得到了这些错误:
[aries-framework/out-of-band/service] 2021/08/26 02:20:41 UTC - n/a -> ERROR command=[out-of-band] action[handleDIDEvent] [] errMsg=[ignored]
[aries-framework/http] 2021/08/26 02:20:44 UTC - http.(*OutboundHTTPClient).Send -> ERROR posting DID envelope to agent failed [http://localhost:5001, Post "http://localhost:5001": dial tcp [::1]:5001: connectex: No connection could be made because the target machine actively refused it.]
[aries-framework/out-of-band/service] 2021/08/26 02:20:44 UTC - n/a -> ERROR command=[out-of-band] action [handleDIDEvent] [] errMsg=[ignored]
所以,我有点迷路了,我读到代理应该只使用新的()(带电池),但我不确定在我的本地主机中运行两个代理是否使它们处于冲突中,以及如何"工作吗?我知道框架没有使用rest,它嵌入了go,但是,它如何暴露它的端点?我错过了什么?
感谢我将问题作为github问题发布,并在他们发布的参考资料的帮助下更改了我的代码并获得了入站工作。
修改如下:
func NewDidClient(port int32) (*DidClient, error) {
address := fmt.Sprintf("localhost:%d", port + 1)
inbound, err := ws.NewInbound(address, "ws://" + address, "", "")
framework, _ := aries.New(
aries.WithInboundTransport(inbound),
aries.WithOutboundTransports(ws.NewOutbound()),
aries.WithStoreProvider(mem.NewProvider()),
aries.WithProtocolStateStoreProvider(mem.NewProvider()),
)
ctx, _ := framework.Context()
didExchangeClient, _ := didexchange.New(ctx)
actions := make(chan service.DIDCommAction, 1)
didExchangeClient.RegisterActionEvent(actions)
go func() {
service.AutoExecuteActionEvent(actions)
}()
return &DidClient{Client: *didExchangeClient}, nil
}
它使用websockets,现在两个代理可以相互连接。