GunDB SEA 让其他用户写入用户空间

qwe*_*wsx 2 javascript graph gun gundb

我需要在用户空间中交换私有数据。

\n

因为gun.grantandgun.trust已被弃用,所以我遵循了这个例子:

\n

https://gun.eco/docs/SEA#quickstart \n

\r\n
\r\n
<script src="https://cdn.jsdelivr.net/npm/gun/gun.js"></script>\n<script src="https://cdn.jsdelivr.net/npm/gun/sea.js"></script>\n<script>\n// var Gun = require(\'gun\'); // in NodeJS \n// require(\'gun/sea\');\nvar SEA = Gun.SEA;\n;(async () => {\nvar pair = await SEA.pair();\nvar enc = await SEA.encrypt(\'hello self\', pair);\nvar data = await SEA.sign(enc, pair);\nconsole.log(data);\nvar msg = await SEA.verify(data, pair.pub);\nvar dec = await SEA.decrypt(msg, pair);\nvar proof = await SEA.work(dec, pair);\nvar check = await SEA.work(\'hello self\', pair);\nconsole.log(dec);\nconsole.log(proof === check);\n// now let\'s share private data with someone:\nvar alice = await SEA.pair();\nvar bob = await SEA.pair();\nvar enc = await SEA.encrypt(\'shared secret\', await SEA.secret(bob.epub, alice));\nawait SEA.decrypt(enc, await SEA.secret(alice.epub, bob));\n// `.secret` is Elliptic-curve Diffie\xe2\x80\x93Hellman\n// Bob allows Alice to write to part of his graph, he creates a certificate for Alice\nvar certificate = await SEA.certify(alice.pub, ["^AliceOnly.*"], bob)\n// Alice logs in \nconst gun = Gun();\nawait gun.user().auth(alice);\n// and uses the certificate\nawait gun.get(\'~\'+bob.pub).get(\'AliceOnly\').get(\'do-not-tell-anyone\').put(enc, null, {opt: {cert: certificate}})\nawait gun.get(\'~\'+bob.pub).get(\'AliceOnly\').get(\'do-not-tell-anyone\').once(console.log) // return \'enc\'\n})();\n</script>
Run Code Online (Sandbox Code Playgroud)\r\n
\r\n
\r\n

\n

但它总是抛出“证书验证失败”。

\n

我尝试user.authSEA.pair()但仍然不起作用

\n

Dsh*_*hiz 5

SEA.certify将替换已弃用的方法,以使其他人能够在您的图表上书写。SEA.认证

var Alice = await SEA.pair()
var Bob = await SEA.pair()
var Dave = await SEA.pair()

// Alice wants to allow Bob and Dave to use write to her "inbox" and "stories" UNTIL TOMORROW
// On Alice's side:
var certificate = await SEA.certify([Bob.pub, Dave.pub], [{"*": "inbox", "+": "*"}, {"*": "stories"}], Alice, null, {expiry: Gun.state()+(60*60*24*1000)})

// Now on Bob/Dave's side, they can write to Alice's graph using gun.put:
gun.get('~'+Alice.pub).get('inbox').get('deeper'+Bob.pub).put('hello world', null, {opt: {cert: certificate}}) // {opt: {cert: certificate}} is how you use Certificate in gun.put
Run Code Online (Sandbox Code Playgroud)

与此相关的是,这里还有一些其他非常有用的加密示例。

为了这个答案,我将发布它们:

1对1加密

///////////////////////////////////
// On my side - logged in as myself
///////////////////////////////////
var myPair = gun.user()._.sea;
// retrieve bob's user
const bob = gun.user(bobPublicKey);
// generate encryption secret using bob's epub and my pair
// this means only bob will be able to regenerate this secret with my pub key and his pair
const secret = await SEA.secret(bob.epub, myPair)
// encrypt the data using the secret
const encryptedData = await SEA.encrypt('private message for bob', secret);

////////////////////////////////////
// on Bob's side - logged in as Bob
///////////////////////////////////
const myPair = gun.user()._.sea;
// generate the secret - this will output the same secret generated by myself
// but this time we generate with bobs pair and my epub
const secret = await SEA.secret(myPair.epub, bob)
// just decrypt the data using the secret
const decryptedData = await SEA.decrypt(encryptedData, secret);
Run Code Online (Sandbox Code Playgroud)

多用户加密

(async () => {
  
  /////////////////////////////////////////////////////////////////
  // Instead of logging in with actual users, we are 
  // going to generate SEA pairs which is basically the same thing
  /////////////////////////////////////////////////////////////////
  
  // User 1 encrypts one message
  const user1 = await SEA.pair();
  
  const plainMessage = 'Hello, how are you?';
  const encryptionKey = 'this is my encryption key which is a normal string';
  const encryptedMessage = await SEA.encrypt(plainMessage, encryptionKey);
  
  // User 2, 3 and 4 will receive the message and decrypt it
  const user2 = await SEA.pair();
  const user3 = await SEA.pair();
  const user4 = await SEA.pair();
  
  // Each user gets an encrypted encryption key. If you print them, they all different
  const encryptedEncryptionKeyUser2 = await SEA.encrypt(encryptionKey, await SEA.secret(user2.epub, user1));
  const encryptedEncryptionKeyUser3 = await SEA.encrypt(encryptionKey, await SEA.secret(user3.epub, user1));
  const encryptedEncryptionKeyUser4 = await SEA.encrypt(encryptionKey, await SEA.secret(user4.epub, user1));
  
 
  // Each user decrypts his own encrypted encryption key
  // These three decrypted encryptions keys that we get are all the same
  const decryptedEncryptionKeyUser2 = await SEA.decrypt(
    encryptedEncryptionKeyUser2, 
    await SEA.secret(user1.epub, user2)
  );
  const decryptedEncryptionKeyUser3 = await SEA.decrypt(
    encryptedEncryptionKeyUser3, 
    await SEA.secret(user1.epub, user3)
  );
  const decryptedEncryptionKeyUser4 = await SEA.decrypt(
    encryptedEncryptionKeyUser4, 
    await SEA.secret(user1.epub, user4)
  );
  
  // Each user decrypts the encrypted message using the decrypted encryption key
  const decryptedMessageUser2 = await SEA.decrypt(encryptedMessage, decryptedEncryptionKeyUser2);
  const decryptedMessageUser3 = await SEA.decrypt(encryptedMessage, decryptedEncryptionKeyUser3);
  const decryptedMessageUser4 = await SEA.decrypt(encryptedMessage, decryptedEncryptionKeyUser4);
});
Run Code Online (Sandbox Code Playgroud)