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: support ipns address translations #8502

Merged
merged 3 commits into from
May 14, 2020
Merged
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
## Current Develop Branch
- [#7912](https://github.com/MetaMask/metamask-extension/pull/7912): Disable import button for empty string/file
- [#8246](https://github.com/MetaMask/metamask-extension/pull/8246): Make seed phrase import case-insensitive
- [#8502](https://github.com/MetaMask/metamask-extension/pull/8502): Add support for IPFS address resolution

## 7.7.0 Thu Nov 28 2019
- [#7004](https://github.com/MetaMask/metamask-extension/pull/7004): Connect distinct accounts per site
Expand Down
2 changes: 1 addition & 1 deletion app/scripts/controllers/preferences.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ export default class PreferencesController {
metaMetricsSendCount: 0,

// ENS decentralized website resolution
ipfsGateway: 'ipfs.dweb.link',
ipfsGateway: 'dweb.link',
}, opts.initState)

this.diagnostics = opts.diagnostics
Expand Down
2 changes: 1 addition & 1 deletion app/scripts/lib/ens-ipfs/resolver.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export default async function resolveEnsToIpfsContentId ({ provider, name }) {
let decodedContentHash = contentHash.decode(rawContentHash)
const type = contentHash.getCodec(rawContentHash)

if (type === 'ipfs-ns') {
if (type === 'ipfs-ns' || type === 'ipns-ns') {
decodedContentHash = contentHash.helpers.cidV0ToV1Base32(decodedContentHash)
}

Expand Down
4 changes: 2 additions & 2 deletions app/scripts/lib/ens-ipfs/setup.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ export default function setupEnsIpfsResolver ({ provider, getCurrentNetwork, get
let url = `https://app.ens.domains/name/${name}`
try {
const { type, hash } = await resolveEnsToIpfsContentId({ provider, name })
if (type === 'ipfs-ns') {
const resolvedUrl = `https://${hash}.${ipfsGateway}${path}${search || ''}${fragment || ''}`
if (type === 'ipfs-ns' || type === 'ipns-ns') {
const resolvedUrl = `https://${hash}.${type.slice(0, 4)}.${ipfsGateway}${path}${search || ''}${fragment || ''}`
try {
// check if ipfs gateway has result
const response = await window.fetch(resolvedUrl, { method: 'HEAD' })
Expand Down
28 changes: 28 additions & 0 deletions app/scripts/migrations/045.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
const version = 45
import { cloneDeep } from 'lodash'

/**
* Replaces {@code PreferencesController.ipfsGateway} with 'dweb.link' if set
*/
export default {
version,
migrate: async function (originalVersionedData) {
const versionedData = cloneDeep(originalVersionedData)
versionedData.meta.version = version
const state = versionedData.data
versionedData.data = transformState(state)
return versionedData
},
}

const outdatedGateways = [
'ipfs.io',
'ipfs.dweb.link',
]

function transformState (state) {
if (outdatedGateways.includes(state?.PreferencesController?.ipfsGateway)) {
state.PreferencesController.ipfsGateway = 'dweb.link'
}
return state
}
1 change: 1 addition & 0 deletions app/scripts/migrations/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ const migrations = [
require('./042').default,
require('./043').default,
require('./044').default,
require('./045').default,
]

export default migrations
92 changes: 92 additions & 0 deletions test/unit/migrations/045-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
import assert from 'assert'
import migration45 from '../../../app/scripts/migrations/045'

describe('migration #45', function () {
it('should update the version metadata', function (done) {
const oldStorage = {
'meta': {
'version': 44,
},
'data': {},
}

migration45.migrate(oldStorage)
.then((newStorage) => {
assert.deepEqual(newStorage.meta, {
'version': 45,
})
done()
})
.catch(done)
})

it('should update ipfsGateway value if outdated', function (done) {
const oldStorage = {
meta: {},
data: {
PreferencesController: {
ipfsGateway: 'ipfs.dweb.link',
bar: 'baz',
},
foo: 'bar',
},
}

migration45.migrate(oldStorage)
.then((newStorage) => {
assert.deepEqual(newStorage.data, {
PreferencesController: {
ipfsGateway: 'dweb.link',
bar: 'baz',
},
foo: 'bar',
})
done()
})
.catch(done)
})

it('should not update ipfsGateway value if custom set', function (done) {
const oldStorage = {
meta: {},
data: {
PreferencesController: {
ipfsGateway: 'blah',
bar: 'baz',
},
foo: 'bar',
},
}

migration45.migrate(oldStorage)
.then((newStorage) => {
assert.deepEqual(newStorage.data, {
PreferencesController: {
ipfsGateway: 'blah',
bar: 'baz',
},
foo: 'bar',
})
done()
})
.catch(done)
})

it('should do nothing if no PreferencesController key', function (done) {
const oldStorage = {
meta: {},
data: {
foo: 'bar',
},
}

migration45.migrate(oldStorage)
.then((newStorage) => {
assert.deepEqual(newStorage.data, {
foo: 'bar',
})
done()
})
.catch(done)
})
})