gun db 中公共空间、用户空间和冻结空间的简单示例

2024-01-25

枪看起来很棒——既有用又好用!然而,我很难理解之间的区别public空间放置,auser空间放置和frozen空间放置 https://gun.eco/docs/Introduction#data。我尝试的最简单的例子是:

公共场所

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?

冰冻空间

// From 这个例子 https://gun.eco/docs/Content-Addressing:

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 协议中完成的,有多少取决于应用程序开发商?

有人可以改进上面的例子吗?

EDIT

来自文档:

这很有用,因为它可以让您验证数据是否未被 即使是在公共场所也发生了变化。并使用 SEA 和 GUN,它 如果某些名称/键组合阻止对等方更改数据 使用“#”+哈希。

看来内容地址,冰冻空间,内置。


公共场所

任何人都可以编辑。

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

Docs: https://gun.eco/docs/Hello-World https://gun.eco/docs/Hello-World


用户空间(或键空间)

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

The ~运算符用于访问用户空间。 Gun 将其解释为“只允许由以下密钥签名的数据~放在这里"

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) 

Docs: https://gun.eco/docs/SEA#quickstart https://gun.eco/docs/SEA#quickstart


冰冻空间(哈希空间、内容 ID 空间)

The #使用运算符。 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 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'}) 
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

gun db 中公共空间、用户空间和冻结空间的简单示例 的相关文章

随机推荐