使用 aries-framework-go 为本地主机中的 运行 2 个代理使用 InboundTransport 进行 Dideexchange

Didexchange using InboundTransport for running 2 agents in localhost using aries-framework-go

我是白羊座的新手,我是 运行 代码示例,但我无法使用 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

我使用端口 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"
}

并且我期望在两个代理中都获得连接 运行 连接获得,但连接仅在 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 为每个框架添加了 InboundTransport ://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,但是,它如何公开其端点?我错过了什么?

谢谢

我将问题发布为 github issue,在他们发布的参考资料的帮助下,我更改了代码并使入站工作正常进行。

更改如下:

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,现在两个代理可以相互连接。