Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add IPNS extendable data #309

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -160,17 +160,17 @@ const defaultCreateOptions: CreateOptions = {
* @param {number} lifetime - lifetime of the record (in milliseconds).
* @param {CreateOptions} options - additional create options.
*/
export async function create (peerId: PeerId, value: CID | PeerId | string, seq: number | bigint, lifetime: number, options?: CreateV2OrV1Options): Promise<IPNSRecordV1V2>
export async function create (peerId: PeerId, value: CID | PeerId | string, seq: number | bigint, lifetime: number, options: CreateV2Options): Promise<IPNSRecordV2>
export async function create (peerId: PeerId, value: CID | PeerId | string, seq: number | bigint, lifetime: number, options: CreateOptions): Promise<IPNSRecordV1V2>
export async function create (peerId: PeerId, value: CID | PeerId | string, seq: number | bigint, lifetime: number, options: CreateOptions = defaultCreateOptions): Promise<IPNSRecord> {
export async function create (peerId: PeerId, value: CID | PeerId | string, seq: number | bigint, lifetime: number, options?: CreateV2OrV1Options, kv_data? : Object): Promise<IPNSRecordV1V2>
export async function create (peerId: PeerId, value: CID | PeerId | string, seq: number | bigint, lifetime: number, options: CreateV2Options, kv_data? : Object): Promise<IPNSRecordV2>
export async function create (peerId: PeerId, value: CID | PeerId | string, seq: number | bigint, lifetime: number, options: CreateOptions, kv_data? : Object): Promise<IPNSRecordV1V2>
export async function create (peerId: PeerId, value: CID | PeerId | string, seq: number | bigint, lifetime: number, options: CreateOptions = defaultCreateOptions, kv_data : Object = {}): Promise<IPNSRecord> {
// Validity in ISOString with nanoseconds precision and validity type EOL
const expirationDate = new NanoDate(Date.now() + Number(lifetime))
const validityType = IpnsEntry.ValidityType.EOL
const [ms, ns] = lifetime.toString().split('.')
const lifetimeNs = (BigInt(ms) * BigInt(100000)) + BigInt(ns ?? '0')

return _create(peerId, value, seq, validityType, expirationDate.toString(), lifetimeNs, options)
return _create(peerId, value, seq, validityType, expirationDate.toString(), lifetimeNs, options, kv_data)
}

/**
Expand Down Expand Up @@ -202,7 +202,7 @@ export async function createWithExpiration (peerId: PeerId, value: CID | PeerId
return _create(peerId, value, seq, validityType, expirationDate.toString(), ttlNs, options)
}

const _create = async (peerId: PeerId, value: CID | PeerId | string, seq: number | bigint, validityType: IpnsEntry.ValidityType, validity: string, ttl: bigint, options: CreateOptions = defaultCreateOptions): Promise<IPNSRecord> => {
const _create = async (peerId: PeerId, value: CID | PeerId | string, seq: number | bigint, validityType: IpnsEntry.ValidityType, validity: string, ttl: bigint, options: CreateOptions = defaultCreateOptions, kv_data: object = {}): Promise<IPNSRecord> => {
seq = BigInt(seq)
const isoValidity = uint8ArrayFromString(validity)
const normalizedValue = normalizeValue(value)
Expand All @@ -213,7 +213,7 @@ const _create = async (peerId: PeerId, value: CID | PeerId | string, seq: number
}

const privateKey = await unmarshalPrivateKey(peerId.privateKey)
const data = createCborData(encodedValue, validityType, isoValidity, seq, ttl)
const data = createCborData(encodedValue, validityType, isoValidity, seq, ttl, kv_data)
const sigData = ipnsRecordDataForV2Sig(data)
const signatureV2 = await privateKey.sign(sigData)
let pubKey: Uint8Array | undefined
Expand Down
5 changes: 3 additions & 2 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ export const peerIdFromRoutingKey = (key: Uint8Array): PeerId => {
return peerIdFromBytes(key.slice(IPNS_PREFIX.length))
}

export const createCborData = (value: Uint8Array, validityType: IpnsEntry.ValidityType, validity: Uint8Array, sequence: bigint, ttl: bigint): Uint8Array => {
export const createCborData = (value: Uint8Array, validityType: IpnsEntry.ValidityType, validity: Uint8Array, sequence: bigint, ttl: bigint, kv_data : object = {}): Uint8Array => {
let ValidityType

if (validityType === IpnsEntry.ValidityType.EOL) {
Expand All @@ -178,7 +178,8 @@ export const createCborData = (value: Uint8Array, validityType: IpnsEntry.Validi
Validity: validity,
ValidityType,
Sequence: sequence,
TTL: ttl
TTL: ttl,
data : kv_data
}

return cborg.encode(data)
Expand Down
101 changes: 101 additions & 0 deletions test/extensible_data.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
/* eslint-env mocha */

import { generateKeyPair } from '@libp2p/crypto/keys'
import { peerIdFromKeys } from '@libp2p/peer-id'
import { expect } from 'aegir/chai'
import * as ipns from '../src/index.js'
import { IpnsEntry } from '../src/pb/ipns.js'
import type { PeerId } from '@libp2p/interface'
import { parseCborData } from "../src/utils.js"

const defaultCreateOptions: Object = {
v1Compatible: true
}

describe('ipns extensible data', function () {
this.timeout(20 * 1000)

const contentPath = '/ipfs/bafkqae3imvwgy3zamzzg63janjzs22lqnzzqu'
let peerId: PeerId

before(async () => {
const rsa = await generateKeyPair('RSA', 2048)
peerId = await peerIdFromKeys(rsa.public.bytes, rsa.bytes)
})



it('should be able add a kv store to IPFS record', async () => {
const sequence = 0
const validity = 1000000

let kv_data = {"KEY" : "VALUE"}
const record = await ipns.create(peerId, contentPath, sequence, validity, defaultCreateOptions, { kv_data : kv_data })
expect(record.pubKey).to.equalBytes(peerId.publicKey)

let raw_parsed_cbor_data = parseCborData(record.data)
let parsed_cbor_data = raw_parsed_cbor_data as any;


// console.log("\nparsed_cbor_data")
// console.log(raw_parsed_cbor_data)
// console.log("\nrecord.data")
// console.log(record.data)
// console.log("Object.keys(parsed_cbor_data)")
// console.log(Object.keys(raw_parsed_cbor_data))
// console.log("raw_parsed_cbor_data.data")
// // console.log(parsed_cbor_data) // Produces error, "Property 'data' does not exist on type 'IPNSRecordData'."
// console.log("parsed_cbor_data.data")
// console.log(parsed_cbor_data.data)
// console.log("\n\n")
// console.log(parsed_cbor_data.data)
// console.log({ kv_data : kv_data })


expect(parsed_cbor_data.data.kv_data.KEY).to.equal("VALUE")

const pb = IpnsEntry.decode(ipns.marshal(record))
expect(pb.pubKey).to.equalBytes(peerId.publicKey)
})


it('should be able add a raw object as kv store to IPFS record', async () => {
const sequence = 0
const validity = 1000000

let kv_data = {
"KEY" : "VALUE",
"KEY2" : "VALUE2"
}
const record = await ipns.create(peerId, contentPath, sequence, validity, defaultCreateOptions, kv_data)
expect(record.pubKey).to.equalBytes(peerId.publicKey)

let raw_parsed_cbor_data = parseCborData(record.data)
let parsed_cbor_data = raw_parsed_cbor_data as any;


// console.log("\nparsed_cbor_data")
// console.log(raw_parsed_cbor_data)
// console.log("\nrecord.data")
// console.log(record.data)
// console.log("Object.keys(parsed_cbor_data)")
// console.log(Object.keys(raw_parsed_cbor_data))
// console.log("raw_parsed_cbor_data.data")
// // console.log(parsed_cbor_data) // Produces error, "Property 'data' does not exist on type 'IPNSRecordData'."
// console.log("parsed_cbor_data.data")
// console.log(parsed_cbor_data.data)
// console.log("\n\n")
// console.log(parsed_cbor_data.data)
// console.log({ kv_data : kv_data })


expect(parsed_cbor_data.data.KEY).to.equal("VALUE")
expect(parsed_cbor_data.data.KEY2).to.equal("VALUE2")

const pb = IpnsEntry.decode(ipns.marshal(record))
expect(pb.pubKey).to.equalBytes(peerId.publicKey)
})



})