@bsv/wallet-toolbox
Version:
BRC100 conforming wallet, wallet storage and wallet signer components
187 lines • 6.55 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.WABClient = void 0;
const sdk_1 = require("@bsv/sdk");
class WABClient {
constructor(serverUrl) {
this.serverUrl = serverUrl;
}
/**
* Return the WAB server info
*/
async getInfo() {
const res = await fetch(`${this.serverUrl}/info`);
return res.json();
}
/**
* Generate a random 256-bit presentation key as a hex string (client side).
*/
generateRandomPresentationKey() {
return sdk_1.PrivateKey.fromRandom().toHex();
}
/**
* Start an Auth Method flow
*/
async startAuthMethod(authMethod, presentationKey, payload) {
return authMethod.startAuth(this.serverUrl, presentationKey, payload);
}
/**
* Complete an Auth Method flow
*/
async completeAuthMethod(authMethod, presentationKey, payload) {
return authMethod.completeAuth(this.serverUrl, presentationKey, payload);
}
/**
* List user-linked methods
*/
async listLinkedMethods(presentationKey) {
const res = await fetch(`${this.serverUrl}/user/linkedMethods`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ presentationKey })
});
return res.json();
}
/**
* Unlink a given Auth Method by ID
*/
async unlinkMethod(presentationKey, authMethodId) {
const res = await fetch(`${this.serverUrl}/user/unlinkMethod`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ presentationKey, authMethodId })
});
return res.json();
}
/**
* Request faucet
*/
async requestFaucet(presentationKey) {
const res = await fetch(`${this.serverUrl}/faucet/request`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ presentationKey })
});
return res.json();
}
/**
* Delete user
*/
async deleteUser(presentationKey) {
const res = await fetch(`${this.serverUrl}/user/delete`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ presentationKey })
});
return res.json();
}
// ============================================================
// Shamir Share Management (2-of-3 Key Recovery System)
// ============================================================
/**
* Start OTP verification for share operations
* This initiates the auth flow (e.g., sends SMS code via Twilio)
*
* @param methodType The auth method type (e.g., "TwilioPhone", "DevConsole")
* @param userIdHash SHA256 hash of the user's identity key
* @param payload Auth method specific data (e.g., { phoneNumber: "+1..." })
*/
async startShareAuth(methodType, userIdHash, payload) {
const res = await fetch(`${this.serverUrl}/auth/start`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
methodType,
presentationKey: userIdHash, // Reuse existing auth flow with userIdHash
payload
})
});
return res.json();
}
/**
* Store a Shamir share (Share B) on the server
* Requires prior OTP verification via startShareAuth
*
* @param methodType The auth method type used for verification
* @param payload Contains the OTP code and auth method specific data
* @param shareB The Shamir share to store (format: x.y.threshold.integrity)
* @param userIdHash SHA256 hash of the user's identity key
*/
async storeShare(methodType, payload, shareB, userIdHash) {
const res = await fetch(`${this.serverUrl}/share/store`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
methodType,
payload,
shareB,
userIdHash
})
});
return res.json();
}
/**
* Retrieve a Shamir share (Share B) from the server
* Requires OTP verification
*
* @param methodType The auth method type used for verification
* @param payload Contains the OTP code and auth method specific data
* @param userIdHash SHA256 hash of the user's identity key
*/
async retrieveShare(methodType, payload, userIdHash) {
const res = await fetch(`${this.serverUrl}/share/retrieve`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
methodType,
payload,
userIdHash
})
});
return res.json();
}
/**
* Update a Shamir share (for key rotation)
* Requires OTP verification
*
* @param methodType The auth method type used for verification
* @param payload Contains the OTP code and auth method specific data
* @param userIdHash SHA256 hash of the user's identity key
* @param newShareB The new Shamir share to store
*/
async updateShare(methodType, payload, userIdHash, newShareB) {
const res = await fetch(`${this.serverUrl}/share/update`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
methodType,
payload,
userIdHash,
newShareB
})
});
return res.json();
}
/**
* Delete a Shamir user's account and stored share
* Requires OTP verification
*
* @param methodType The auth method type used for verification
* @param payload Contains the OTP code and auth method specific data
* @param userIdHash SHA256 hash of the user's identity key
*/
async deleteShamirUser(methodType, payload, userIdHash) {
const res = await fetch(`${this.serverUrl}/share/delete`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
methodType,
payload,
userIdHash
})
});
return res.json();
}
}
exports.WABClient = WABClient;
//# sourceMappingURL=WABClient.js.map