calimero-wallet-utils
Version:
Calimero wallet integration utils
110 lines (109 loc) • 4.23 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.CalimeroWalletUtils = void 0;
const NearAPI = __importStar(require("near-api-js"));
class CalimeroWalletUtils {
static init(config) {
return new CalimeroWalletUtils(config);
}
constructor(config) {
this.config = config;
}
async fetchChallenge() {
const response = await fetch(`${this.config.calimeroUrl}/api/public/challenge`, {
headers: {
'Content-type': 'application/json; charset=utf-8',
},
method: 'POST',
});
if (!response.ok) {
const body = await response.text();
let parsedBody;
try {
parsedBody = JSON.parse(body);
}
catch (e) {
throw new Error(`${response.status} ${body}`);
}
throw new Error(`${response.status}, ${parsedBody}`);
}
return await response.json();
}
async signatureForChallenge(accountId, signer, challenge) {
const signed = await signer.signMessage(Buffer.from(challenge), accountId, this.config.walletNetworkId);
const signature = Buffer.from(signed.signature).toString('base64');
const publicKey = signed.publicKey.toString();
return { challenge, signature, publicKey, accountId };
}
async generatePrivateShardXSignature(accountId, signer) {
const challenge = await this.fetchChallenge();
const signedChallenge = await this.signatureForChallenge(accountId, signer, challenge.data);
const encodedSig = Buffer.from(JSON.stringify(signedChallenge)).toString('base64');
return encodedSig;
}
async syncPrivateShardAccount(accountId, signer) {
const xSignature = await this.generatePrivateShardXSignature(accountId, signer);
await this.syncAccount(accountId, xSignature);
}
async getCalimeroConnection(keyStore, xSignature) {
const calimero = await NearAPI.connect({
headers: {
'x-signature': xSignature,
},
keyStore,
networkId: this.config.walletNetworkId,
nodeUrl: this.config.rpcEndpoint,
});
return calimero;
}
async syncAccount(accountId, xSignature) {
const postData = {
accountId,
shardId: this.config.shardId,
};
const response = await fetch(`${this.config.calimeroUrl}/api/public/sync`, {
body: JSON.stringify(postData),
headers: {
'Content-type': 'application/json; charset=utf-8',
'x-signature': xSignature,
},
method: 'POST',
});
if (!response.ok) {
const body = await response.text();
let parsedBody;
try {
parsedBody = JSON.parse(body);
}
catch (e) {
throw new Error(`${response.status} ${body}`);
}
throw new Error(`${response.status}, ${parsedBody}`);
}
return await response.json();
}
}
exports.CalimeroWalletUtils = CalimeroWalletUtils;