wallet包含一組用戶身份,用戶運行的應用程序在連接到通道時會選擇這些身份之一,結合MSP使用此身份確定對諸如ledger之類的通道資源的訪問權限。
1、類型
wallet存儲有三種不同的形式:
- File system:最普通的,最好理解的方式,是很好的默認選擇
- In-memory:當飲用程序在受限環境中運行而無法訪問文件系統時,使用這種wallet,通常是網絡瀏覽器。這種wallet是不穩定的,應用程序正常結束或者崩潰後,身份將丟失
- CouchDB:這是用的最少的形式,但是對於那些想要使用數據庫備份和還原機制的用戶,CouchDBwallet可以提供簡化災難恢復的選擇
使用Wallets類提供的factory funciton來創建wallets。
2、結構
如下圖所示,一個wallet可以保存多個身份,每個身份由特定的ca頒發,每個身份具有一個標準的結構,包括描述性標籤、包含公鑰的X.509證書、私鑰和某些特定於Fabric的元數據。不同的wallet類型將此結構適當地映射到其存儲機制。
有一些關鍵的類方法可以簡化管理wallets和身份:
const identity: X509Identity = {
credentials: {
certificate: certificatePEM,
privateKey: privateKeyPEM,
},
mspId: 'Org1MSP',
type: 'X.509',
};
await wallet.put(identityLabel, identity);
上面建立了一個包含元數據Org1MSP、X.509證書和私鑰的身份,然後將這個身份加入到wallet中。
網關類只需要通過mspId和type元數據來設定一個身份——上面例子中的Org1MSP和X.509。網關當前使用MSPID值來表示特定的peers(connection profile中),如下面所示:
organizations:
Org1:
mspid: Org1MSP
peers:
- peer0.org1.example.com
3、操作方式
const wallet = await Wallets.newFileSystemWallet('../identity/user/isabella/wallet');
const cert = fs.readFileSync(path.join(credPath, '.../[email protected]')).toString();
const key = fs.readFileSync(path.join(credPath, '.../_sk')).toString();
const identityLabel = '[email protected]';
const identity = {
credentials: {
certificate: cert,
privateKey: key,
},
mspId: 'Org1MSP',
type: 'X.509',
};
await wallet.put(identityLabel, identity);
- 程序第一次運行時,將會在本地文件系統創建一個wallet
- 從文件系統導入cert和key
- 通過cert、key和Org1MSP建立一個新的X.509身份
- 將身份通過描述性標籤加入wallet中