Skip to content
This repository has been archived by the owner on Apr 4, 2022. It is now read-only.

Commit

Permalink
handle validation for substrate addresses (relaxed)
Browse files Browse the repository at this point in the history
  • Loading branch information
tboeckmann committed Jul 26, 2020
1 parent 6955f4b commit 3597483
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 48 deletions.
81 changes: 50 additions & 31 deletions src/app/inbox/inbox-compose/inbox-compose.component.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -346,40 +346,59 @@ describe('InboxComposeComponent', () => {
})

describe('resolveAddress', () => {
it('should call nameserviceService.resolveName if given a name-like value', () => {
spyOn(nameserviceService, 'resolveName').and.callThrough()
component.resolveAddress(ensName)
expect(nameserviceService.resolveName).toHaveBeenCalled()

})
it('should call nameserviceService.resolveName with params for protocol, network & name-like value', () => {
spyOn(nameserviceService, 'resolveName').and.callThrough()
component.resolveAddress(ensName)
expect(nameserviceService.resolveName).toHaveBeenCalledWith(
component.currentProtocol,
component.currentNetwork,
ensName
)
})
it('should return an observable with body containing address hash if given a name-like value', async () => {
let obs = await component.resolveAddress(ensName)
let expectedBody = { address: currentAccount }
obs.subscribe(res => {
expect(res['body']).toEqual(expectedBody)
describe('in ethereum', () => {

it('should call nameserviceService.resolveName if given a name-like value', () => {
spyOn(nameserviceService, 'resolveName').and.callThrough()
component.currentProtocol = 'ethereum'
component.resolveAddress(ensName)
expect(nameserviceService.resolveName).toHaveBeenCalled()

})
})
it('should return an observable with body containing address hash if given an address-like value', async () => {
let obs = await component.resolveAddress(currentAccount)
let expectedBody = { address: currentAccount }
obs.subscribe(res => {
expect(res['body']).toEqual(expectedBody)
it('should call nameserviceService.resolveName with params for protocol, network & name-like value ', () => {
spyOn(nameserviceService, 'resolveName').and.callThrough()
component.currentProtocol = 'ethereum'
component.resolveAddress(ensName)
expect(nameserviceService.resolveName).toHaveBeenCalledWith(
component.currentProtocol,
component.currentNetwork,
ensName
)
})
it('should return an observable with body containing address hash if given a name-like value', async () => {
component.currentProtocol = 'ethereum'
let obs = await component.resolveAddress(ensName)
let expectedBody = { address: currentAccount }
obs.subscribe(res => {
expect(res['body']).toEqual(expectedBody)
})
})
it('should return an observable with body containing address hash if given an address-like value', async () => {
component.currentProtocol = 'ethereum'
let obs = await component.resolveAddress(currentAccount)
let expectedBody = { address: currentAccount }
obs.subscribe(res => {
expect(res['body']).toEqual(expectedBody)
})
})
it('should return an observable with body containing empty address hash if given a value that is not name-like or address-like', async () => {
component.currentProtocol = 'ethereum'
let obs = await component.resolveAddress('string')
let expectedBody = { address: '' }
obs.subscribe(res => {
expect(res['body']).toEqual(expectedBody)
})
})
})
it('should return an observable with body containing empty address hash if given a value that is not name-like or address-like', async () => {
let obs = await component.resolveAddress('string')
let expectedBody = { address: '' }
obs.subscribe(res => {
expect(res['body']).toEqual(expectedBody)
describe('in substrate', () => {

it('should return an observable with body containing address hash if given an address-like value', async () => {
component.currentProtocol = 'substrate'
let obs = await component.resolveAddress(currentAccount)
let expectedBody = { address: currentAccount }
obs.subscribe(res => {
expect(res['body']).toEqual(expectedBody)
})
})
})
})
Expand Down
48 changes: 31 additions & 17 deletions src/app/inbox/inbox-compose/inbox-compose.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -194,15 +194,17 @@ export class InboxComposeComponent implements OnInit {
).subscribe((res) => {
res.subscribe(val => {
let address = val['body']['address']
if (this.mailchainService.validateEthAddress(address)) {
if (
(this.currentProtocol == 'ethereum' && this.mailchainService.validateEthAddress(address)) ||
(this.currentProtocol == 'substrate' && this.mailchainService.validateSubstrateAddress(address))
) {
this.model.to = address
this.setRecipientLoadingIcon('valid')
this.setRecipientLoadingText('valid address')
} else {
this.setRecipientLoadingIcon('invalid')
this.setRecipientLoadingText('invalid address')
}

}, err => {
this.setRecipientLoadingIcon('invalid')
this.setRecipientLoadingText(err['error']['message'])
Expand All @@ -228,21 +230,33 @@ export class InboxComposeComponent implements OnInit {
public async resolveAddress(value) {
let returnObs

if (this.mailchainService.validateEnsName(value)) {
returnObs = await this.nameserviceService.resolveName(
this.currentProtocol,
this.currentNetwork,
value
)
} else if (this.mailchainService.validateEthAddress(value)) {
returnObs = of(
{ body: { address: value } }
)
} else {
returnObs = of(
{ body: { address: '' } }
)
}
if (this.currentProtocol == 'ethereum') {
if (this.mailchainService.validateEnsName(value)) {
returnObs = await this.nameserviceService.resolveName(
this.currentProtocol,
this.currentNetwork,
value
)
} else if (this.mailchainService.validateEthAddress(value)) {
returnObs = of(
{ body: { address: value } }
)
} else {
returnObs = of(
{ body: { address: '' } }
)
}
} else if (this.currentProtocol == 'substrate') {
if (this.mailchainService.validateSubstrateAddress(value)) {
returnObs = of(
{ body: { address: value } }
)
} else {
returnObs = of(
{ body: { address: '' } }
)
}
};

return returnObs

Expand Down
11 changes: 11 additions & 0 deletions src/app/services/mailchain/mailchain.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,17 @@ export class MailchainService {
return regex.test(value)
}

/**
* tests the value matches the Substrate Address Regex
* @param value the address value to test, e.g. TODOTODO
* expects TODODO
*/
public validateSubstrateAddress(value) {
// let regex = new RegExp('0x[0-9a-fA-F]{40}$');
// return regex.test(value)
return true
}

/**
* getContentTypeForView: determines how the application should handle various content types in messages.
* Returns `html` or `plaintext` based on the contentType provided.
Expand Down

0 comments on commit 3597483

Please sign in to comment.