|
|
@@ -10,10 +10,29 @@ import WebClientNode from './webclient-node';
|
|
|
|
|
|
export class BankClient {
|
|
|
|
|
|
+ public static parseBankLink(bankLink: string) {
|
|
|
+ if (!bankLink.startsWith('bank:')) {
|
|
|
+ throw new Error('address must start with bank:');
|
|
|
+ }
|
|
|
+ const deprefixed = bankLink.substring(5);
|
|
|
+ let host: string | undefined;
|
|
|
+ let address: string;
|
|
|
+ let topic: string;
|
|
|
+ if (deprefixed[0] === '/' && deprefixed[1] === '/') {
|
|
|
+ [ host, address, topic ] = deprefixed.substring(2).split('/');
|
|
|
+ } else {
|
|
|
+ [ address, topic ] = deprefixed.split('/');
|
|
|
+ }
|
|
|
+ if (!address || !topic) {
|
|
|
+ throw new Error('cannot parse address and topic');
|
|
|
+ }
|
|
|
+ return { host, address, topic };
|
|
|
+ }
|
|
|
+
|
|
|
private privateKey: RsaPrivateKey | undefined;
|
|
|
private bootstrapPromise: any;
|
|
|
private bootstrapResult: any;
|
|
|
-
|
|
|
+
|
|
|
constructor(private urlBase: string, private ipfsUrlBase: string, private storage: Storage = new Storage('bankClient'), private webClient: IWebClient = new WebClientNode()) {
|
|
|
|
|
|
}
|
|
|
@@ -115,12 +134,16 @@ export class BankClient {
|
|
|
}
|
|
|
};
|
|
|
}
|
|
|
+ if (params.links) {
|
|
|
+ formData.links = JSON.stringify(params.links);
|
|
|
+ }
|
|
|
for (const attr of ['title', 'text', 'type']) {
|
|
|
if (params[attr] != null) {
|
|
|
formData[attr] = params[attr];
|
|
|
}
|
|
|
}
|
|
|
// console.log('formData', formData);
|
|
|
+
|
|
|
const uploadResponse: any = await this.webClient.requestJSON({
|
|
|
formData,
|
|
|
method: 'POST',
|
|
|
@@ -157,10 +180,9 @@ export class BankClient {
|
|
|
return uploadResponse.hash;
|
|
|
}
|
|
|
|
|
|
- public async appendBank(bankLink: string, itemHash: string): Promise<void> {
|
|
|
+ public async appendBank(bankAddress: string, bankTopic: string, itemHash: string): Promise<void> {
|
|
|
const payload = await this.makePlaintextPayload(itemHash);
|
|
|
- const { address, topic } = this.parseBankLink(bankLink);
|
|
|
- const topicURL = this.urlBase + '/bank/private/' + encodeURIComponent(address) + '/' + encodeURIComponent(topic);
|
|
|
+ const topicURL = this.urlBase + '/bank/private/' + encodeURIComponent(bankAddress) + '/' + encodeURIComponent(bankTopic);
|
|
|
await this.webClient.requestJSON({
|
|
|
body: payload,
|
|
|
method: 'PUT',
|
|
|
@@ -272,16 +294,4 @@ export class BankClient {
|
|
|
});
|
|
|
});
|
|
|
}
|
|
|
-
|
|
|
- private parseBankLink(bankLink: string) {
|
|
|
- if (!bankLink.startsWith('bank:')) {
|
|
|
- throw new Error('address must start with bank:');
|
|
|
- }
|
|
|
- const [ address, topic ] = bankLink.substring(5).split('/');
|
|
|
- if (!address || !topic) {
|
|
|
- throw new Error('cannot parse address and topic');
|
|
|
- }
|
|
|
- return { address, topic };
|
|
|
- }
|
|
|
-
|
|
|
}
|