javascript - gun db中公共(public)空间、用户空间、卡住空间的简单例子

标签 javascript authentication gun content-indexing

枪看起来很棒 - 既有用又可用!但是,我很难理解 public space put, a user space put and a frozen space put 之间的区别。我对最简单示例的尝试是:

公共(public)空间

let gun = Gun() 
gun.put('hello') // public space put anyone can edit? 

用户空间

let user = gun.user() 
user.create('Bob','password123',console.log) 
user.auth('Bob' ,'password123',console.log) 
user.get('test').put("Bob's text") 


let user2 = gun.user() 
user2.create('Eve','password456',console.log) 
user2.auth('Eve' ,'password456',console.log) 
user2.get('test').put("Eve's text") 
gun.get('test').once(console.log)

// Should this result in separately indexed entries in the db?
// Is the verification built in or up to the app developer?

卡住空间

//来自 the example :

var data = "hello world";
var hash = await SEA.work(data, null, null, {name: "SHA-256"});
gun.get('#').get(hash).put(data);

//Would not this hash key's value be replaceable by another user? Is the onus on the app developer to check result of returned result (and remove peers that send bad info)? 

假设用户可以选择任何中继服务器(包括随机修改数据的中继服务器),有多少身份验证(用户空间)和内容 ID(卡住)是在 GUN 协议(protocol)中完成的,有多少是关闭的给应用开发者?

谁能改进上面的例子?

编辑

来自文档:

This is useful, because it lets you verify that data has not been changed even if it is in a public place. And using SEA with GUN, it prevent peers from changing the data if some name/key combo of '#'+hash is used.

内容寻址,卡住空间,似乎是内置的。

最佳答案

公共(public)空间

任何人都可以编辑。

let gun = Gun() 
gun.get('foo').put({hello: 'world'})

文档:https://gun.eco/docs/Hello-World


用户空间(或 key 空间)

只能放置用用户 key 签名的数据。使用 SEA。

~ 运算符用于访问用户空间。 Gun 将其解释为“只允许将由 ~ 之后的 key 签名的数据放在此处

let Bob = await SEA.pair();
await gun.user().auth(Bob) 
gun.get('~'+Bob.pub).get('test').get('TestPropery').put("Hello from Bob",console.log) 

let Eve = await SEA.pair() 
await gun.user().auth(Eve) // comment this out and below line will fail, because authorised user Bob not Eve
gun.get('~'+Eve.pub).get('test').get('TestPropery').put("Hello from Alice",console.log) 

文档:https://gun.eco/docs/SEA#quickstart


卡住空间(哈希空间、内容ID空间)

# 运算符被使用。 Gun 解释类似“仅当其散列与附加的散列对象匹配时才允许将数据放在这里。

var data = "hello world";
var hash = await SEA.work(data, null, null, {name: "SHA-256"});
gun.get('#').get(hash).put(data);

来自文档:https://gun.eco/docs/Content-Addressing


我还观察到您可以在用户空间之上卡住空间,但反之则不行:

//works (content hash id enforced)
gun.get(pub).get('#').get('bR+eukWF7mYgxibHHRc6tJ+G6PIMEB91O1WVEbAYuWU=').put('NJViiTklbpVb2mmXmRel1cZ0F5lm6ZSTAjYg3RWhqkU.qbu9aOlUXGbrFwqZqeLdw2KiMlpj3QMbezmGRm4u7l0') 

//you can't append data to a # obj like this
gun.get('#').get('bR+eukWF7mYgxibHHRc6tJ+G6PIMEB91O1WVEbAYuWU=').get('NJViiTklbpVb2mmXmRel1cZ0F5lm6ZSTAjYg3RWhqkU.qbu9aOlUXGbrFwqZqeLdw2KiMlpj3QMbezmGRm4u7l0').put({'something':'else'}) 

关于javascript - gun db中公共(public)空间、用户空间、卡住空间的简单例子,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72016674/

相关文章:

web-services - 为外部站点(不使用 OAuth)存储用户凭据的智能方法是什么?

gun - 如何使用GUNDB上传和下载媒体文件?

php - Javascript 原型(prototype)是否类似于 PHP 静态方法?

javascript - 设置选择列表中的最大项目数 - html

javascript - 如何防止HTML表单在短时间内连续发送多次?

javascript - 为什么 Jasmine 在我提出并捕获错误时失败了?

linux - git push 两步验证失败的解决方法(linux)

java - servlet 登录应用程序中未找到列异常

javascript - 如果没有更新,如何停止接收更新?

javascript - 如何从集合中删除 Node ?