Compare commits

...

5 Commits

Author SHA1 Message Date
Jackson Mills
7f3e852dac Lint 2022-07-15 16:24:40 -07:00
Jackson Mills
d07efd5bc4 Use swap method that works with react-native 2022-07-15 15:54:08 -07:00
Jackson Mills
60e90aaed0 Add validation checks for bip39 and rfc1751 + tests 2022-07-15 15:49:54 -07:00
dependabot[bot]
14ffaae960 build(deps): bump parse-url from 6.0.0 to 6.0.2 (#2035)
Bumps [parse-url](https://github.com/IonicaBizau/parse-url) from 6.0.0 to 6.0.2.
- [Release notes](https://github.com/IonicaBizau/parse-url/releases)
- [Commits](https://github.com/IonicaBizau/parse-url/commits)

---
updated-dependencies:
- dependency-name: parse-url
  dependency-type: indirect
...

Signed-off-by: dependabot[bot] <support@github.com>

Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2022-07-12 17:04:26 -04:00
Mayukha Vadari
8eab8b3c04 ci: cache npm dependencies (#2038)
* attempt to cache

* dummy commit

* change action strategy

* simplify
2022-07-12 10:04:25 -04:00
5 changed files with 110 additions and 37 deletions

View File

@@ -18,11 +18,12 @@ jobs:
node-version: [14.x]
steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v3
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v1
uses: actions/setup-node@v3
with:
node-version: ${{ matrix.node-version }}
cache: 'npm'
- run: npm install -g npm@7
- run: npm ci
- run: npm run build
@@ -37,11 +38,13 @@ jobs:
node-version: [12.x, 14.x, 16.x]
steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v3
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v1
uses: actions/setup-node@v3
with:
node-version: ${{ matrix.node-version }}
cache: 'npm'
cache-dependency-path: '**/package-lock.json'
- run: npm install -g npm@7
- run: npm ci
- run: npm run build
@@ -63,11 +66,13 @@ jobs:
--health-cmd="wget localhost:6006 || exit 1" --health-interval=5s --health-retries=10 --health-timeout=2s
steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v3
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v1
uses: actions/setup-node@v3
with:
node-version: ${{ matrix.node-version }}
cache: 'npm'
cache-dependency-path: '**/package-lock.json'
- run: npm install -g npm@7
- run: npm ci
- run: npm run build
@@ -97,6 +102,8 @@ jobs:
uses: actions/setup-node@v1
with:
node-version: ${{ matrix.node-version }}
cache: 'npm'
cache-dependency-path: '**/package-lock.json'
- run: npm install -g npm@7
- run: npm ci
- run: npm run build

50
package-lock.json generated
View File

@@ -6998,7 +6998,8 @@
},
"node_modules/filter-obj": {
"version": "1.1.0",
"integrity": "sha1-mzERErxsYSehbgFsbF1/GeCAXFs=",
"resolved": "https://registry.npmjs.org/filter-obj/-/filter-obj-1.1.0.tgz",
"integrity": "sha512-8rXg1ZnX7xzy2NGDVkBVaAy+lSlPNwad13BtgSlLuxfIslyt5Vg64U7tFcCt4WS1R0hvtnQybT/IyCkGZ3DpXQ==",
"dev": true,
"engines": {
"node": ">=0.10.0"
@@ -12616,8 +12617,9 @@
}
},
"node_modules/parse-path": {
"version": "4.0.3",
"integrity": "sha512-9Cepbp2asKnWTJ9x2kpw6Fe8y9JDbqwahGCTvklzd/cEq5C5JC59x2Xb0Kx+x0QZ8bvNquGO8/BWP0cwBHzSAA==",
"version": "4.0.4",
"resolved": "https://registry.npmjs.org/parse-path/-/parse-path-4.0.4.tgz",
"integrity": "sha512-Z2lWUis7jlmXC1jeOG9giRO2+FsuyNipeQ43HAjqAZjwSe3SEf+q/84FGPHoso3kyntbxa4c4i77t3m6fGf8cw==",
"dev": true,
"dependencies": {
"is-ssh": "^1.3.0",
@@ -12627,13 +12629,14 @@
}
},
"node_modules/parse-url": {
"version": "6.0.0",
"integrity": "sha512-cYyojeX7yIIwuJzledIHeLUBVJ6COVLeT4eF+2P6aKVzwvgKQPndCBv3+yQ7pcWjqToYwaligxzSYNNmGoMAvw==",
"version": "6.0.2",
"resolved": "https://registry.npmjs.org/parse-url/-/parse-url-6.0.2.tgz",
"integrity": "sha512-uCSjOvD3T+6B/sPWhR+QowAZcU/o4bjPrVBQBGFxcDF6J6FraCGIaDBsdoQawiaaAVdHvtqBe3w3vKlfBKySOQ==",
"dev": true,
"dependencies": {
"is-ssh": "^1.3.0",
"normalize-url": "^6.1.0",
"parse-path": "^4.0.0",
"parse-path": "^4.0.4",
"protocols": "^1.4.0"
}
},
@@ -13105,8 +13108,9 @@
}
},
"node_modules/qs": {
"version": "6.10.1",
"integrity": "sha512-M528Hph6wsSVOBiYUnGf+K/7w0hNshs/duGsNXPUCLH5XAqjEtiPGwNONLV0tBH8NoGb0mvD5JubnUTrujKDTg==",
"version": "6.11.0",
"resolved": "https://registry.npmjs.org/qs/-/qs-6.11.0.tgz",
"integrity": "sha512-MvjoMCJwEarSbUYk5O+nmoSzSutSsTwF85zcHPQ9OrlFoZOYIjaqBAJIqIXjptyD5vThxGq52Xu/MaJzRkIk4Q==",
"dev": true,
"dependencies": {
"side-channel": "^1.0.4"
@@ -13120,6 +13124,7 @@
},
"node_modules/query-string": {
"version": "6.14.1",
"resolved": "https://registry.npmjs.org/query-string/-/query-string-6.14.1.tgz",
"integrity": "sha512-XDxAeVmpfu1/6IjyT/gXHOl+S0vQ9owggJ30hhWKdHAsNPOcasn5o9BW0eejZqL2e4vMjhAxoW3jVHcD6mbcYw==",
"dev": true,
"dependencies": {
@@ -14590,6 +14595,7 @@
},
"node_modules/split-on-first": {
"version": "1.1.0",
"resolved": "https://registry.npmjs.org/split-on-first/-/split-on-first-1.1.0.tgz",
"integrity": "sha512-43ZssAJaMusuKWL8sKUBQXHWOpq8d6CfN/u1p4gUzfJkM05C8rxTmYrkIPTXapZpORA6LkkzcUulJ8FqA7Uudw==",
"dev": true,
"engines": {
@@ -14784,7 +14790,8 @@
},
"node_modules/strict-uri-encode": {
"version": "2.0.0",
"integrity": "sha1-ucczDHBChi9rFC3CdLvMWGbONUY=",
"resolved": "https://registry.npmjs.org/strict-uri-encode/-/strict-uri-encode-2.0.0.tgz",
"integrity": "sha512-QwiXZgpRcKkhTj2Scnn++4PKtWsH0kpzZ62L2R6c/LUVYv7hVnZqcg2+sMuT6R7Jusu1vviK/MFsu6kNJfWlEQ==",
"dev": true,
"engines": {
"node": ">=4"
@@ -22214,7 +22221,8 @@
},
"filter-obj": {
"version": "1.1.0",
"integrity": "sha1-mzERErxsYSehbgFsbF1/GeCAXFs=",
"resolved": "https://registry.npmjs.org/filter-obj/-/filter-obj-1.1.0.tgz",
"integrity": "sha512-8rXg1ZnX7xzy2NGDVkBVaAy+lSlPNwad13BtgSlLuxfIslyt5Vg64U7tFcCt4WS1R0hvtnQybT/IyCkGZ3DpXQ==",
"dev": true
},
"find-cache-dir": {
@@ -26428,8 +26436,9 @@
}
},
"parse-path": {
"version": "4.0.3",
"integrity": "sha512-9Cepbp2asKnWTJ9x2kpw6Fe8y9JDbqwahGCTvklzd/cEq5C5JC59x2Xb0Kx+x0QZ8bvNquGO8/BWP0cwBHzSAA==",
"version": "4.0.4",
"resolved": "https://registry.npmjs.org/parse-path/-/parse-path-4.0.4.tgz",
"integrity": "sha512-Z2lWUis7jlmXC1jeOG9giRO2+FsuyNipeQ43HAjqAZjwSe3SEf+q/84FGPHoso3kyntbxa4c4i77t3m6fGf8cw==",
"dev": true,
"requires": {
"is-ssh": "^1.3.0",
@@ -26439,13 +26448,14 @@
}
},
"parse-url": {
"version": "6.0.0",
"integrity": "sha512-cYyojeX7yIIwuJzledIHeLUBVJ6COVLeT4eF+2P6aKVzwvgKQPndCBv3+yQ7pcWjqToYwaligxzSYNNmGoMAvw==",
"version": "6.0.2",
"resolved": "https://registry.npmjs.org/parse-url/-/parse-url-6.0.2.tgz",
"integrity": "sha512-uCSjOvD3T+6B/sPWhR+QowAZcU/o4bjPrVBQBGFxcDF6J6FraCGIaDBsdoQawiaaAVdHvtqBe3w3vKlfBKySOQ==",
"dev": true,
"requires": {
"is-ssh": "^1.3.0",
"normalize-url": "^6.1.0",
"parse-path": "^4.0.0",
"parse-path": "^4.0.4",
"protocols": "^1.4.0"
}
},
@@ -26799,8 +26809,9 @@
"dev": true
},
"qs": {
"version": "6.10.1",
"integrity": "sha512-M528Hph6wsSVOBiYUnGf+K/7w0hNshs/duGsNXPUCLH5XAqjEtiPGwNONLV0tBH8NoGb0mvD5JubnUTrujKDTg==",
"version": "6.11.0",
"resolved": "https://registry.npmjs.org/qs/-/qs-6.11.0.tgz",
"integrity": "sha512-MvjoMCJwEarSbUYk5O+nmoSzSutSsTwF85zcHPQ9OrlFoZOYIjaqBAJIqIXjptyD5vThxGq52Xu/MaJzRkIk4Q==",
"dev": true,
"requires": {
"side-channel": "^1.0.4"
@@ -26808,6 +26819,7 @@
},
"query-string": {
"version": "6.14.1",
"resolved": "https://registry.npmjs.org/query-string/-/query-string-6.14.1.tgz",
"integrity": "sha512-XDxAeVmpfu1/6IjyT/gXHOl+S0vQ9owggJ30hhWKdHAsNPOcasn5o9BW0eejZqL2e4vMjhAxoW3jVHcD6mbcYw==",
"dev": true,
"requires": {
@@ -27950,6 +27962,7 @@
},
"split-on-first": {
"version": "1.1.0",
"resolved": "https://registry.npmjs.org/split-on-first/-/split-on-first-1.1.0.tgz",
"integrity": "sha512-43ZssAJaMusuKWL8sKUBQXHWOpq8d6CfN/u1p4gUzfJkM05C8rxTmYrkIPTXapZpORA6LkkzcUulJ8FqA7Uudw==",
"dev": true
},
@@ -28105,7 +28118,8 @@
},
"strict-uri-encode": {
"version": "2.0.0",
"integrity": "sha1-ucczDHBChi9rFC3CdLvMWGbONUY=",
"resolved": "https://registry.npmjs.org/strict-uri-encode/-/strict-uri-encode-2.0.0.tgz",
"integrity": "sha512-QwiXZgpRcKkhTj2Scnn++4PKtWsH0kpzZ62L2R6c/LUVYv7hVnZqcg2+sMuT6R7Jusu1vviK/MFsu6kNJfWlEQ==",
"dev": true
},
"string_decoder": {

View File

@@ -1,7 +1,7 @@
/* eslint-disable max-lines -- There are lots of equivalent constructors which make sense to have here. */
import BigNumber from 'bignumber.js'
import { fromSeed } from 'bip32'
import { mnemonicToSeedSync } from 'bip39'
import { mnemonicToSeedSync, validateMnemonic } from 'bip39'
import _ from 'lodash'
import {
classicAddressToXAddress,
@@ -229,6 +229,12 @@ class Wallet {
})
}
// Otherwise decode using bip39's mnemonic standard
if (!validateMnemonic(mnemonic)) {
throw new ValidationError(
'Unable to parse the given mnemonic using bip39 encoding',
)
}
const seed = mnemonicToSeedSync(mnemonic)
const masterNode = fromSeed(seed)
const node = masterNode.derivePath(

View File

@@ -136,7 +136,13 @@ function getSubKey(
const ch = [0, 0, 0, 0, 0, 0, 0, 0, 0]
let word = ''
for (word of sublist) {
const idx = rfc1751WordList.indexOf(word)
const idx = rfc1751WordList.indexOf(word.toUpperCase())
if (idx === -1) {
throw new TypeError(
`Expected an RFC1751 word, but received '${word}'. ` +
`For the full list of words in the RFC1751 encoding see https://datatracker.ietf.org/doc/html/rfc1751`,
)
}
const shift = (8 - ((bits + 11) % 8)) % 8
const y = idx << shift
const cl = y >> 16
@@ -171,10 +177,14 @@ function bufferToArray(buf: Buffer): number[] {
* @returns A buffer containing the same data with reversed endianness
*/
function swap128(buf: Buffer): Buffer {
const result = Buffer.alloc(16)
result.writeBigUInt64LE(buf.readBigUInt64BE(0), 8)
result.writeBigUInt64LE(buf.readBigUInt64BE(8), 0)
return result
// Interprets buffer as an array of (two, in this case) 64-bit numbers and swaps byte order in-place.
const reversedBytes = buf.swap64()
// Swap the two 64-bit numbers since our buffer is 128 bits.
return Buffer.concat(
[reversedBytes.slice(8, 16), reversedBytes.slice(0, 8)],
16,
)
}
export { rfc1751MnemonicToKey, keyToRFC1751Mnemonic }

View File

@@ -143,6 +143,41 @@ describe('Wallet', function () {
assert.equal(wallet.seed, expectedSeed)
})
it('throws an error when using an RFC1751 mnemonic for bip39', function () {
const algorithm = ECDSA.ed25519
const mnemonic =
'CAB BETH HANK BIRD MEND SIGN GILD ANY KERN HYDE CHAT STUB'
assert.throws(() => {
Wallet.fromMnemonic(mnemonic, {
mnemonicEncoding: 'bip39',
algorithm,
})
}, /^Unable to parse the given mnemonic using bip39 encoding$/u)
})
it('throws an error when using an bip39 mnemonic for RFC1751', function () {
const mnemonic =
'draw attack antique swing base employ blur above palace lucky glide clap pen use illegal'
assert.throws(() => {
Wallet.fromMnemonic(mnemonic, {
mnemonicEncoding: 'rfc1751',
})
}, /^Expected an RFC1751 word, but received 'attack'\. For the full list of words in the RFC1751 encoding see https:\/\/datatracker\.ietf\.org\/doc\/html\/rfc1/u)
})
it('derives a wallet using rfc1751 mnemonic with lowercase words', function () {
const algorithm = ECDSA.ed25519
const mnemonic =
'cab beth hank bird mend sign gild any kern hyde chat stub'
const expectedSeed = 'sEdVaw4m9W3H3ou3VnyvDwvPAP5BEz1'
const wallet = Wallet.fromMnemonic(mnemonic, {
mnemonicEncoding: 'rfc1751',
algorithm,
})
assert.equal(wallet.seed, expectedSeed)
})
it('derives a wallet using a Regular Key Pair', function () {
const masterAddress = 'rUAi7pipxGpYfPNg3LtPcf2ApiS8aw9A93'
const regularKeyPair = {
@@ -211,11 +246,11 @@ describe('Wallet', function () {
describe('fromMnemonic', function () {
const mnemonic =
'try milk link drift aware pass obtain again music stick pluck fold'
'assault rare scout seed design extend noble drink talk control guitar quote'
const publicKey =
'0257B550BA2FDCCF0ADDA3DEB2A5411700F3ADFDCC7C68E1DCD1E2B63E6B0C63E6'
'035953FCD81D001CF634EB44A87940F3F98ADF2483D09C914BAED0539BE50F385D'
const privateKey =
'008F942B6E229C0E9CEE47E7A94253DABB6A9855F4BA2D8A741FA31851A1D423C3'
'0013FC461CA5799F1357C8130AF703CBA7E9C28E072C6CA8F7DEF8601CDE98F394'
it('derives a wallet using default derivation path', function () {
const wallet = Wallet.fromMnemonic(mnemonic)
@@ -235,15 +270,16 @@ describe('Wallet', function () {
it('derives a wallet using a Regular Key Pair', function () {
const masterAddress = 'rUAi7pipxGpYfPNg3LtPcf2ApiS8aw9A93'
const regularKeyPair = {
mnemonic: 'KNEW BENT LYNN LED GAD BEN KENT SHAM HOBO RINK WALT ALLY',
mnemonic: 'I IRE BOND BOW TRIO LAID SEAT GOAL HEN IBIS IBIS DARE',
publicKey:
'02FBC77338A52D9733641437A77369ACB0D89D52642740A008509F7A3A7450C841',
'0330E7FC9D56BB25D6893BA3F317AE5BCF33B3291BD63DB32654A313222F7FD020',
privateKey:
'007A10DF756751129060DD29C9BB6733ADB92507B7DD83BB0795CAA09FB815BE22',
'001ACAAEDECE405B2A958212629E16F2EB46B153EEE94CDD350FDEFF52795525B7',
}
const wallet = Wallet.fromMnemonic(regularKeyPair.mnemonic, {
masterAddress,
mnemonicEncoding: 'rfc1751',
})
assert.equal(wallet.publicKey, regularKeyPair.publicKey)
@@ -253,7 +289,7 @@ describe('Wallet', function () {
})
describe('fromEntropy', function () {
let entropy
let entropy: number[]
const publicKey =
'0390A196799EE412284A5D80BF78C3E84CBB80E1437A0AECD9ADF94D7FEAAFA284'
const privateKey =