英文:
Didexchange using InboundTransport for running 2 agents in localhost using aries-framework-go
问题
我是你的中文翻译助手,以下是你提供的代码的翻译:
我对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)
}
这里省略了错误处理,但是我在我的代码中有它。
所以,没有传入传入传输时,它们都说服务端点是这样的:didcomm:transport/queue
我使用invitation handler在端口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"
}
将其传递给端口5005上的代理,使用连接处理程序的POST方法,像这样:
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代理中的连接状态为abandoned,我认为问题在于两个框架应该能够在没有我的干预下相互连接,但它们无法看到彼此,所以我在每个框架中都添加了InboundTransport,使用http://localhost和端口5001和5006,但是然后我得到了这些错误:
[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]
所以,我有点迷失了,我读到代理应该只使用new()(带电池),但我不确定在本地主机上同时运行两个代理是否会导致冲突,以及“didcomm:transport/queue”是如何工作的?我知道该框架不使用REST,而是与Go一起嵌入,但是它是如何公开其端点的?我漏掉了什么?
谢谢。
英文:
I'm new to aries and I'm running an example of the code but I haven't been able to stablish a connection using the didexchange protocol.
I am creating two services using go, one running on port 5000 and other at 5005.
Here's my code:
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)
}
I'm omiting the error handling here but I have it in my code.
So, running without the inbound transport they both say that the service endpoint is this: didcomm:transport/queue
I create an invitation using the invitation handler on port 5000, like this:
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"
}
pass it to the agent in port 5005 using post in the connection handler like this:
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"
}
And I was expecting to get a connection in both agents running the connection get, but the connection is only on the 5005 agent, like this:
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"
}]
But I'm getting these errors on the console:
[aries-framework/out-of-band/service] 2021/08/26 02:11:15 UTC - n/a -> ERROR command=[out-of-band] action=handleDIDEvent] [] errMsg=[ignored]
The connection in agent 5005 says its abandonned, and I think the problem its that both frameworks should be able to connect whith each other without my intervention but they cannot see each other, so I added the InboundTransport to each using http://localhost with ports 5001 and 5006, but then I got these errors:
[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]
So, I'm a little lost, I read that the agents should work with only the new() (with batteries) but I'm not sure if running both agents in my localhost is making them in conflict, and how does "didcomm:transport/queue" work? I get that the framework is not using rest and its embebbed with go, but then, how does it expose its endpoints? what am I missing?
Thanks
答案1
得分: 1
我将为您翻译以下内容:
我在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,现在两个代理可以相互连接。
英文:
I posted the question as a github issue and with the help of the references they posted I changed my code and got the inbound working.
Here are the changes:
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
}
It uses websockets and now both agents can connect with each other.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论