首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用InboundTransport在本地主机上使用aries框架运行2个代理的二进制转换

使用InboundTransport在本地主机上使用aries框架运行2个代理的二进制转换
EN

Stack Overflow用户
提问于 2021-08-26 13:09:15
回答 1查看 81关注 0票数 0

我是aries的新手,我正在运行一个代码示例,但我还不能使用didexchange协议建立连接。

我正在使用go创建两个服务,一个在端口5000上运行,另一个在5005上运行。

下面是我的代码:

代码语言:javascript
复制
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上使用邀请处理程序创建了一个邀请,如下所示:

代码语言:javascript
复制
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中的代理,如下所示:

代码语言:javascript
复制
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代理上,如下所示:

代码语言:javascript
复制
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"
}]

但是我在控制台上看到了这些错误:

代码语言:javascript
复制
[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中的连接说它被放弃了,我认为问题在于这两个框架应该能够在没有我干预的情况下相互连接,但它们不能看到对方,所以我使用端口5001和5006的http://localhost向每个框架添加了InboundTransport,但随后我得到了以下错误:

代码语言:javascript
复制
[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() (带电池),但我不确定在我的localhost中运行这两个代理是否会使它们发生冲突,以及"didcomm:transport/queue“是如何工作的?我知道这个框架没有使用rest,它被go嵌入了,但是,它如何公开它的端点呢?我遗漏了什么?

谢谢

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-08-26 17:07:45

我以github issue的形式发布了这个问题,在他们发布的参考资料的帮助下,我更改了代码,并使入站工作。

以下是更改:

代码语言:javascript
复制
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,现在两个代理都可以相互连接。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68939148

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档