Skip to content

Commit

Permalink
feat(AENS): Add ability to spend by name
Browse files Browse the repository at this point in the history
  • Loading branch information
nduchak committed Sep 25, 2019
1 parent 9fa021b commit 8850900
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 11 deletions.
7 changes: 2 additions & 5 deletions es/ae/aens.js
Original file line number Diff line number Diff line change
Expand Up @@ -173,11 +173,8 @@ async function claim (name, salt, options = {}) {
}))

const result = await this.send(claimTx, opt)

return {
...result,
...opt.waitMined && await this.aensQuery(name, opt)
}
const nameInter = this.Chain.defaults.waitMined ? await this.aensQuery(name, opt) : {}
return Object.assign(result, nameInter)
}

/**
Expand Down
4 changes: 4 additions & 0 deletions es/tx/builder/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,10 @@ function validateField (value, key, type, prefix) {
return assert((!isNaN(value) || BigNumber.isBigNumber(value)) && BigNumber(value).gte(0), { value, isMinusValue })
}
case FIELD_TYPES.id:
if (Array.isArray(prefix)) {
const p = prefix.find(p => p === value.split('_')[0])
return assert(p && PREFIX_ID_TAG[value.split('_')[0]], { value, prefix })
}
return assert(assertedType(value, prefix) && PREFIX_ID_TAG[value.split('_')[0]] && value.split('_')[0] === prefix, { value, prefix })
case FIELD_TYPES.binary:
return assert(value.split('_')[0] === prefix, { prefix, value })
Expand Down
12 changes: 6 additions & 6 deletions es/tx/builder/schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -374,14 +374,14 @@ const ACCOUNT_TX_2 = [
TX_FIELD('flags', FIELD_TYPES.int),
TX_FIELD('nonce', FIELD_TYPES.int),
TX_FIELD('balance', FIELD_TYPES.int),
TX_FIELD('gaContract', FIELD_TYPES.id, 'ct'),
TX_FIELD('gaContract', FIELD_TYPES.id, ['ct', 'nm']),
TX_FIELD('gaAuthFun', FIELD_TYPES.binary, 'cb')
]

const SPEND_TX = [
...BASE_TX,
TX_FIELD('senderId', FIELD_TYPES.id, 'ak'),
TX_FIELD('recipientId', FIELD_TYPES.id, 'ak'),
TX_FIELD('recipientId', FIELD_TYPES.id, ['ak', 'nm']),
TX_FIELD('amount', FIELD_TYPES.int),
TX_FIELD('fee', FIELD_TYPES.int),
TX_FIELD('ttl', FIELD_TYPES.int),
Expand Down Expand Up @@ -431,7 +431,7 @@ const NAME_TRANSFER_TX = [
TX_FIELD('accountId', FIELD_TYPES.id, 'ak'),
TX_FIELD('nonce', FIELD_TYPES.int),
TX_FIELD('nameId', FIELD_TYPES.id, 'nm'),
TX_FIELD('recipientId', FIELD_TYPES.id, 'ak'),
TX_FIELD('recipientId', FIELD_TYPES.id, ['ak', 'nm']),
TX_FIELD('fee', FIELD_TYPES.int),
TX_FIELD('ttl', FIELD_TYPES.int)
]
Expand Down Expand Up @@ -501,7 +501,7 @@ const CONTRACT_CALL_TX = [
...BASE_TX,
TX_FIELD('callerId', FIELD_TYPES.id, 'ak'),
TX_FIELD('nonce', FIELD_TYPES.int),
TX_FIELD('contractId', FIELD_TYPES.id, 'ct'),
TX_FIELD('contractId', FIELD_TYPES.id, ['ct', 'nm']),
TX_FIELD('abiVersion', FIELD_TYPES.int),
TX_FIELD('fee', FIELD_TYPES.int),
TX_FIELD('ttl', FIELD_TYPES.int),
Expand Down Expand Up @@ -541,7 +541,7 @@ const ORACLE_REGISTER_TX = [

const ORACLE_EXTEND_TX = [
...BASE_TX,
TX_FIELD('oracleId', FIELD_TYPES.id, 'ok'),
TX_FIELD('oracleId', FIELD_TYPES.id, ['ok', 'nm']),
TX_FIELD('nonce', FIELD_TYPES.int),
TX_FIELD('oracleTtlType', FIELD_TYPES.int),
TX_FIELD('oracleTtlValue', FIELD_TYPES.int),
Expand All @@ -553,7 +553,7 @@ const ORACLE_QUERY_TX = [
...BASE_TX,
TX_FIELD('senderId', FIELD_TYPES.id, 'ak'),
TX_FIELD('nonce', FIELD_TYPES.int),
TX_FIELD('oracleId', FIELD_TYPES.id, 'ok'),
TX_FIELD('oracleId', FIELD_TYPES.id, ['ok', 'nm']),
TX_FIELD('query', FIELD_TYPES.string),
TX_FIELD('queryFee', FIELD_TYPES.int),
TX_FIELD('queryTtlType', FIELD_TYPES.int),
Expand Down
8 changes: 8 additions & 0 deletions test/integration/aens.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ describe('Aens', function () {
configure(this)

let aens
let nameHash
const account = generateKeyPair()
const name = randomName()

Expand Down Expand Up @@ -68,12 +69,19 @@ describe('Aens', function () {

it('updates names', async () => {
const claim = await aens.aensQuery(name)
nameHash = claim.id
const address = await aens.address()
return claim.update(address).should.eventually.deep.include({
pointers: [R.fromPairs([['key', 'account_pubkey'], ['id', address]])]
})
})

it('Spend by name', async () => {
const current = await aens.address()
const onAccount = aens.addresses().find(acc => acc !== current)
await aens.spend(100, nameHash, { onAccount }).catch(async e => console.log(await e.verifyTx()))
})

it('transfers names', async () => {
const claim = await aens.aensQuery(name)

Expand Down

0 comments on commit 8850900

Please sign in to comment.