0

我是 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

我使用端口 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://localhost 将 InboundTransport 添加到每个框架端口 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 :运输/队列”工作?我知道框架没有使用 rest 并且它嵌入了 go,但是,它如何暴露它的端点?我错过了什么?

谢谢

4

1 回答 1

0

我将问题发布为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,现在两个代理可以相互连接。

于 2021-08-26T17:07:44.817 回答