You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何通过Node使用Composer-Admin API创建PeerAdmin卡片及其他卡片?

创建PeerAdmin卡片(或其他卡片)使用Composer-Admin API的方法

核心思路

Hyperledger Composer里没有直接的createPeerAdminCard专属API,但咱们可以通过手动构建Card对象再导入的方式生成PeerAdmin卡片。毕竟PeerAdmin是Fabric网络层面的管理员,卡片本质是绑定了管理员身份凭证和Fabric网络连接配置的载体;而普通业务网络的身份卡片,用createIdentity方法就能快速搞定。


一、创建PeerAdmin卡片步骤

PeerAdmin属于Fabric网络级别的管理员,得手动凑齐所有必要信息来构建卡片:

  1. 导入依赖模块
const { AdminConnection, Card } = require('composer-admin');
const { FileSystemCardStore } = require('composer-common');
  1. 初始化AdminConnection
    先指定卡片的存储位置,这里用默认的文件系统存储:
const adminConnection = new AdminConnection({ 
    cardStore: new FileSystemCardStore() 
});
  1. 准备关键信息

    • 连接配置(Connection Profile):包含Fabric网络的节点、排序器、CA等核心信息,你可以从本地JSON文件读取,也像下面示例一样直接写代码定义,记得和你的实际网络匹配。
    • Peer管理员身份凭证:从Fabric的crypto-config目录里拿,比如crypto-config/peerOrganizations/org1.example.com/users/Admin@org1.example.com/msp下的PEM格式证书和私钥。
    • 卡片名称:惯例是peerAdmin@<你的网络名称>。
  2. 构建Card对象

// 示例连接配置,根据你的实际Fabric网络修改
const connectionProfile = {
    name: 'my-fabric-network',
    type: 'hlfv1',
    profile: {
        peers: [
            {
                peer: 'peer0.org1.example.com',
                url: 'grpc://localhost:7051',
                eventUrl: 'grpc://localhost:7053',
                sslTargetNameOverride: 'peer0.org1.example.com'
            }
        ],
        orderers: [
            {
                orderer: 'orderer.example.com',
                url: 'grpc://localhost:7050'
            }
        ],
        ca: {
            ca: 'ca.org1.example.com',
            url: 'http://localhost:7054',
            name: 'ca.org1.example.com'
        },
        channel: 'mychannel',
        mspID: 'Org1MSP',
        timeout: 300
    }
};

// Peer管理员的身份信息
const adminIdentity = {
    credentials: {
        certificate: '-----BEGIN CERTIFICATE-----\n<替换成你的管理员证书内容>\n-----END CERTIFICATE-----',
        privateKey: '-----BEGIN PRIVATE KEY-----\n<替换成你的管理员私钥内容>\n-----END PRIVATE KEY-----'
    },
    mspId: 'Org1MSP', // 对应Peer组织的MSP ID
    type: 'X.509'
};

// 创建PeerAdmin卡片实例
const peerAdminCard = new Card({
    cardName: 'peerAdmin@my-fabric-network',
    connectionProfile,
    identity: adminIdentity
});
  1. 导入卡片到AdminConnection
await adminConnection.importCard('peerAdmin@my-fabric-network', peerAdminCard);

可以加一行代码验证卡片是否导入成功:

const cardExists = await adminConnection.exists('peerAdmin@my-fabric-network');
console.log(`PeerAdmin卡片是否创建成功: ${cardExists}`);

二、创建普通业务网络身份卡片

如果是给业务网络创建用户身份卡片,步骤更简单,用AdminConnection的createIdentity方法就行(前提是已经用管理员卡片连接到目标业务网络):

// 先用管理员卡片连接到业务网络
await adminConnection.connect('admin@my-business-network');

// 创建新身份,参数依次是参与者类型、身份ID、配置选项
const identityOptions = {
    issuer: false, // 是否设置为可发行新身份的角色
    name: 'user1'
};
const userCard = await adminConnection.createIdentity('org.example.User', 'user1@example.com', identityOptions);

// 把生成的卡片导入到存储中
await adminConnection.importCard('user1@my-business-network', userCard);

注意事项

  • PeerAdmin的身份必须是Fabric网络里已经注册并启用的管理员,证书和私钥一定要对应,不然卡片用不了。
  • 连接配置里的节点地址、MSP ID、通道名称这些,必须和你的实际Fabric网络完全匹配。
  • 用文件系统存储的话,卡片会自动存在用户目录下的~/.composer/cards文件夹里。

内容的提问来源于stack exchange,提问作者T_murder

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 11:53:38