gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-05 03:25:21 24 4
gpt4 key购买 nike

枪看起来很棒 - 既有用又可用!但是,我很难理解 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 beenchanged even if it is in a public place. And using SEA with GUN, itprevent 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com