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

Commit

Permalink
feat: use publicidentity as alternative to contact (#262)
Browse files Browse the repository at this point in the history
  • Loading branch information
tjwelde authored Apr 27, 2020
1 parent 9a89437 commit 1675017
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 10 deletions.
4 changes: 3 additions & 1 deletion src/components/MessageDetailView/MessageDetailView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,9 @@ class MessageDetailView extends React.Component<Props, State> {
<ClaimDetailView
claim={(message.body as sdk.IRequestTerms).content}
/>
{showTask ? (
{showTask && message.sender ? (
<SubmitLegitimations
receiver={message.sender.publicIdentity}
receiverAddresses={[message.senderAddress]}
claim={(message.body as sdk.IRequestTerms).content}
onCancel={this.handleCancel}
Expand Down Expand Up @@ -134,6 +135,7 @@ class MessageDetailView extends React.Component<Props, State> {
return (
<AttestClaim
claimerAddresses={[message.senderAddress]}
claimer={message.sender?.publicIdentity}
requestForAttestation={
(message.body as sdk.IRequestAttestationForClaim).content
.requestForAttestation
Expand Down
11 changes: 9 additions & 2 deletions src/containers/Tasks/AttestClaim/AttestClaim.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { BlockUi } from '../../../types/UserFeedback'
type Props = {
claimerAddresses: Array<IContact['publicIdentity']['address']>
requestForAttestation: sdk.IRequestForAttestation
claimer?: sdk.IPublicIdentity

onCancel?: () => void
onFinished?: () => void
Expand All @@ -35,15 +36,21 @@ class AttestClaim extends React.Component<Props, State> {
}

private attestClaim(): void {
const { requestForAttestation, onFinished, claimerAddresses } = this.props
const {
requestForAttestation,
onFinished,
claimerAddresses,
claimer,
} = this.props
const blockUi: BlockUi = FeedbackService.addBlockUi({
headline: 'Writing attestation to chain',
})

attestationWorkflow
.approveAndSubmitAttestationForClaim(
requestForAttestation,
claimerAddresses[0]
claimerAddresses[0],
claimer
)
.then(() => {
blockUi.remove()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import './SubmitLegitimations.scss'
export type SubmitLegitimationsProps = {
claim: sdk.IPartialClaim
receiverAddresses: Array<IContact['publicIdentity']['address']>
receiver?: sdk.IPublicIdentity

enablePreFilledClaim?: boolean

Expand Down Expand Up @@ -121,6 +122,7 @@ class SubmitLegitimations extends React.Component<Props, State> {
const {
getAttestedClaims,
enablePreFilledClaim,
receiver,
receiverAddresses,
onFinished,
} = this.props
Expand All @@ -134,6 +136,7 @@ class SubmitLegitimations extends React.Component<Props, State> {
claim,
getAttestedClaims(),
receiverAddresses,
receiver,
selectedDelegation
).then(() => {
if (onFinished) {
Expand Down
25 changes: 21 additions & 4 deletions src/services/AttestationWorkflow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ class AttestationWorkflow {
claim: IPartialClaim,
legitimations: sdk.IAttestedClaim[],
receiverAddresses: Array<IContact['publicIdentity']['address']>,
receiver?: sdk.IPublicIdentity,
delegation?: IMyDelegation
): Promise<void> {
const messageBody: sdk.ISubmitTerms = {
Expand All @@ -64,6 +65,9 @@ class AttestationWorkflow {
messageBody.content.delegationId = delegation.id
}

if (receiver) {
return MessageRepository.sendToPublicIdentity(receiver, messageBody)
}
return MessageRepository.sendToAddresses(receiverAddresses, messageBody)
}

Expand Down Expand Up @@ -128,10 +132,11 @@ class AttestationWorkflow {
*/
public static async approveAndSubmitAttestationForClaim(
requestForAttestation: sdk.IRequestForAttestation,
claimerAddress: IContact['publicIdentity']['address']
claimerAddress: IContact['publicIdentity']['address'],
claimerIdentity?: sdk.IPublicIdentity
): Promise<void> {
const claimer = await ContactRepository.findByAddress(claimerAddress)
if (!claimer) {
if (!claimer && !claimerIdentity) {
throw new Error('claimer not found')
}
const attestedClaim = await AttestationService.attestClaim(
Expand All @@ -143,7 +148,7 @@ class AttestationWorkflow {
attestation: attestedClaim.attestation,
cTypeHash: attestedClaim.request.claim.cTypeHash,
claimerAddress: attestedClaim.request.claim.owner,
claimerAlias: claimer.metaData.name,
claimerAlias: (claimer && claimer.metaData.name) || '',
created: Date.now(),
})

Expand All @@ -152,7 +157,19 @@ class AttestationWorkflow {
content: attestedClaim,
type: MessageBodyType.SUBMIT_ATTESTATION_FOR_CLAIM,
}
return MessageRepository.send([claimer], attestationMessageBody)

if (claimerIdentity) {
return MessageRepository.sendToPublicIdentity(
claimerIdentity,
attestationMessageBody
)
}

if (claimer) {
return MessageRepository.send([claimer], attestationMessageBody)
}

throw new Error('unreachable code')
}

/**
Expand Down
39 changes: 36 additions & 3 deletions src/services/MessageRepository.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,27 @@ class MessageRepository {
})
}

/**
* takes a public identity
* converts them to Contacts and initiates message sending
*
* @param receivers
* @param messageBody
*/
public static sendToPublicIdentity(
receiver: sdk.IPublicIdentity,
messageBody: sdk.MessageBody
): Promise<void> {
const receiverContact: IContact = {
metaData: {
name: '',
},
publicIdentity: receiver,
}

return MessageRepository.send([receiverContact], messageBody)
}

public static async multiSendToAddresses(
receiverAddresses: Array<IContact['publicIdentity']['address']>,
messageBodies: sdk.MessageBody[]
Expand Down Expand Up @@ -132,13 +153,24 @@ class MessageRepository {
encryptedMessages.map((encryptedMessage: sdk.IEncryptedMessage) => {
return ContactRepository.findByAddress(
encryptedMessage.senderAddress
).then((sender: IContact) => {
).then((contact: IContact) => {
try {
const m: sdk.IMessage = sdk.Message.createFromEncryptedMessage(
const m = sdk.Message.createFromEncryptedMessage(
encryptedMessage,
myIdentity
)
sdk.Message.ensureOwnerIsSender(m)
let sender = contact
if (!sender) {
sender = {
metaData: { name: '', unregistered: true },
publicIdentity: {
address: encryptedMessage.senderAddress,
boxPublicKeyAsHex: encryptedMessage.senderBoxPublicKey,
},
}
}

return {
...m,
encryptedMessage,
Expand Down Expand Up @@ -188,7 +220,8 @@ class MessageRepository {
.then(response => response.json())
.then(() => {
notifySuccess(
`Message '${messageBody.type}' to ${receiver.metaData.name} successfully sent.`
`Message '${messageBody.type}' to receiver ${receiver.metaData
.name || receiver.publicIdentity.address} successfully sent.`
)
})
.catch(error => {
Expand Down

0 comments on commit 1675017

Please sign in to comment.