@eluvio/elv-client-js 3.1.94 → 3.1.95

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@eluvio/elv-client-js",
3
- "version": "3.1.94",
3
+ "version": "3.1.95",
4
4
  "description": "Javascript client for the Eluvio Content Fabric",
5
5
  "main": "src/ElvClient.js",
6
6
  "author": "Kevin Talmadge",
package/src/ElvClient.js CHANGED
@@ -694,6 +694,67 @@ class ElvClient {
694
694
  return this.utils.FormatAddress(this.signer.address);
695
695
  }
696
696
 
697
+ /*
698
+ TOKEN 211b PREFIX + BODY | aplsjcJf1HYcDDUuCdXcSZtU86nYK162YmYJeuqwMczEBJVkD5D5EvsBvVwYDRsf4hzDvBWMoe9piBpqx...
699
+ PREFIX 6b aplsjc | apl=plain s=ES256K jc=json-compressed
700
+ BODY 205b base58(SIGNATURE + PAYLOAD)
701
+ SIGNATURE + PAYLOAD 151b 151b * 138 / 100 + 1 = 209b (>= 205b)
702
+ SIGNATURE 66b ES256K_Di9Lu83mz4wMoehCEeQhKpJJ7ApmDZLumAa2Cge48F6EHYnbn8msATGGpjucScwimei1TWGd7aeyQY45AdXd5tT1Z
703
+ PAYLOAD 85b json-compressed
704
+ json 79b {"adr":"VVf4DQU357tDnZGYQeDrntRJ5rs=","spc":"ispc3ANoVSzNA3P6t7abLR69ho5YPPZU"}
705
+ */
706
+
707
+ /**
708
+ * Create a signed authorization token that can be used to authorize against the fabric
709
+ *
710
+ * @methodGroup Authorization
711
+ * @namedParams
712
+ * @param {number} duration=86400000 - Time until the token expires, in milliseconds (1 hour = 60 * 60 * 1000 = 3600000). Default is 24 hours.
713
+ * @param {Object=} spec - Additional attributes for this token
714
+ * @param {string=} address - Address of the signing account - if not specified, the current signer address will be used.
715
+ * @param {function=} Sign - If specified, this function will be used to produce the signature instead of the client's current signer
716
+ * @param {boolean=} addEthereumPrefix=true - If specified, the 'Ethereum Signed Message' prefixed hash format will be performed. Disable this if the provided Sign method already does this (e.g. Metamask)
717
+ */
718
+ async CreateFabricToken({
719
+ duration=24 * 60 * 60 * 1000,
720
+ spec={},
721
+ address,
722
+ Sign,
723
+ addEthereumPrefix=true,
724
+ }={}) {
725
+ address = address || this.CurrentAccountAddress();
726
+
727
+ let token = {
728
+ ...spec,
729
+ sub:`iusr${Utils.AddressToHash(address)}`,
730
+ adr: Buffer.from(address.replace(/^0x/, ""), "hex").toString("base64"),
731
+ spc: await this.ContentSpaceId(),
732
+ iat: Date.now(),
733
+ exp: Date.now() + duration,
734
+ };
735
+
736
+ if(!Sign) {
737
+ Sign = async message => this.authClient.Sign(message);
738
+ }
739
+
740
+ let message = `Eluvio Content Fabric Access Token 1.0\n${JSON.stringify(token)}`;
741
+
742
+ if(addEthereumPrefix) {
743
+ message = Ethers.utils.keccak256(Buffer.from(`\x19Ethereum Signed Message:\n${message.length}${message}`, "utf-8"));
744
+ }
745
+
746
+ const signature = await Sign(message);
747
+
748
+ const compressedToken = Pako.deflateRaw(Buffer.from(JSON.stringify(token), "utf-8"));
749
+ return `acspjc${this.utils.B58(
750
+ Buffer.concat([
751
+ Buffer.from(signature.replace(/^0x/, ""), "hex"),
752
+ Buffer.from(compressedToken)
753
+ ])
754
+ )}`;
755
+ }
756
+
757
+
697
758
  /**
698
759
  * Issue a self-signed authorization token
699
760
  *
@@ -705,7 +766,7 @@ class ElvClient {
705
766
  * @param {string=} policyId - The object ID of the policy for this token
706
767
  * @param {string=} subject - The subject of the token
707
768
  * @param {string} grantType=read - Permissions to grant for this token. Options: "access", "read", "create", "update", "read-crypt"
708
- * @param {number} duration - Time until the token expires, in milliseconds (1 hour = 60 * 60 * 1000)
769
+ * @param {number} duration - Time until the token expires, in milliseconds (1 hour = 60 * 60 * 1000 = 3600000)
709
770
  * @param {boolean} allowDecryption=false - If specified, the re-encryption key will be included in the token,
710
771
  * enabling the user of this token to download encrypted content from the specified object
711
772
  * @param {Object=} context - Additional JSON context
@@ -85,24 +85,28 @@ class RemoteSigner extends Ethers.Signer {
85
85
  */
86
86
  async signDigest(digest) {
87
87
  if(!this.signatureCache[digest]) {
88
- this.signatureCache[digest] = new Promise(async resolve => {
89
- let signature = await Utils.ResponseToJson(
90
- this.HttpClient.Request({
91
- method: "POST",
92
- path: UrlJoin("as", "wlt", "sign", "eth", this.id),
93
- headers: {
94
- Authorization: `Bearer ${this.authToken}`
95
- },
96
- body: {
97
- hash: digest
98
- }
99
- })
100
- );
101
-
102
- signature.v = parseInt(signature.v, 16);
103
- signature.recoveryParam = signature.v - 27;
104
-
105
- resolve(signature);
88
+ this.signatureCache[digest] = new Promise(async (resolve, reject) => {
89
+ try {
90
+ let signature = await Utils.ResponseToJson(
91
+ this.HttpClient.Request({
92
+ method: "POST",
93
+ path: UrlJoin("as", "wlt", "sign", "eth", this.id),
94
+ headers: {
95
+ Authorization: `Bearer ${this.authToken}`
96
+ },
97
+ body: {
98
+ hash: digest
99
+ }
100
+ })
101
+ );
102
+
103
+ signature.v = parseInt(signature.v, 16);
104
+ signature.recoveryParam = signature.v - 27;
105
+
106
+ resolve(signature);
107
+ } catch(error) {
108
+ reject(error);
109
+ }
106
110
  });
107
111
  }
108
112
 
@@ -13,25 +13,6 @@ const Test = async () => {
13
13
  });
14
14
 
15
15
  client.SetSigner({signer});
16
-
17
-
18
- const ethUrl = "https://host-216-66-40-19.contentfabric.io/eth";
19
- const asUrl = "https://host-66-220-3-86.contentfabric.io";
20
-
21
- client.SetNodes({
22
- ethereumURIs: [
23
- ethUrl
24
- ],
25
- authServiceURIs: [
26
- asUrl
27
- ]
28
- });
29
-
30
- console.log(
31
- await client.ContentObjectTenantId({
32
- objectId: "iq__3PXRZX5NCzPQsfTEquCctB2K3KJh"
33
- })
34
- )
35
16
  } catch(error) {
36
17
  console.error(error);
37
18
  console.error(JSON.stringify(error, null, 2));