Skip to content

Commit

Permalink
Shelley address generation - proof of concept
Browse files Browse the repository at this point in the history
  • Loading branch information
elfinka committed Jul 10, 2020
1 parent 6b7fb24 commit 00fb999
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 2 deletions.
4 changes: 4 additions & 0 deletions app/frontend/wallet/helpers/mnemonicToWalletSecretDef.ts
Expand Up @@ -3,8 +3,12 @@ import {decodePaperWalletMnemonic, mnemonicToRootKeypair} from 'cardano-crypto.j
import {validateMnemonic, isMnemonicInPaperWalletFormat} from '../mnemonic'

import derivationSchemes from './derivation-schemes'
import {ADALITE_CONFIG} from '../../config'

const guessDerivationSchemeFromMnemonic = (mnemonic) => {
if (ADALITE_CONFIG.ADALITE_CARDANO_VERSION === 'shelley') {
return derivationSchemes.v2 //TODO: v1 should have been abandoned in shelley (?)
}
return mnemonic.split(' ').length === 12 ? derivationSchemes.v1 : derivationSchemes.v2
}

Expand Down
11 changes: 11 additions & 0 deletions app/frontend/wallet/shelley/helpers/addresses.ts
@@ -1,5 +1,6 @@
import * as lib from '@emurgo/js-chain-libs'
import bech32 from './bech32'
import {deriveBaseAddress} from 'cardano-crypto.js'
const {Address, Account, AddressDiscrimination, PublicKey, AddressKind} = lib

const getDiscriminator = (network) => {
Expand Down Expand Up @@ -66,6 +67,16 @@ export const groupAddressFromXpub = (spendXpub: Xpub, stakeXpub: Xpub, network:
return _addr.to_string('addr')
}

export const baseAddressFromXpub = (spendXpub: Xpub, stakeXpub: Xpub, account, networkId) => {
const addrBuffer = deriveBaseAddress(
spendXpub.slice(0, 32),
stakeXpub.slice(0, 32),
account,
networkId
)
return bech32.encode({prefix: 'addr', data: addrBuffer})
}

const getAddressKind = (address: string) => {
// separate get_kind method since chain-libs throw error with legacy
// addresses TODO: refactor
Expand Down
18 changes: 18 additions & 0 deletions app/frontend/wallet/shelley/shelley-address-provider.ts
Expand Up @@ -3,6 +3,7 @@ import {
accountAddressFromXpub,
singleAddressFromXpub,
groupAddressFromXpub,
baseAddressFromXpub,
} from './helpers/addresses'

const shelleyPath = (account: number, isChange: boolean, addrIdx: number) => {
Expand Down Expand Up @@ -73,3 +74,20 @@ export const ShelleyGroupAddressProvider = (
address: groupAddressFromXpub(spendXpub, stakeXpub, cryptoProvider.network),
}
}

export const ShelleyBaseAddressProvider = (
cryptoProvider,
accountIndex: number,
isChange: boolean
) => async (i: number) => {
const pathSpend = shelleyPath(accountIndex, isChange, i)
const spendXpub = await cryptoProvider.deriveXpub(pathSpend)

const pathStake = shelleyStakeAccountPath(accountIndex)
const stakeXpub = await cryptoProvider.deriveXpub(pathStake)

return {
path: pathSpend,
address: baseAddressFromXpub(spendXpub, stakeXpub, accountIndex, 0), //TODO: get network from crypto provider
}
}
19 changes: 17 additions & 2 deletions app/tests/src/shelley.js
Expand Up @@ -3,7 +3,10 @@ import assert from 'assert'

import ShelleyJsCryptoProvider from '../../frontend/wallet/shelley/shelley-js-crypto-provider'

import {ShelleyGroupAddressProvider} from '../../frontend/wallet/shelley/shelley-address-provider'
import {
ShelleyBaseAddressProvider,
ShelleyGroupAddressProvider,
} from '../../frontend/wallet/shelley/shelley-address-provider'
import {buildTransaction} from '../../frontend/wallet/shelley/helpers/chainlib-wrapper'
import mnemonicToWalletSecretDef from '../../frontend/wallet/helpers/mnemonicToWalletSecretDef'
import _ from 'lodash'
Expand Down Expand Up @@ -61,9 +64,21 @@ describe('shelley address derivation', () => {

assert.equal(address, expected)
})

const mnemonic3 = 'test walk nut penalty hip pave soap entry language right filter choice'
it('should derive base address from cardano shelley test vectors', async () => {
const cp = await getCryptoProvider(mnemonic3, 'testnet') //TODO: change discriminator to networkId for shelley
const addrGen = ShelleyBaseAddressProvider(cp, 0, false)

const {address} = await addrGen(0)
const expected =
'addr1qz2fxv2umyhttkxyxp8x0dlpdt3k6cwng5pxj3jhsydzer3jcu5d8ps7zex2k2xt3uqxgjqnnj83ws8lhrn648jjxtwqcyl47r'

assert.equal(address, expected)
})
})

describe('legacy utxo daedalus witness computation', () => {
describe.skip('legacy utxo daedalus witness computation', () => {
it('should sign legacy daedalus witness correctly', async () => {
const buildData = {
inputs: [
Expand Down

0 comments on commit 00fb999

Please sign in to comment.